Skip to content

Commit 5beb038

Browse files
committed
Merge branch 'asm-brige'
# Conflicts: # byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/TypeWriter.java
2 parents 0757d15 + 244957a commit 5beb038

File tree

17 files changed

+398
-58
lines changed

17 files changed

+398
-58
lines changed

byte-buddy-dep/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@
3434
</description>
3535

3636
<dependencies>
37+
<dependency>
38+
<groupId>codes.rafael.asmjdkbridge</groupId>
39+
<artifactId>asm-jdk-bridge</artifactId>
40+
<version>0.0.1-SNAPSHOT</version>
41+
</dependency>
3742
<dependency>
3843
<groupId>org.ow2.asm</groupId>
3944
<artifactId>asm</artifactId>
@@ -497,6 +502,7 @@
497502
<goal>testCompile</goal>
498503
</goals>
499504
<configuration>
505+
<enablePreview>true</enablePreview>
500506
<testIncludes>
501507
<include>net/bytebuddy/test/precompiled/v4/**</include>
502508
</testIncludes>
@@ -591,6 +597,7 @@
591597
<goal>testCompile</goal>
592598
</goals>
593599
<configuration>
600+
<enablePreview>true</enablePreview>
594601
<target>${bytecode.test.version}</target>
595602
<testIncludes>
596603
<include>net/bytebuddy/test/precompiled/v4jsr14/**</include>
@@ -907,6 +914,7 @@
907914
<goal>testCompile</goal>
908915
</goals>
909916
<configuration>
917+
<enablePreview>true</enablePreview>
910918
<parameters>true</parameters>
911919
<testIncludes>
912920
<include>net/bytebuddy/test/precompiled/v8parameters/**</include>

byte-buddy-dep/src/main/java/net/bytebuddy/ByteBuddy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ public ByteBuddy(ClassFileVersion classFileVersion) {
335335
InstrumentedType.Factory.Default.MODIFIABLE,
336336
DEFAULT_TYPE_VALIDATION,
337337
VisibilityBridgeStrategy.Default.ALWAYS,
338-
AsmClassReader.Factory.Default.INSTANCE,
339-
AsmClassWriter.Factory.Default.INSTANCE,
338+
AsmClassReader.Factory.Default.IMPLICIT,
339+
AsmClassWriter.Factory.Default.IMPLICIT,
340340
new LatentMatcher.Resolved<MethodDescription>(isSynthetic().or(isDefaultFinalizer())));
341341
}
342342

byte-buddy-dep/src/main/java/net/bytebuddy/asm/Advice.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public static Advice to(TypeDescription advice, ClassFileLocator classFileLocato
342342
classFileLocator,
343343
Collections.<OffsetMapping.Factory<?>>emptyList(),
344344
Delegator.ForRegularInvocation.Factory.INSTANCE,
345-
AsmClassReader.Factory.Default.INSTANCE);
345+
AsmClassReader.Factory.Default.IMPLICIT);
346346
}
347347

348348
/**
@@ -436,7 +436,7 @@ public static Advice to(TypeDescription enterAdvice, TypeDescription exitAdvice,
436436
classFileLocator,
437437
Collections.<OffsetMapping.Factory<?>>emptyList(),
438438
Delegator.ForRegularInvocation.Factory.INSTANCE,
439-
AsmClassReader.Factory.Default.INSTANCE);
439+
AsmClassReader.Factory.Default.IMPLICIT);
440440
}
441441

442442
/**
@@ -14876,7 +14876,7 @@ protected WithCustomMapping() {
1487614876
this(PostProcessor.NoOp.INSTANCE,
1487714877
Collections.<Class<? extends Annotation>, OffsetMapping.Factory<?>>emptyMap(),
1487814878
Delegator.ForRegularInvocation.Factory.INSTANCE,
14879-
AsmClassReader.Factory.Default.INSTANCE);
14879+
AsmClassReader.Factory.Default.IMPLICIT);
1488014880
}
1488114881

1488214882
/**

byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/ClassWriterStrategy.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ public AsmClassWriter make(int flags, AsmClassReader classReader) {
123123
* {@inheritDoc}
124124
*/
125125
public AsmClassWriter make(int flags, TypePool typePool) {
126-
return new AsmClassWriter.Default(classWriterStrategy.resolve(flags, typePool));
126+
return new AsmClassWriter.ForAsm(classWriterStrategy.resolve(flags, typePool));
127127
}
128128

129129
/**
130130
* {@inheritDoc}
131131
*/
132132
public AsmClassWriter make(int flags, AsmClassReader classReader, TypePool typePool) {
133133
ClassReader unwrapped = classReader.unwrap(ClassReader.class);
134-
return new AsmClassWriter.Default(unwrapped == null
134+
return new AsmClassWriter.ForAsm(unwrapped == null
135135
? classWriterStrategy.resolve(flags, typePool)
136136
: classWriterStrategy.resolve(flags, typePool, unwrapped));
137137
}

byte-buddy-dep/src/main/java/net/bytebuddy/dynamic/scaffold/TypeWriter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6126,12 +6126,14 @@ protected void onVisitInnerClass(String internalName, @MaybeNull String outerNam
61266126
}
61276127

61286128
@Override
6129+
@MaybeNull
61296130
protected FieldVisitor onVisitField(int modifiers, String name, String descriptor, @MaybeNull String signature, @MaybeNull Object value) {
61306131
visitedFields.add(new SignatureKey(name, descriptor));
61316132
return super.onVisitField(modifiers, name, descriptor, signature, value);
61326133
}
61336134

61346135
@Override
6136+
@MaybeNull
61356137
protected MethodVisitor onVisitMethod(int modifiers, String internalName, String descriptor, @MaybeNull String signature, @MaybeNull String[] exception) {
61366138
visitedMethods.add(new SignatureKey(internalName, descriptor));
61376139
return super.onVisitMethod(modifiers, internalName, descriptor, signature, exception);

byte-buddy-dep/src/main/java/net/bytebuddy/pool/TypePool.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ public Default(CacheProvider cacheProvider, ClassFileLocator classFileLocator, R
806806
* @param parentPool The parent type pool.
807807
*/
808808
public Default(CacheProvider cacheProvider, ClassFileLocator classFileLocator, ReaderMode readerMode, TypePool parentPool) {
809-
this(cacheProvider, classFileLocator, readerMode, AsmClassReader.Factory.Default.INSTANCE, parentPool);
809+
this(cacheProvider, classFileLocator, readerMode, AsmClassReader.Factory.Default.IMPLICIT, parentPool);
810810
}
811811

812812
/**

byte-buddy-dep/src/main/java/net/bytebuddy/utility/AsmClassReader.java

Lines changed: 139 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,17 @@
1515
*/
1616
package net.bytebuddy.utility;
1717

18+
import codes.rafael.asmjdkbridge.JdkClassReader;
19+
import net.bytebuddy.ClassFileVersion;
20+
import net.bytebuddy.build.AccessControllerPlugin;
1821
import net.bytebuddy.utility.nullability.MaybeNull;
22+
import net.bytebuddy.utility.privilege.GetSystemPropertyAction;
1923
import org.objectweb.asm.Attribute;
2024
import org.objectweb.asm.ClassReader;
2125
import org.objectweb.asm.ClassVisitor;
2226

27+
import java.security.PrivilegedAction;
28+
2329
/**
2430
* A facade for creating a class reader that accepts {@link ClassVisitor} instances and reader flags.
2531
*/
@@ -66,35 +72,121 @@ interface Factory {
6672
AsmClassReader make(byte[] binaryRepresentation, boolean experimental);
6773

6874
/**
69-
* A default implementation that creates a pure ASM {@link ClassReader}.
75+
* Default implementations for factories of {@link AsmClassReader}s.
7076
*/
7177
enum Default implements Factory {
7278

7379
/**
74-
* The singleton instance.
80+
* Uses a processor as it is configured by {@link OpenedClassReader#PROCESSOR_PROPERTY},
81+
* or {@link AsmClassWriter.Factory.Default#ASM_FIRST} if no implicit processor is defined.
7582
*/
76-
INSTANCE;
83+
IMPLICIT {
84+
85+
/**
86+
* {@inheritDoc}
87+
*/
88+
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
89+
return FACTORY.make(binaryRepresentation, experimental);
90+
}
91+
},
7792

7893
/**
79-
* {@inheritDoc}
94+
* A factory for a class reader that uses ASM's internal implementation whenever possible.
8095
*/
81-
public AsmClassReader make(byte[] binaryRepresentation) {
82-
return new AsmClassReader.Default(OpenedClassReader.of(binaryRepresentation));
96+
ASM_FIRST {
97+
/**
98+
* {@inheritDoc}
99+
*/
100+
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
101+
return ClassFileVersion.ofClassFile(binaryRepresentation).isGreaterThan(ClassFileVersion.latest())
102+
? CLASS_FILE_API_ONLY.make(binaryRepresentation)
103+
: ASM_ONLY.make(binaryRepresentation);
104+
}
105+
},
106+
107+
/**
108+
* A factory for a class reader that uses the class file API whenever possible.
109+
*/
110+
CLASS_FILE_API_FIRST {
111+
/**
112+
* {@inheritDoc}
113+
*/
114+
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
115+
return ClassFileVersion.ofThisVm().isAtLeast(ClassFileVersion.JAVA_V24)
116+
? CLASS_FILE_API_ONLY.make(binaryRepresentation)
117+
: ASM_ONLY.make(binaryRepresentation);
118+
}
119+
},
120+
121+
/**
122+
* A factory for a class reader that always uses ASM's internal implementation.
123+
*/
124+
ASM_ONLY {
125+
/**
126+
* {@inheritDoc}
127+
*/
128+
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
129+
return new ForAsm(OpenedClassReader.of(binaryRepresentation, experimental));
130+
}
131+
},
132+
133+
/**
134+
* A factory for a class reader that always uses the class file API.
135+
*/
136+
CLASS_FILE_API_ONLY {
137+
138+
/**
139+
* {@inheritDoc}
140+
*/
141+
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
142+
return new AsmClassReader.ForClassFileApi(new JdkClassReader(binaryRepresentation));
143+
}
144+
};
145+
146+
/**
147+
* The implicit factory to use for writing class files.
148+
*/
149+
private static final Factory FACTORY;
150+
151+
/*
152+
* Resolves the implicit reader factory, if any.
153+
*/
154+
static {
155+
String processor;
156+
try {
157+
processor = doPrivileged(new GetSystemPropertyAction(OpenedClassReader.PROCESSOR_PROPERTY));
158+
} catch (Throwable ignored) {
159+
processor = null;
160+
} // TODO: ASM_FIRST
161+
FACTORY = processor == null ? Default.CLASS_FILE_API_ONLY : Default.valueOf(processor);
162+
}
163+
164+
/**
165+
* A proxy for {@code java.security.AccessController#doPrivileged} that is activated if available.
166+
*
167+
* @param action The action to execute from a privileged context.
168+
* @param <T> The type of the action's resolved value.
169+
* @return The action's resolved value.
170+
*/
171+
@MaybeNull
172+
@AccessControllerPlugin.Enhance
173+
private static <T> T doPrivileged(PrivilegedAction<T> action) {
174+
return action.run();
83175
}
84176

85177
/**
86178
* {@inheritDoc}
87179
*/
88-
public AsmClassReader make(byte[] binaryRepresentation, boolean experimental) {
89-
return new AsmClassReader.Default(OpenedClassReader.of(binaryRepresentation, experimental));
180+
public AsmClassReader make(byte[] binaryRepresentation) {
181+
return make(binaryRepresentation, OpenedClassReader.EXPERIMENTAL);
90182
}
91183
}
92184
}
93185

94186
/**
95-
* A class reader for ASM's default {@link ClassReader}.
187+
* A class reader for ASM's own {@link ClassReader}.
96188
*/
97-
class Default implements AsmClassReader {
189+
class ForAsm implements AsmClassReader {
98190

99191
/**
100192
* Indicates that no custom attributes should be mapped.
@@ -107,11 +199,11 @@ class Default implements AsmClassReader {
107199
private final ClassReader classReader;
108200

109201
/**
110-
* Creates a new default ASM class reader.
202+
* Creates a new ASM class reader that uses ASM's internal implementation.
111203
*
112204
* @param classReader The class reader that represents the class file to be read.
113205
*/
114-
public Default(ClassReader classReader) {
206+
public ForAsm(ClassReader classReader) {
115207
this.classReader = classReader;
116208
}
117209

@@ -132,4 +224,39 @@ public void accept(ClassVisitor classVisitor, int flags) {
132224
classReader.accept(classVisitor, NO_ATTRIBUTES, flags);
133225
}
134226
}
227+
228+
/**
229+
* A class reader that is based upon the Class File API.
230+
*/
231+
class ForClassFileApi implements AsmClassReader {
232+
233+
/**
234+
* The class reader that represents the class file to be read.
235+
*/
236+
private final JdkClassReader classReader;
237+
238+
/**
239+
* Creates a new class reader that is based upon the Class File API.
240+
*
241+
* @param classReader The class reader that represents the class file to be read.
242+
*/
243+
public ForClassFileApi(JdkClassReader classReader) {
244+
this.classReader = classReader;
245+
}
246+
247+
/**
248+
* {@inheritDoc}
249+
*/
250+
@MaybeNull
251+
public <T> T unwrap(Class<T> type) {
252+
return null;
253+
}
254+
255+
/**
256+
* {@inheritDoc}
257+
*/
258+
public void accept(ClassVisitor classVisitor, int flags) {
259+
classReader.accept(classVisitor, flags);
260+
}
261+
}
135262
}

0 commit comments

Comments
 (0)