Java Code Examples for org.objectweb.asm.AnnotationVisitor#visit()
The following examples show how to use
org.objectweb.asm.AnnotationVisitor#visit() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IntArrayFieldInitializer.java From bazel with Apache License 2.0 | 6 votes |
@Override public boolean writeFieldDefinition( ClassWriter cw, boolean isFinal, boolean annotateTransitiveFields) { int accessLevel = Opcodes.ACC_STATIC; if (visibility != Visibility.PRIVATE) { accessLevel |= Opcodes.ACC_PUBLIC; } if (isFinal) { accessLevel |= Opcodes.ACC_FINAL; } FieldVisitor fv = cw.visitField(accessLevel, fieldName, DESC, null, null); if (annotateTransitiveFields && dependencyInfo.dependencyType() == DependencyInfo.DependencyType.TRANSITIVE) { AnnotationVisitor av = fv.visitAnnotation( RClassGenerator.PROVENANCE_ANNOTATION_CLASS_DESCRIPTOR, /*visible=*/ true); av.visit(RClassGenerator.PROVENANCE_ANNOTATION_LABEL_KEY, dependencyInfo.label()); av.visitEnd(); } fv.visitEnd(); return true; }
Example 2
Source File: AnnotationNode.java From Cafebabe with GNU General Public License v3.0 | 6 votes |
/** * Makes the given visitor visit a given annotation value. * * @param annotationVisitor * an annotation visitor. Maybe {@literal null}. * @param name * the value name. * @param value * the actual value. */ static void accept(final AnnotationVisitor annotationVisitor, final String name, final Object value) { if (annotationVisitor != null) { if (value instanceof String[]) { String[] typeValue = (String[]) value; annotationVisitor.visitEnum(name, typeValue[0], typeValue[1]); } else if (value instanceof AnnotationNode) { AnnotationNode annotationValue = (AnnotationNode) value; annotationValue.accept(annotationVisitor.visitAnnotation(name, annotationValue.desc)); } else if (value instanceof List) { AnnotationVisitor arrayAnnotationVisitor = annotationVisitor.visitArray(name); if (arrayAnnotationVisitor != null) { List<?> arrayValue = (List<?>) value; for (int i = 0, n = arrayValue.size(); i < n; ++i) { accept(arrayAnnotationVisitor, null, arrayValue.get(i)); } arrayAnnotationVisitor.visitEnd(); } } else { annotationVisitor.visit(name, value); } } }
Example 3
Source File: DefaultApplicationFactory.java From thorntail with Apache License 2.0 | 6 votes |
public static byte[] create(String name, String path) throws IOException { ClassReader reader = new ClassReader(basicClassBytes()); String slashName = name.replace('.', '/'); ClassWriter writer = new ClassWriter(0); Remapper remapper = new Remapper() { @Override public String map(String typeName) { if (typeName.equals("org/wildfly/swarm/jaxrs/runtime/DefaultApplication")) { return slashName; } return super.map(typeName); } }; ClassRemapper adapter = new ClassRemapper(writer, remapper); reader.accept(adapter, 0); AnnotationVisitor ann = writer.visitAnnotation("Ljavax/ws/rs/ApplicationPath;", true); ann.visit("value", path); ann.visitEnd(); writer.visitEnd(); return writer.toByteArray(); }
Example 4
Source File: AnnotationNode.java From JReFrameworker with MIT License | 6 votes |
/** * Makes the given visitor visit a given annotation value. * * @param annotationVisitor an annotation visitor. Maybe {@literal null}. * @param name the value name. * @param value the actual value. */ static void accept( final AnnotationVisitor annotationVisitor, final String name, final Object value) { if (annotationVisitor != null) { if (value instanceof String[]) { String[] typeValue = (String[]) value; annotationVisitor.visitEnum(name, typeValue[0], typeValue[1]); } else if (value instanceof AnnotationNode) { AnnotationNode annotationValue = (AnnotationNode) value; annotationValue.accept(annotationVisitor.visitAnnotation(name, annotationValue.desc)); } else if (value instanceof List) { AnnotationVisitor arrayAnnotationVisitor = annotationVisitor.visitArray(name); if (arrayAnnotationVisitor != null) { List<?> arrayValue = (List<?>) value; for (int i = 0, n = arrayValue.size(); i < n; ++i) { accept(arrayAnnotationVisitor, null, arrayValue.get(i)); } arrayAnnotationVisitor.visitEnd(); } } else { annotationVisitor.visit(name, value); } } }
Example 5
Source File: StateTrackingClassVisitor.java From scott with MIT License | 6 votes |
@Override public void visitEnd() { if (instrumentationActions.isJUnit4RuleInjectionRequired) { FieldVisitor fv = super.visitField(Opcodes.ACC_PUBLIC, "scottReportingRule", Type.getDescriptor(ScottReportingRule.class), null, null); fv.visitAnnotation(Type.getDescriptor(Rule.class), true).visitEnd(); } if (instrumentationActions.isJUnit5ExtensionInjectionRequired) { AnnotationVisitor av0 = super.visitAnnotation("Lorg/junit/jupiter/api/extension/ExtendWith;", true); AnnotationVisitor av1 = av0.visitArray("value"); av1.visit(null, Type.getType("Lhu/advancedweb/scott/runtime/ScottJUnit5Extension;")); av1.visitEnd(); av0.visitEnd(); } super.visitEnd(); }
Example 6
Source File: IntFieldInitializer.java From bazel with Apache License 2.0 | 6 votes |
@Override public boolean writeFieldDefinition( ClassWriter cw, boolean isFinal, boolean annotateTransitiveFields) { int accessLevel = Opcodes.ACC_STATIC; if (visibility != Visibility.PRIVATE) { accessLevel |= Opcodes.ACC_PUBLIC; } if (isFinal) { accessLevel |= Opcodes.ACC_FINAL; } FieldVisitor fv = cw.visitField(accessLevel, fieldName, DESC, null, isFinal ? value : null); if (annotateTransitiveFields && dependencyInfo.dependencyType() == DependencyInfo.DependencyType.TRANSITIVE) { AnnotationVisitor av = fv.visitAnnotation( RClassGenerator.PROVENANCE_ANNOTATION_CLASS_DESCRIPTOR, /*visible=*/ true); av.visit(RClassGenerator.PROVENANCE_ANNOTATION_LABEL_KEY, dependencyInfo.label()); av.visitEnd(); } fv.visitEnd(); return !isFinal; }
Example 7
Source File: AnnotationNode.java From JByteMod-Beta with GNU General Public License v2.0 | 6 votes |
/** * Makes the given visitor visit a given annotation value. * * @param av * an annotation visitor. Maybe <tt>null</tt>. * @param name * the value name. * @param value * the actual value. */ static void accept(final AnnotationVisitor av, final String name, final Object value) { if (av != null) { if (value instanceof String[]) { String[] typeconst = (String[]) value; av.visitEnum(name, typeconst[0], typeconst[1]); } else if (value instanceof AnnotationNode) { AnnotationNode an = (AnnotationNode) value; an.accept(av.visitAnnotation(name, an.desc)); } else if (value instanceof List) { AnnotationVisitor v = av.visitArray(name); if (v != null) { List<?> array = (List<?>) value; for (int j = 0; j < array.size(); ++j) { accept(v, null, array.get(j)); } v.visitEnd(); } } else { av.visit(name, value); } } }
Example 8
Source File: AbstractAnnotationDescriptionTest.java From byte-buddy with Apache License 2.0 | 5 votes |
@Override public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) { super.visit(version, access, name, signature, superName, interfaces); AnnotationVisitor annotationVisitor = visitAnnotation(Type.getDescriptor(BrokenAnnotation.class), true); annotationVisitor.visit("incompatibleValue", INTEGER); AnnotationVisitor incompatibleValueArray = annotationVisitor.visitArray("incompatibleValueArray"); incompatibleValueArray.visit(null, INTEGER); incompatibleValueArray.visitEnd(); if (incompatibleDeclaration) { annotationVisitor.visitAnnotation("incompatibleAnnotationDeclaration", Type.getDescriptor(BrokenAnnotationProperty.class)).visitEnd(); AnnotationVisitor incompatibleAnnotationDeclarationArray = annotationVisitor.visitArray("incompatibleAnnotationDeclarationArray"); incompatibleAnnotationDeclarationArray.visitAnnotation(null, Type.getDescriptor(BrokenAnnotationProperty.class)); incompatibleAnnotationDeclarationArray.visitEnd(); annotationVisitor.visitEnum("incompatibleEnumerationDeclaration", Type.getDescriptor(BrokenEnumerationProperty.class), FOO.toUpperCase()); AnnotationVisitor incompatibleEnumerationDeclarationArray = annotationVisitor.visitArray("incompatibleAnnotationDeclarationArray"); incompatibleEnumerationDeclarationArray.visitEnum(null, Type.getDescriptor(BrokenEnumerationProperty.class), FOO.toUpperCase()); incompatibleEnumerationDeclarationArray.visitEnd(); } if (allowMissingValues) { annotationVisitor.visitEnum("unknownEnumerationConstant", Type.getDescriptor(SampleEnumeration.class), FOO); AnnotationVisitor unknownEnumConstantArray = annotationVisitor.visitArray("unknownEnumerationConstantArray"); unknownEnumConstantArray.visitEnum(null, Type.getDescriptor(SampleEnumeration.class), FOO); unknownEnumConstantArray.visitEnd(); annotationVisitor.visit("missingType", Type.getType("Lnet/bytebuddy/inexistant/Foo;")); AnnotationVisitor missingTypeArray = annotationVisitor.visitArray("missingTypeArray"); missingTypeArray.visit(null, Type.getType("Lnet/bytebuddy/inexistant/Foo;")); missingTypeArray.visitEnd(); } annotationVisitor.visitEnd(); }
Example 9
Source File: InnerClassGenerator.java From Mixin with MIT License | 5 votes |
@Override public void visitSource(String source, String debug) { super.visitSource(source, debug); AnnotationVisitor av = this.cv.visitAnnotation("Lorg/spongepowered/asm/mixin/transformer/meta/MixinInner;", false); av.visit("mixin", this.info.getOwner().toString()); av.visit("name", this.info.getOriginalName().substring(this.info.getOriginalName().lastIndexOf('/') + 1)); av.visitEnd(); }
Example 10
Source File: ASMContentHandler.java From JByteMod-Beta with GNU General Public License v2.0 | 5 votes |
@Override public void begin(final String nm, final Attributes attrs) throws SAXException { AnnotationVisitor av = (AnnotationVisitor) peek(); if (av != null) { av.visit(attrs.getValue("name"), getValue(attrs.getValue("desc"), attrs.getValue("value"))); } }
Example 11
Source File: BytecodeOutputter.java From Concurnas with MIT License | 5 votes |
public static void addNullablefieldAnnotation(AnnotationVisitor av0, boolean[] nullstatus) { AnnotationVisitor av1 = av0.visitArray("nullable"); for(boolean item : nullstatus) { av1.visit(null, item?Boolean.TRUE:Boolean.FALSE); } av1.visitEnd(); }
Example 12
Source File: MethodDecoratorTest.java From pitest with Apache License 2.0 | 5 votes |
@Test public void shouldForwardVisitAnnotationDefaultCallsToChild() { AnnotationVisitor av = getTesteeVisitor().visitAnnotationDefault(); if (av != null) av.visit("foo", "bar"); getTesteeVisitor().visitInsn(NOP); getTesteeVisitor().visitInsn(Opcodes.ATHROW); getTesteeVisitor().visitEnd(); verify(this.mv).visitAnnotationDefault(); }
Example 13
Source File: ModClassGenerator.java From customstuff4 with GNU General Public License v3.0 | 4 votes |
private static byte[] generateClassCode(ModInfo info) { checkArgument(info.isValid(), "Invalid mod id"); ClassWriter cw = new ClassWriter(0); cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, "cs4mod/" + info.id, null, "java/lang/Object", new String[] {CS4_MOD}); // Mod annotation AnnotationVisitor av = cw.visitAnnotation(String.format("L%s;", MOD), true); av.visit("modid", info.id); av.visit("name", info.name); av.visit("version", info.version); av.visit("dependencies", String.format("required-after:%s;%s", CustomStuff4.ID, info.dependencies)); av.visitEnd(); // Constructor MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitFieldInsn(GETSTATIC, "net/minecraftforge/common/MinecraftForge", "EVENT_BUS", desc(EVENT_BUS)); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKEVIRTUAL, EVENT_BUS, "register", voidMethodDesc("java/lang/Object"), false); mv.visitInsn(RETURN); mv.visitMaxs(2, 1); mv.visitEnd(); // PreInit mv = cw.visitMethod(ACC_PUBLIC, "preInit", voidMethodDesc(PRE_INIT_EVENT), null, null); av = mv.visitAnnotation(String.format("L%s;", EVENT_HANDLER), true); av.visitEnd(); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, MOD_LOADER, "onPreInitMod", voidMethodDesc(CS4_MOD), false); mv.visitInsn(RETURN); mv.visitMaxs(1, 2); mv.visitEnd(); // Init mv = cw.visitMethod(ACC_PUBLIC, "init", voidMethodDesc(INIT_EVENT), null, null); av = mv.visitAnnotation(String.format("L%s;", EVENT_HANDLER), true); av.visitEnd(); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, MOD_LOADER, "onInitMod", voidMethodDesc(CS4_MOD), false); mv.visitInsn(RETURN); mv.visitMaxs(1, 2); mv.visitEnd(); // PostInit mv = cw.visitMethod(ACC_PUBLIC, "postInit", voidMethodDesc(POST_INIT_EVENT), null, null); av = mv.visitAnnotation(String.format("L%s;", EVENT_HANDLER), true); av.visitEnd(); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, MOD_LOADER, "onPostInitMod", voidMethodDesc(CS4_MOD), false); mv.visitInsn(RETURN); mv.visitMaxs(1, 2); mv.visitEnd(); // RegisterBlocks mv = cw.visitMethod(ACC_PUBLIC, "onRegisterBlocks", voidMethodDesc(REGISTER_BLOCKS_EVENT), voidMethodDesc(REGISTER_BLOCKS_SIGNATURE), null); av = mv.visitAnnotation(desc(SUBSCRIBE_EVENT), true); av.visitEnd(); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, MOD_LOADER, "onRegisterBlocks", voidMethodDesc(CS4_MOD), false); mv.visitInsn(RETURN); mv.visitMaxs(1, 2); mv.visitEnd(); // RegisterItems mv = cw.visitMethod(ACC_PUBLIC, "onRegisterItems", voidMethodDesc(REGISTER_ITEMS_EVENT), voidMethodDesc(REGISTER_ITEMS_SIGNATURE), null); av = mv.visitAnnotation(desc(SUBSCRIBE_EVENT), true); av.visitEnd(); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, MOD_LOADER, "onRegisterItems", voidMethodDesc(CS4_MOD), false); mv.visitInsn(RETURN); mv.visitMaxs(1, 2); mv.visitEnd(); // RegisterModels mv = cw.visitMethod(ACC_PUBLIC, "onRegisterModels", voidMethodDesc(REGISTER_MODELS_EVENT), null, null); av = mv.visitAnnotation(desc(SUBSCRIBE_EVENT), true); av.visitEnd(); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESTATIC, MOD_LOADER, "onRegisterModels", voidMethodDesc(CS4_MOD), false); mv.visitInsn(RETURN); mv.visitMaxs(1, 2); mv.visitEnd(); return cw.toByteArray(); }
Example 14
Source File: CommonMethodsBuilder.java From OpenPeripheral with MIT License | 4 votes |
public void createScriptMethodWrapper(String methodName, int methodIndex, IMethodExecutor executor) { final String generatedMethodName = methodName.replaceAll("[^A-Za-z0-9_]", "_") + "$" + Integer.toString(methodIndex); final MethodVisitor wrap = writer.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC, generatedMethodName, WRAP_TYPE.getDescriptor(), null, null); final Optional<String> returnSignal = executor.getReturnSignal(); final AnnotationVisitor av = wrap.visitAnnotation(CALLBACK_TYPE.getDescriptor(), true); av.visit("value", methodName); // functions with return signal always return immediately av.visit("direct", executor.isAsynchronous() || returnSignal.isPresent()); av.visit("doc", DocUtils.doc(executor.description())); av.visitEnd(); // TODO: getter/setter av.visitEnd(); wrap.visitCode(); wrap.visitVarInsn(Opcodes.ALOAD, 0); // this wrap.visitInsn(Opcodes.DUP); // this, this wrap.visitFieldInsn(Opcodes.GETFIELD, clsName, TARGET_FIELD_NAME, targetType.getDescriptor()); // this, target Label skip = new Label(); wrap.visitInsn(Opcodes.DUP); // this, target, target wrap.visitJumpInsn(Opcodes.IFNONNULL, skip); // this, target wrap.visitInsn(Opcodes.POP); // this wrap.visitMethodInsn(Opcodes.INVOKEINTERFACE, BASE_TYPE.getInternalName(), "invalidState", INVALID_STATE_TYPE.getDescriptor(), true); // result wrap.visitInsn(Opcodes.ARETURN); wrap.visitLabel(skip); wrap.visitFieldInsn(Opcodes.GETSTATIC, clsName, METHODS_FIELD_NAME, EXECUTORS_TYPE.getDescriptor()); // this, target, methods[] visitIntConst(wrap, methodIndex); // this, target, methods[], methodIndex wrap.visitInsn(Opcodes.AALOAD); // this, target, executor if (returnSignal.isPresent()) wrap.visitLdcInsn(returnSignal.get()); wrap.visitVarInsn(Opcodes.ALOAD, 1); // this, target, executor, (returnSignal), context wrap.visitVarInsn(Opcodes.ALOAD, 2); // this, target, executor, (returnSignal), context, args if (returnSignal.isPresent()) { final String baseCallName = executor.isAsynchronous()? "callSignallingAsync" : "callSignallingSync"; wrap.visitMethodInsn(Opcodes.INVOKEINTERFACE, SIGNALLING_BASE_TYPE.getInternalName(), baseCallName, SIGNALLING_CALLER_METHOD_TYPE.getDescriptor(), true); } else { wrap.visitMethodInsn(Opcodes.INVOKEINTERFACE, BASE_TYPE.getInternalName(), "call", CALLER_METHOD_TYPE.getDescriptor(), true); } wrap.visitInsn(Opcodes.ARETURN); wrap.visitMaxs(0, 0); wrap.visitEnd(); }
Example 15
Source File: Annotation.java From OSRS-Deobfuscator with BSD 2-Clause "Simplified" License | 4 votes |
public void accept(AnnotationVisitor visitor) { for (Element element : elements) visitor.visit(element.getName(), element.getValue()); visitor.visitEnd(); }
Example 16
Source File: AnnotatedClassTest.java From JAADAS with GNU General Public License v3.0 | 4 votes |
@Override protected void generate(TraceClassVisitor cw) { MethodVisitor mv; AnnotationVisitor av0; cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "soot/asm/backend/targets/AnnotatedClass", null, "java/lang/Object", null); cw.visitSource("AnnotatedClass.java", null); { av0 = cw.visitAnnotation("Lsoot/asm/backend/targets/MyTestAnnotation;", true); av0.visit("iVal", new Integer(1)); av0.visit("fVal", new Float("1.0")); av0.visit("lVal", new Long(1L)); av0.visit("dVal", new Double("1.0")); av0.visit("zVal", Boolean.TRUE); av0.visit("bVal", new Byte((byte)1)); av0.visit("sVal", new Short((short)1)); av0.visit("strVal", "1"); av0.visit("rVal", Type.getType("Lsoot/asm/backend/targets/AnnotatedClass;")); av0.visit("iAVal", new int[] {1,2,3,4}); { AnnotationVisitor av1 = av0.visitArray("sAVal"); av1.visit(null, "A"); av1.visit(null, "B"); av1.visit(null, "C"); av1.visitEnd(); } av0.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); }
Example 17
Source File: AnnotatedMethodTest.java From JAADAS with GNU General Public License v3.0 | 4 votes |
@Override protected void generate(TraceClassVisitor cw) { MethodVisitor mv; AnnotationVisitor av0; cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "soot/asm/backend/targets/AnnotatedMethod", null, "java/lang/Object", null); cw.visitSource("AnnotatedMethod.java", null); { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "doSth", "()V", null, null); { av0 = mv.visitAnnotation("Lsoot/asm/backend/targets/MyTestAnnotation;", true); av0.visit("iVal", new Integer(124)); av0.visit("fVal", new Float("5132.0")); av0.visit("lVal", new Long(5123L)); av0.visit("dVal", new Double("745.0")); av0.visit("zVal", Boolean.TRUE); av0.visit("bVal", new Byte((byte)1)); av0.visit("sVal", new Short((short)123)); av0.visit("strVal", "435243"); av0.visit("rVal", Type.getType("Lsoot/asm/backend/targets/AnnotatedClass;")); av0.visit("iAVal", new int[] {123,234,345,456}); { AnnotationVisitor av1 = av0.visitArray("sAVal"); av1.visit(null, "A"); av1.visit(null, "B"); av1.visit(null, "C"); av1.visitEnd(); } av0.visitEnd(); } mv.visitCode(); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); }
Example 18
Source File: ApplicationFactory.java From ARCHIVE-wildfly-swarm with Apache License 2.0 | 4 votes |
public static byte[] create(String name, String context) { ClassWriter cw = new ClassWriter(0); MethodVisitor mv; AnnotationVisitor av0; cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, name.replace('.', '/'), null, "javax/ws/rs/core/Application", null); int lastDot = name.lastIndexOf('.'); String simpleName = name.substring(lastDot + 1); cw.visitSource(simpleName + ".java", null); { av0 = cw.visitAnnotation("Ljavax/ws/rs/ApplicationPath;", true); av0.visit("value", "/"); av0.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitLineNumber(10, l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "javax/ws/rs/core/Application", "<init>", "()V", false); mv.visitInsn(RETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + name.replace('.', '/') + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); } cw.visitEnd(); return cw.toByteArray(); }
Example 19
Source File: AnnotatedAnnotatedClassTest.java From JAADAS with GNU General Public License v3.0 | 4 votes |
@Override protected void generate(TraceClassVisitor cw) { MethodVisitor mv; AnnotationVisitor av0; cw.visit(V1_5, ACC_PUBLIC + ACC_SUPER, "soot/asm/backend/targets/AnnotatedAnnotatedClass", null, "java/lang/Object", null); cw.visitSource("AnnotatedAnnotatedClass.java", null); { av0 = cw.visitAnnotation("Lsoot/asm/backend/targets/MyAnnotatedAnnotation;", false); { AnnotationVisitor av1 = av0.visitAnnotation("value", "Lsoot/asm/backend/targets/MyTestAnnotation;"); av1.visit("iVal", new Integer(1)); av1.visit("fVal", new Float("1.0")); av1.visit("lVal", new Long(1L)); av1.visit("dVal", new Double("1.0")); av1.visit("zVal", Boolean.TRUE); av1.visit("bVal", new Byte((byte)1)); av1.visit("sVal", new Short((short)1)); av1.visit("strVal", "1"); av1.visit("rVal", Type.getType("Lsoot/asm/backend/targets/AnnotatedClass;")); av1.visit("iAVal", new int[] {1,2,3,4}); { AnnotationVisitor av2 = av1.visitArray("sAVal"); av2.visit(null, "A"); av2.visit(null, "B"); av2.visit(null, "C"); av2.visitEnd(); } av1.visitEnd(); } av0.visitEnd(); } { mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); mv.visitCode(); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitInsn(RETURN); mv.visitMaxs(0, 0); mv.visitEnd(); } cw.visitEnd(); }
Example 20
Source File: ASMClassParser.java From TickDynamic with MIT License | 4 votes |
protected void parseAnnotation(AnnotationVisitor anno, String token) throws Exception { if(token == null || token.length() == 0 || token.length() == 2) return; //Nothing to parse //System.out.println("Parse annotation: " + token); //TODO: Handle escaped quotes properly if(token.contains("\\\"")) throw new Exception(getCurrentTokenLine() + ": Parser currently does not handle escaped quotes in annotations! Bug me about this -_-(Unless you are on an old version"); int offset = 1; //Start after the ( int index; while(true) { String valueName; String value; //Get value name index = token.indexOf("=", offset); valueName = token.substring(offset, index); //Get value offset = index+1; char tokenChar = token.charAt(offset); if(tokenChar == '"') //String value { index = token.indexOf('"', offset+1); value = token.substring(offset+1, index); anno.visit(valueName, value); //System.out.println("AnnotationStr: " + valueName + "=" + value); offset = index+1; } else if(tokenChar == '{') //Array value throw new Exception(getCurrentTokenLine() + ": Parser currently does not handle arrays in annotations!"); else if(tokenChar == 'L') //Enum or Object Type { //Start with getting the Type name index = token.indexOf(";", offset); value = token.substring(offset, index+1); offset = index+1; //If we have a '.' after that, it's an Enum if(token.charAt(offset) == '.') { //Find length int index1 = token.indexOf(",", offset); int index2 = token.indexOf(")", offset); if(index1 < index2 && index1 != -1) index = index1; else index = index2; String entryName = token.substring(offset+1, index); anno.visitEnum(valueName, value, entryName); //System.out.println("AnnotationEnum: " + valueName + "=" + value + "." + entryName); offset = index; } else { anno.visit(valueName, org.objectweb.asm.Type.getType(value)); //System.out.println("AnnotationObj: " + valueName + "=" + value); } } else { //Check for Boolean and Number values index = token.indexOf(",", offset); if(index == -1) value = token.substring(offset, token.length()-1); else value = token.substring(offset, index); ValueType parsedValue = parseValue(value); anno.visit(valueName, parsedValue.value); //System.out.println("AnnotationBoolNr: " + valueName + "=" + parsedValue.value); offset = index; } tokenChar = token.charAt(offset); if(tokenChar == ',') //Continue to next value { offset ++; continue; } else if(tokenChar == ')') //Done break; throw new Exception(getCurrentTokenLine() + ": Error while parsing Annotation: Expected ',' or ')', got: " + tokenChar); } //TODO: If we get "// invisible" before end of line, the annotation is invisible? }