-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add package info support #60
Draft
dufoli
wants to merge
1
commit into
quarkusio:master
Choose a base branch
from
dufoli:pkg
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−1
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package io.quarkus.gizmo; | ||
|
||
import org.objectweb.asm.AnnotationVisitor; | ||
import org.objectweb.asm.ClassWriter; | ||
import org.objectweb.asm.Opcodes; | ||
|
||
import java.lang.annotation.RetentionPolicy; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
public class PackageCreator implements AutoCloseable, AnnotatedElement { | ||
|
||
public static PackageCreator.Builder builder() { | ||
return new PackageCreator.Builder(); | ||
} | ||
|
||
private final List<AnnotationCreatorImpl> annotations = new ArrayList<>(); | ||
private final BytecodeCreatorImpl enclosing; | ||
private final ClassOutput classOutput; | ||
private final String packageName; | ||
|
||
PackageCreator(BytecodeCreatorImpl enclosing, ClassOutput classOutput, String packageName) { | ||
this.enclosing = enclosing; | ||
this.classOutput = classOutput; | ||
this.packageName = packageName.replace('.', '/') + "/package-info"; | ||
} | ||
|
||
@Override | ||
public AnnotationCreator addAnnotation(String annotationType, RetentionPolicy retentionPolicy) { | ||
AnnotationCreatorImpl ac = new AnnotationCreatorImpl(annotationType, retentionPolicy); | ||
annotations.add(ac); | ||
return ac; | ||
} | ||
|
||
/** | ||
* Write the class bytes to the given class output. | ||
* | ||
* @param classOutput the class output (must not be {@code null}) | ||
*/ | ||
public void writeTo(ClassOutput classOutput) { | ||
Objects.requireNonNull(classOutput); | ||
ClassWriter file = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); | ||
final GizmoClassVisitor cv = new GizmoClassVisitor(Gizmo.ASM_API_VERSION, file, classOutput.getSourceWriter(packageName)); | ||
cv.visit(Opcodes.V1_5, Opcodes.ACC_ABSTRACT + Opcodes.ACC_INTERFACE, packageName, null, | ||
"java/lang/Object", null); | ||
|
||
for(AnnotationCreatorImpl annotation : annotations) { | ||
AnnotationVisitor av = cv.visitAnnotation(DescriptorUtils.extToInt(annotation.getAnnotationType()), annotation.getRetentionPolicy() == RetentionPolicy.RUNTIME); | ||
for(Map.Entry<String, Object> e : annotation.getValues().entrySet()) { | ||
AnnotationUtils.visitAnnotationValue(av, e.getKey(), e.getValue()); | ||
} | ||
av.visitEnd(); | ||
} | ||
|
||
cv.visitEnd(); | ||
|
||
classOutput.write(packageName, file.toByteArray()); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
final ClassOutput classOutput = this.classOutput; | ||
if (classOutput != null) { | ||
writeTo(classOutput); | ||
} | ||
} | ||
public static class Builder { | ||
|
||
private ClassOutput classOutput; | ||
|
||
private BytecodeCreatorImpl enclosing; | ||
|
||
private String packageName; | ||
|
||
Builder() { | ||
} | ||
|
||
PackageCreator.Builder enclosing(BytecodeCreatorImpl enclosing) { | ||
this.enclosing = enclosing; | ||
return this; | ||
} | ||
|
||
public PackageCreator.Builder classOutput(ClassOutput classOutput) { | ||
this.classOutput = classOutput; | ||
return this; | ||
} | ||
|
||
public PackageCreator.Builder packageName(String packageName) { | ||
this.packageName = packageName; | ||
return this; | ||
} | ||
|
||
public PackageCreator build() { | ||
Objects.requireNonNull(packageName); | ||
return new PackageCreator(enclosing, classOutput, packageName); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.quarkus.gizmo; | ||
|
||
import org.jboss.jandex.AnnotationInstance; | ||
import org.jboss.jandex.AnnotationValue; | ||
import org.jboss.jandex.DotName; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
public class PackageCreatorTest { | ||
@Test | ||
public void testPackageAnnotationWithString() throws Exception { | ||
TestClassLoader cl = new TestClassLoader(getClass().getClassLoader()); | ||
try (PackageCreator creator = PackageCreator.builder().classOutput(cl).packageName("com.myTest").build()) { | ||
creator.addAnnotation(AnnotationInstance.create(DotName.createSimple(MyAnnotation.class.getName()), null, new AnnotationValue[] { | ||
AnnotationValue.createEnumValue("enumVal", DotName.createSimple("io.quarkus.gizmo.MyEnum"), "NO") | ||
} )); | ||
} | ||
//supported only with java 9 | ||
/* | ||
MyAnnotation annotation = cl.getDefinedPackage("com.MyTest") | ||
.getAnnotation(MyAnnotation.class); | ||
Assert.assertEquals(MyEnum.NO, annotation.enumVal()); | ||
|
||
*/ | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't you just do getClass().getPackage() for a class in the package?