Skip to content

Commit

Permalink
Fixes piranhacloud#3335 - Add warning about not including the --add-e…
Browse files Browse the repository at this point in the history
…xports flag
  • Loading branch information
Thihup committed Jun 20, 2023
1 parent 15488a3 commit 9f7f812
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 18 deletions.
16 changes: 11 additions & 5 deletions extension/annotationscan-classfile/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<argLine>${argLine} --add-exports=java.base/jdk.internal.classfile=cloud.piranha.extension.annotationscan.classfile --add-exports=java.base/jdk.internal.classfile.attribute=cloud.piranha.extension.annotationscan.classfile</argLine>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.Set;
import java.util.stream.Stream;

import static java.lang.System.Logger.Level.ERROR;
import static java.lang.System.Logger.Level.WARNING;
import static java.lang.invoke.MethodType.methodType;
import static java.util.Arrays.stream;
Expand Down Expand Up @@ -219,42 +220,63 @@ class Holder {
*/
private static final int TRUSTED = -1;

/**
* Stores the logger.
*/
private static final Logger LOGGER = System.getLogger(ClassfileAnnotationScanInitializer.class.getName());

private Holder() {
}

static {
try {
MethodHandles.Lookup trustedLookup = getLookup();
MethodHandles.Lookup lookup = getLookup();

Class<?> classFileClass = trustedLookup.findClass("jdk.internal.classfile.Classfile");
Class<?> classModelClass = trustedLookup.findClass("jdk.internal.classfile.ClassModel");
Class<?> attributeMapperClass = trustedLookup.findClass("jdk.internal.classfile.AttributeMapper");
Class<?> attributesClass = trustedLookup.findClass("jdk.internal.classfile.Attributes");
Class<?> annotationClass = trustedLookup.findClass("jdk.internal.classfile.Annotation");
Class<?> classFileOptionsArrayClass = trustedLookup.findClass("[Ljdk.internal.classfile.Classfile$Option;");
Class<?> runtimeVisibleAnnotationsAttributeClass = trustedLookup.findClass("jdk.internal.classfile.attribute.RuntimeVisibleAnnotationsAttribute");
Class<?> classFileClass = lookup.findClass("jdk.internal.classfile.Classfile");
Class<?> classModelClass = lookup.findClass("jdk.internal.classfile.ClassModel");
Class<?> attributeMapperClass = lookup.findClass("jdk.internal.classfile.AttributeMapper");
Class<?> attributesClass = lookup.findClass("jdk.internal.classfile.Attributes");
Class<?> annotationClass = lookup.findClass("jdk.internal.classfile.Annotation");
Class<?> classFileOptionsArrayClass = lookup.findClass("[Ljdk.internal.classfile.Classfile$Option;");
Class<?> runtimeVisibleAnnotationsAttributeClass = lookup.findClass("jdk.internal.classfile.attribute.RuntimeVisibleAnnotationsAttribute");

MethodHandle parse = trustedLookup.findStatic(classFileClass, "parse", methodType(classModelClass, byte[].class, classFileOptionsArrayClass));
MethodHandle parse = lookup.findStatic(classFileClass, "parse", methodType(classModelClass, byte[].class, classFileOptionsArrayClass));
Object classFileOptionsEmptyArray = MethodHandles.arrayConstructor(classFileOptionsArrayClass).invoke(0);
PARSE = MethodHandles.insertArguments(parse, 1, classFileOptionsEmptyArray)
.asType(methodType(Object.class, byte[].class));
FIND_ATTRIBUTE = trustedLookup.findVirtual(classModelClass, "findAttribute", methodType(Optional.class, attributeMapperClass))
FIND_ATTRIBUTE = lookup.findVirtual(classModelClass, "findAttribute", methodType(Optional.class, attributeMapperClass))
.asType(methodType(Optional.class, Object.class, Object.class));
ANNOTATIONS = trustedLookup.findVirtual(runtimeVisibleAnnotationsAttributeClass, "annotations", methodType(List.class))
ANNOTATIONS = lookup.findVirtual(runtimeVisibleAnnotationsAttributeClass, "annotations", methodType(List.class))
.asType(methodType(List.class, Object.class));
CLASS_SYMBOL = trustedLookup.findVirtual(annotationClass, "classSymbol", methodType(ClassDesc.class))
CLASS_SYMBOL = lookup.findVirtual(annotationClass, "classSymbol", methodType(ClassDesc.class))
.asType(methodType(ClassDesc.class, Object.class));
RUNTIME_VISIBLE_ANNOTATIONS_ATTRIBUTE_MAPPER = trustedLookup.findStaticGetter(attributesClass, "RUNTIME_VISIBLE_ANNOTATIONS", attributeMapperClass).invoke();
RUNTIME_VISIBLE_ANNOTATIONS_ATTRIBUTE_MAPPER = lookup.findStaticGetter(attributesClass, "RUNTIME_VISIBLE_ANNOTATIONS", attributeMapperClass).invoke();
} catch (Throwable e) {
throw new ExceptionInInitializerError(e);
}
}

private static MethodHandles.Lookup getLookup() {
MethodHandles.Lookup lookup = MethodHandles.lookup();
try {
lookup.findClass("jdk.internal.classfile.Classfile");
lookup.findClass("jdk.internal.classfile.attribute.RuntimeVisibleAnnotationsAttribute");
return lookup;
} catch (ClassNotFoundException | IllegalAccessException e) {
Module module = lookup.lookupClass().getModule();
String moduleName = module.isNamed() ? module.getName() : "ALL-UNNAMED";
LOGGER.log(WARNING, "The classfile annotation scan extension requires the following flags to work: " +
"--add-exports=java.base/jdk.internal.classfile={0} --add-exports=java.base/jdk.internal.classfile.attribute={0}. " +
"Using non-standard APIs as fallback for now.", moduleName);
}
try {
ReflectionFactory reflectionFactory = ReflectionFactory.getReflectionFactory();
Class<MethodHandles.Lookup> lookupClass = MethodHandles.Lookup.class;
Constructor<MethodHandles.Lookup> constructor = lookupClass.getDeclaredConstructor(Class.class, Class.class, int.class);
Constructor<?> lookupConstructor = reflectionFactory.newConstructorForSerialization(lookupClass, constructor);
return (MethodHandles.Lookup) lookupConstructor.newInstance(Object.class, null, TRUSTED);
} catch (Throwable e) {
LOGGER.log(ERROR, "Could not use the non-standard APIs, this extension will fail. Please add the flags to the command line.");
throw new ExceptionInInitializerError(e);
}
}
Expand Down

0 comments on commit 9f7f812

Please sign in to comment.