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

raphw / byte-buddy / #641

19 Aug 2024 10:38PM CUT coverage: 85.389% (-0.06%) from 85.448%
#641

push

raphw
Disable validation for minor breakage of protected API.

28759 of 33680 relevant lines covered (85.39%)

0.85 hits per line

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

32.0
/byte-buddy-dep/src/main/java/net/bytebuddy/utility/OpenedClassReader.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 net.bytebuddy.ClassFileVersion;
19
import net.bytebuddy.build.AccessControllerPlugin;
20
import net.bytebuddy.utility.privilege.GetSystemPropertyAction;
21
import org.objectweb.asm.ClassReader;
22
import org.objectweb.asm.Opcodes;
23

24
import java.security.PrivilegedAction;
25

26
/**
27
 * A factory for a {@link ClassReader} that does not apply a class file version check if the
28
 * {@code net.bytebuddy.experimental} property is set.
29
 */
30
public class OpenedClassReader implements AsmClassReader.Factory {
×
31

32
    /**
33
     * Indicates that Byte Buddy should not validate the maximum supported class file version.
34
     */
35
    public static final String EXPERIMENTAL_PROPERTY = "net.bytebuddy.experimental";
36

37
    /**
38
     * {@code true} if Byte Buddy is executed in experimental mode.
39
     */
40
    public static final boolean EXPERIMENTAL;
41

42
    /**
43
     * Indicates the ASM API version that is used throughout Byte Buddy.
44
     */
45
    public static final int ASM_API;
46

47
    /*
48
     * Checks the experimental property.
49
     */
50
    static {
51
        boolean experimental;
52
        try {
53
            experimental = Boolean.parseBoolean(doPrivileged(new GetSystemPropertyAction(EXPERIMENTAL_PROPERTY)));
1✔
54
        } catch (Exception ignored) {
×
55
            experimental = false;
×
56
        }
1✔
57
        EXPERIMENTAL = experimental;
1✔
58
        ASM_API = Opcodes.ASM9;
1✔
59
    }
1✔
60

61
    /**
62
     * {@inheritDoc}
63
     */
64
    public AsmClassReader make(byte[] binaryRepresentation) {
65
        return new AsmClassReader.Default(of(binaryRepresentation));
×
66
    }
67

68
    /**
69
     * A proxy for {@code java.security.AccessController#doPrivileged} that is activated if available.
70
     *
71
     * @param action The action to execute from a privileged context.
72
     * @param <T>    The type of the action's resolved value.
73
     * @return The action's resolved value.
74
     */
75
    @AccessControllerPlugin.Enhance
76
    private static <T> T doPrivileged(PrivilegedAction<T> action) {
77
        return action.run();
×
78
    }
79

80
    /**
81
     * Creates a class reader for the given binary representation of a class file.
82
     *
83
     * @param binaryRepresentation The binary representation of a class file to read.
84
     * @return An appropriate class reader.
85
     */
86
    public static ClassReader of(byte[] binaryRepresentation) {
87
        ClassFileVersion classFileVersion = ClassFileVersion.ofClassFile(binaryRepresentation), latest = ClassFileVersion.latest();
1✔
88
        if (classFileVersion.isGreaterThan(latest)) {
1✔
89
            if (EXPERIMENTAL) {
×
90
                binaryRepresentation[4] = (byte) (latest.getMinorVersion() >>> 8);
×
91
                binaryRepresentation[5] = (byte) latest.getMinorVersion();
×
92
                binaryRepresentation[6] = (byte) (latest.getMajorVersion() >>> 8);
×
93
                binaryRepresentation[7] = (byte) latest.getMajorVersion();
×
94
                ClassReader classReader = new ClassReader(binaryRepresentation);
×
95
                binaryRepresentation[4] = (byte) (classFileVersion.getMinorVersion() >>> 8);
×
96
                binaryRepresentation[5] = (byte) classFileVersion.getMinorVersion();
×
97
                binaryRepresentation[6] = (byte) (classFileVersion.getMajorVersion() >>> 8);
×
98
                binaryRepresentation[7] = (byte) classFileVersion.getMajorVersion();
×
99
                return classReader;
×
100
            } else {
101
                throw new IllegalArgumentException(classFileVersion
×
102
                        + " is not supported by the current version of Byte Buddy which officially supports " + latest
103
                        + " - update Byte Buddy or set " + EXPERIMENTAL_PROPERTY + " as a VM property");
104
            }
105
        } else {
106
            return new ClassReader(binaryRepresentation);
1✔
107
        }
108
    }
109
}
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

© 2025 Coveralls, Inc