AppName.java

1
package com.rox.video;
2
3
import java.lang.reflect.Field;
4
import java.util.Optional;
5
import java.util.function.Consumer;
6
7
/**
8
 * The app's name as the desktop shows it. On X11 (including XWayland) GNOME's dock and Alt-Tab name a
9
 * Java window after its window class, which AWT takes from the main class - {@code com-rox-RomVideoSmokeDemo}
10
 * - with no public way to change it.
11
 */
12
public final class AppName {
13
    static final String X11_WINDOW_CLASS_FIELD = "awtAppClassName";
14
    static final String OPEN_FLAG = "--add-opens java.desktop/sun.awt.X11=ALL-UNNAMED";
15
16
    private AppName(){
17
    }
18
19
    /**
20
     * Names every window created from now on - so call it before the first. Only X11 needs this; there
21
     * it needs {@value #OPEN_FLAG}, and without that the default name stays, with a warning.
22
     */
23
    public static void set(final String name){
24 1 1. set : removed call to com/rox/video/AppName::set → SURVIVED
        set(name, DesktopPlatform.x11Toolkit(), System.err::println);
25
    }
26
27
    /** @param x11Toolkit X11's toolkit class, if that's the toolkit in use */
28
    static void set(final String name, final Optional<Class<?>> x11Toolkit, final Consumer<String> warn){
29 1 1. set : removed call to java/util/Optional::ifPresent → KILLED
        x11Toolkit.ifPresent(toolkit -> {
30 1 1. lambda$set$0 : negated conditional → KILLED
            if (!setStaticField(toolkit, X11_WINDOW_CLASS_FIELD, name)){
31 1 1. lambda$set$0 : removed call to java/util/function/Consumer::accept → KILLED
                warn.accept("Windows keep the default name - run with " + OPEN_FLAG + " to call them " + name);
32
            }
33
        });
34
    }
35
36
    /** @return whether it could be set - the field must exist and its package be open to this one */
37
    static boolean setStaticField(final Class<?> holder, final String field, final Object value){
38
        try {
39
            final Field target = holder.getDeclaredField(field);
40 1 1. setStaticField : removed call to java/lang/reflect/Field::setAccessible → KILLED
            target.setAccessible(true);
41 1 1. setStaticField : removed call to java/lang/reflect/Field::set → KILLED
            target.set(null, value);
42 1 1. setStaticField : replaced boolean return with false for com/rox/video/AppName::setStaticField → KILLED
            return true;
43
        } catch (ReflectiveOperationException | RuntimeException e){
44
            //RuntimeException: InaccessibleObjectException, for a package that isn't open
45 1 1. setStaticField : replaced boolean return with true for com/rox/video/AppName::setStaticField → KILLED
            return false;
46
        }
47
    }
48
}

Mutations

24

1.1
Location : set
Killed by : none
removed call to com/rox/video/AppName::set → SURVIVED
Covering tests

29

1.1
Location : set
Killed by : com.rox.video.AppNameTest.[engine:junit-jupiter]/[class:com.rox.video.AppNameTest]/[method:onX11WithoutTheFieldOpenItWarnsHowToFixIt()]
removed call to java/util/Optional::ifPresent → KILLED

30

1.1
Location : lambda$set$0
Killed by : com.rox.video.AppNameTest.[engine:junit-jupiter]/[class:com.rox.video.AppNameTest]/[method:onX11WithoutTheFieldOpenItWarnsHowToFixIt()]
negated conditional → KILLED

31

1.1
Location : lambda$set$0
Killed by : com.rox.video.AppNameTest.[engine:junit-jupiter]/[class:com.rox.video.AppNameTest]/[method:onX11WithoutTheFieldOpenItWarnsHowToFixIt()]
removed call to java/util/function/Consumer::accept → KILLED

40

1.1
Location : setStaticField
Killed by : com.rox.video.AppNameTest.[engine:junit-jupiter]/[class:com.rox.video.AppNameTest]/[method:setsAStaticField()]
removed call to java/lang/reflect/Field::setAccessible → KILLED

41

1.1
Location : setStaticField
Killed by : com.rox.video.AppNameTest.[engine:junit-jupiter]/[class:com.rox.video.AppNameTest]/[method:aFieldThatCannotBeWrittenIsNotSet()]
removed call to java/lang/reflect/Field::set → KILLED

42

1.1
Location : setStaticField
Killed by : com.rox.video.AppNameTest.[engine:junit-jupiter]/[class:com.rox.video.AppNameTest]/[method:setsAStaticField()]
replaced boolean return with false for com/rox/video/AppName::setStaticField → KILLED

45

1.1
Location : setStaticField
Killed by : com.rox.video.AppNameTest.[engine:junit-jupiter]/[class:com.rox.video.AppNameTest]/[method:aMissingFieldIsNotSet()]
replaced boolean return with true for com/rox/video/AppName::setStaticField → KILLED

Active mutators

Tests examined


Report generated by PIT 1.22.1