• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

raphw / byte-buddy / #868

17 Mar 2026 07:53PM UTC coverage: 83.945% (+0.005%) from 83.94%
#868

push

raphw
Fix test.

29966 of 35697 relevant lines covered (83.95%)

0.84 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.09
/byte-buddy-dep/src/main/java/net/bytebuddy/utility/GraalImageCode.java
1
/*
2
 * Copyright 2014 - Present Rafael Winterhalter
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.bytebuddy.utility;
17

18
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19
import net.bytebuddy.build.AccessControllerPlugin;
20
import net.bytebuddy.utility.nullability.MaybeNull;
21
import net.bytebuddy.utility.privilege.GetSystemPropertyAction;
22

23
import java.lang.reflect.Method;
24
import java.security.PrivilegedAction;
25
import java.util.Arrays;
26
import java.util.Comparator;
27
import java.util.List;
28
import java.util.Locale;
29

30
/**
31
 * A utility that resolves Graal VM native image properties.
32
 */
33
public enum GraalImageCode {
1✔
34

35
    /**
36
     * Indicates that a Graal VM assisted configuration agent is running.
37
     */
38
    AGENT(true, false),
1✔
39

40
    /**
41
     * Indicates that a Graal VM native image build is executed.
42
     */
43
    BUILD(true, false),
1✔
44

45
    /**
46
     * Indicates that a Graal VM native image is being executed.
47
     */
48
    RUNTIME(true, true),
1✔
49

50
    /**
51
     * Indicates that a Graal VM property is set to an unknown value.
52
     */
53
    UNKNOWN(false, false),
1✔
54

55
    /**
56
     * Indicates that no Graal VM property is set.
57
     */
58
    NONE(false, false);
1✔
59

60
    /**
61
     * Indicates that member properties and annotations should be sorted when being read.
62
     */
63
    public static final String REPRODUCIBLE_PROPERTIES = "net.bytebuddy.reproducible";
64

65
    /**
66
     * Resolves if properties should always be sorted at the cost of some runtime overhead.
67
     */
68
    private static final boolean SORTED = Boolean.parseBoolean(doPrivileged(new GetSystemPropertyAction(REPRODUCIBLE_PROPERTIES)));
1✔
69

70
    /**
71
     * The current image code or {@code null} if the image code was not yet resolved. The image code must be
72
     * initialized lazily to avoid that it's bound to a value during native compilation.
73
     */
74
    @MaybeNull
75
    private static GraalImageCode current;
76

77
    /**
78
     * Resolves the status of the Graal image code.
79
     *
80
     * @return The status of the Graal image code.
81
     */
82
    @SuppressFBWarnings(value = {"LI_LAZY_INIT_STATIC", "NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE"}, justification = "This behaviour is intended to avoid early binding in native images.")
83
    public static GraalImageCode getCurrent() {
84
        GraalImageCode current = GraalImageCode.current;
1✔
85
        if (current == null) {
1✔
86
            String value = doPrivileged(new GetSystemPropertyAction("org.graalvm.nativeimage.imagecode"));
1✔
87
            if (value == null) {
1✔
88
                String vendor = doPrivileged(new GetSystemPropertyAction("java.vm.vendor"));
1✔
89
                current = vendor != null && vendor.toLowerCase(Locale.US).contains("graalvm")
1✔
90
                        ? doPrivileged(ImageCodeContextAction.INSTANCE)
1✔
91
                        : GraalImageCode.NONE;
92
            } else if (value.equalsIgnoreCase("agent")) {
1✔
93
                current = GraalImageCode.AGENT;
×
94
            } else if (value.equalsIgnoreCase("runtime")) {
×
95
                current = GraalImageCode.RUNTIME;
×
96
            } else if (value.equalsIgnoreCase("buildtime")) {
×
97
                current = GraalImageCode.BUILD;
×
98
            } else {
99
                current = GraalImageCode.UNKNOWN;
×
100
            }
101
            GraalImageCode.current = current;
1✔
102
        }
103
        return current;
1✔
104
    }
105

106
    /**
107
     * Sorts the provided values only if an active Graal image code is set.
108
     *
109
     * @param value      The values to sort.
110
     * @param comparator the comparator to use.
111
     * @param <T>        The array component type.
112
     * @return The supplied array, potentially sorted.
113
     */
114
    public <T> T[] sorted(T[] value, Comparator<? super T> comparator) {
115
        if (SORTED || defined) {
1✔
116
            Arrays.sort(value, comparator);
1✔
117
        }
118
        return value;
1✔
119
    }
120

121
    /**
122
     * A proxy for {@code java.security.AccessController#doPrivileged} that is activated if available.
123
     *
124
     * @param action The action to execute from a privileged context.
125
     * @param <T>    The type of the action's resolved value.
126
     * @return The action's resolved value.
127
     */
128
    @MaybeNull
129
    @AccessControllerPlugin.Enhance
130
    private static <T> T doPrivileged(PrivilegedAction<T> action) {
131
        return action.run();
×
132
    }
133

134
    /**
135
     * {@code true} if this image code indicates that a valid Graal related property is set.
136
     */
137
    private final boolean defined;
138

139
    /**
140
     * {@code true} if this image code indicates that a Graal native image build is executed.
141
     */
142
    private final boolean nativeImageExecution;
143

144
    /**
145
     * Creates a new Graal image code.
146
     *
147
     * @param defined              {@code true} if this image code indicates that a valid Graal related property is set.
148
     * @param nativeImageExecution {@code true} if this image code indicates that a Graal native image build is executed.
149
     */
150
    GraalImageCode(boolean defined, boolean nativeImageExecution) {
1✔
151
        this.defined = defined;
1✔
152
        this.nativeImageExecution = nativeImageExecution;
1✔
153
    }
1✔
154

155
    /**
156
     * Returns {@code true} if this image code indicates that a valid Graal related property is set.
157
     *
158
     * @return {@code true} if this image code indicates that a valid Graal related property is set.
159
     */
160
    public boolean isDefined() {
161
        return defined;
1✔
162
    }
163

164
    /**
165
     * Returns {@code true} if this image code indicates that a Graal native image build is executed.
166
     *
167
     * @return {@code true} if this image code indicates that a Graal native image build is executed.
168
     */
169
    public boolean isNativeImageExecution() {
170
        return nativeImageExecution;
1✔
171
    }
172

173
    /**
174
     * A privileged action to resolve the image code via the current JVM processes input arguments, if available.
175
     */
176
    protected enum ImageCodeContextAction implements PrivilegedAction<GraalImageCode> {
×
177

178
        /**
179
         * The singleton instance.
180
         */
181
        INSTANCE;
×
182

183
        @Override
184
        public GraalImageCode run() {
185
            try {
186
                Method method = Class.forName("java.lang.management.ManagementFactory").getMethod("getRuntimeMXBean");
×
187
                @SuppressWarnings("unchecked")
188
                List<String> arguments = (List<String>) method.getReturnType().getMethod("getInputArguments").invoke(method.invoke(null));
×
189
                for (String argument : arguments) {
×
190
                    if (argument.startsWith("-agentlib:native-image-agent")) {
×
191
                        return GraalImageCode.AGENT;
×
192
                    }
193
                }
×
194
            } catch (Throwable ignored) {
×
195
                /* do nothing */
196
            }
×
197
            return GraalImageCode.NONE;
×
198
        }
199
    }
200
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc