use of org.objectweb.asm.AnnotationVisitor in project cdap by caskdata.
the class ArtifactInspector method isPlugin.
/**
* Detects if a class is annotated with {@link Plugin} without loading the class.
*
* @param className name of the class
* @param classLoader ClassLoader for loading the class file of the given class
* @return true if the given class is annotated with {@link Plugin}
*/
private boolean isPlugin(String className, ClassLoader classLoader) {
try (InputStream is = classLoader.getResourceAsStream(className.replace('.', '/') + ".class")) {
if (is == null) {
return false;
}
// Use ASM to inspect the class bytecode to see if it is annotated with @Plugin
final boolean[] isPlugin = new boolean[1];
ClassReader cr = new ClassReader(is);
cr.accept(new ClassVisitor(Opcodes.ASM5) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (Plugin.class.getName().equals(Type.getType(desc).getClassName()) && visible) {
isPlugin[0] = true;
}
return super.visitAnnotation(desc, visible);
}
}, ClassReader.SKIP_CODE | ClassReader.SKIP_DEBUG | ClassReader.SKIP_FRAMES);
return isPlugin[0];
} catch (IOException e) {
// If failed to open the class file, then it cannot be a plugin
LOG.warn("Failed to open class file for {}", className, e);
return false;
}
}
use of org.objectweb.asm.AnnotationVisitor in project j2objc by google.
the class PackageInfoLookup method parseDataFromClassFile.
private PackageData parseDataFromClassFile(InputFile file) throws IOException {
PackageDataBuilder builder = new PackageDataBuilder();
ClassReader classReader = new ClassReader(file.getInputStream());
classReader.accept(new ClassVisitor(Opcodes.ASM5) {
@Override
public AnnotationVisitor visitAnnotation(String desc, boolean visible) {
if (desc.equals("Lcom/google/j2objc/annotations/ObjectiveCName;")) {
return new AnnotationVisitor(Opcodes.ASM5) {
@Override
public void visit(String name, Object value) {
if (name.equals("value")) {
builder.setObjectiveCName(value.toString());
}
}
};
} else if (desc.equals("Ljavax/annotation/ParametersAreNonnullByDefault;")) {
builder.setParametersAreNonnullByDefault();
} else if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport;")) {
return new AnnotationVisitor(Opcodes.ASM5) {
@Override
public void visitEnum(String name, String desc, String value) {
if (desc.equals("Lcom/google/j2objc/annotations/ReflectionSupport$Level;") && name.equals("value")) {
builder.setReflectionSupportLevel(ReflectionSupport.Level.valueOf(value));
}
}
};
}
return null;
}
}, 0);
return builder.build();
}
use of org.objectweb.asm.AnnotationVisitor in project groovy-core by groovy.
the class AsmClassGenerator method visitParameterAnnotations.
private void visitParameterAnnotations(Parameter parameter, int paramNumber, MethodVisitor mv) {
for (AnnotationNode an : parameter.getAnnotations()) {
// skip built-in properties
if (an.isBuiltIn())
continue;
if (an.hasSourceRetention())
continue;
final String annotationDescriptor = BytecodeHelper.getTypeDescription(an.getClassNode());
AnnotationVisitor av = mv.visitParameterAnnotation(paramNumber, annotationDescriptor, an.hasRuntimeRetention());
visitAnnotationAttributes(an, av);
av.visitEnd();
}
}
use of org.objectweb.asm.AnnotationVisitor in project groovy-core by groovy.
the class AsmClassGenerator method visitAnnotationArrayElement.
private void visitAnnotationArrayElement(Expression expr, int arrayElementType, AnnotationVisitor av) {
switch(arrayElementType) {
case 1:
AnnotationNode atAttr = (AnnotationNode) ((AnnotationConstantExpression) expr).getValue();
AnnotationVisitor av2 = av.visitAnnotation(null, BytecodeHelper.getTypeDescription(atAttr.getClassNode()));
visitAnnotationAttributes(atAttr, av2);
av2.visitEnd();
break;
case 2:
av.visit(null, ((ConstantExpression) expr).getValue());
break;
case 3:
av.visit(null, Type.getType(BytecodeHelper.getTypeDescription(expr.getType())));
break;
case 4:
PropertyExpression propExpr = (PropertyExpression) expr;
av.visitEnum(null, BytecodeHelper.getTypeDescription(propExpr.getObjectExpression().getType()), String.valueOf(((ConstantExpression) propExpr.getProperty()).getValue()));
break;
}
}
use of org.objectweb.asm.AnnotationVisitor in project groovy-core by groovy.
the class AsmClassGenerator method visitAnnotations.
private void visitAnnotations(AnnotatedNode targetNode, Object visitor) {
for (AnnotationNode an : targetNode.getAnnotations()) {
// skip built-in properties
if (an.isBuiltIn())
continue;
if (an.hasSourceRetention())
continue;
AnnotationVisitor av = getAnnotationVisitor(targetNode, an, visitor);
visitAnnotationAttributes(an, av);
av.visitEnd();
}
}
Aggregations