List of usage examples for org.objectweb.asm MethodVisitor visitAnnotation
public AnnotationVisitor visitAnnotation(final String descriptor, final boolean visible)
From source file:com.googlecode.ddom.weaver.asm.MethodVisitorTee.java
License:Apache License
public AnnotationVisitor visitAnnotation(String desc, boolean visible) { AnnotationVisitor result = null;/* w ww . jav a2 s . c o m*/ for (MethodVisitor visitor : visitors) { result = AnnotationVisitorTee.mergeVisitors(result, visitor.visitAnnotation(desc, visible)); } return result; }
From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java
License:Open Source License
protected void generatePropertyMethod(PropertyInfo property) { MethodVisitor mv = cv.visitMethod(property.writeAccessLevel + ACC_FINAL, property.propertyMethodName, "()" + property.propertyInterfaceSignature, property.propertyInterfaceSignatureGeneric != null ? "()" + property.propertyInterfaceSignatureGeneric : null,/*from w w w .jav a2 s . c o m*/ null); { AnnotationVisitor av0 = mv.visitAnnotation("L" + Property.class.getName().replace('.', '/') + ";", true); av0.visit("writeable", Boolean.TRUE); av0.visit("name", property.propertyName); av0.visit("dataSignature", property.dataSignature); av0.visit("dataSignatureGeneric", property.dataSignatureGeneric != null ? property.dataSignatureGeneric : ""); av0.visit("humanReadableName", property.humanReadablePropertyName != null ? property.humanReadablePropertyName : ""); av0.visitEnd(); } mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature); Label l1 = new Label(); mv.visitJumpInsn(IFNONNULL, l1); Label l2 = new Label(); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, property.propertyDataType); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn(property.propertyName); mv.visitMethodInsn(INVOKESPECIAL, property.propertyDataType, "<init>", "(Ljava/lang/Object;Ljava/lang/String;)V"); mv.visitFieldInsn(PUTFIELD, className, property.propertyFieldName, property.propertyDataSignature); mv.visitLabel(l1); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature); mv.visitTypeInsn(CHECKCAST, property.propertyInterfaceType); mv.visitInsn(ARETURN); Label l3 = new Label(); mv.visitLabel(l3); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l3, 0); mv.visitMaxs(5, 1); mv.visitEnd(); }
From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java
License:Open Source License
protected void generateReadOnlyPropertyMethod(PropertyInfo property) { MethodVisitor mv = cv.visitMethod(property.readAccessLevel + ACC_FINAL, property.readOnlyPropertyMethodName, "()" + property.readOnlyPropertyInterfaceSignature, property.readOnlyPropertyInterfaceSignatureGeneric != null ? "()" + property.readOnlyPropertyInterfaceSignatureGeneric : null,/* w w w. j a va 2 s. co m*/ null); { AnnotationVisitor av0 = mv.visitAnnotation("L" + Property.class.getName().replace('.', '/') + ";", true); av0.visit("writeable", Boolean.FALSE); av0.visit("name", property.propertyName); av0.visit("dataSignature", property.dataSignature); av0.visit("dataSignatureGeneric", property.dataSignatureGeneric != null ? property.dataSignatureGeneric : ""); av0.visit("humanReadableName", property.humanReadablePropertyName != null ? property.humanReadablePropertyName : ""); av0.visitEnd(); } mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature); Label l1 = new Label(); mv.visitJumpInsn(IFNONNULL, l1); Label l2 = new Label(); mv.visitLabel(l2); mv.visitVarInsn(ALOAD, 0); mv.visitTypeInsn(NEW, property.propertyDataType); mv.visitInsn(DUP); mv.visitVarInsn(ALOAD, 0); mv.visitLdcInsn(property.propertyName); mv.visitMethodInsn(INVOKESPECIAL, property.propertyDataType, "<init>", "(Ljava/lang/Object;Ljava/lang/String;)V"); mv.visitFieldInsn(PUTFIELD, className, property.propertyFieldName, property.propertyDataSignature); mv.visitLabel(l1); mv.visitFrame(F_SAME, 0, null, 0, null); mv.visitVarInsn(ALOAD, 0); mv.visitFieldInsn(GETFIELD, className, property.propertyFieldName, property.propertyDataSignature); mv.visitMethodInsn(INVOKEVIRTUAL, property.propertyDataType, "getReadOnlyProperty", "()" + property.readOnlyPropertyInterfaceSignature); mv.visitTypeInsn(CHECKCAST, property.readOnlyPropertyInterfaceType); mv.visitInsn(ARETURN); Label l3 = new Label(); mv.visitLabel(l3); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l3, 0); mv.visitMaxs(5, 1); mv.visitEnd(); }
From source file:de.chimos.property.compiler.passtwo.javafx.PassTwoJFXClassVisitor.java
License:Open Source License
protected void generateGetterMethod(PropertyInfo property) { MethodVisitor mv = cv.visitMethod(property.readAccessLevel, property.getterMethodName, "()" + property.dataSignature, property.dataSignatureGeneric != null ? "()" + property.dataSignatureGeneric : null, null); if (property.enableSerialization == true && config.serialization == Config.Serialization.JAXB) { AnnotationVisitor av0 = mv.visitAnnotation("Ljavax/xml/bind/annotation/XmlElement;", true); av0.visitEnd();//from www . j a v a 2 s . c o m } mv.visitCode(); Label l0 = new Label(); mv.visitLabel(l0); mv.visitVarInsn(ALOAD, 0); mv.visitMethodInsn(INVOKESPECIAL, className, property.internalGetterMethodName, "()" + property.dataSignature); mv.visitInsn(ARETURN); Label l1 = new Label(); mv.visitLabel(l1); mv.visitLocalVariable("this", "L" + className + ";", null, l0, l1, 0); mv.visitMaxs(1, 1); mv.visitEnd(); }
From source file:egovframework.rte.itl.webservice.service.impl.EgovWebServiceClassLoaderImpl.java
License:Apache License
private byte[] createServiceEndpointInterfaceClass( final ServiceEndpointInterfaceInfo serviceEndpointInterfaceInfo) throws ClassNotFoundException { // CHECKSTYLE:OFF String serviceEndpointInterfaceClassName = getServiceEndpointInterfaceClassName( serviceEndpointInterfaceInfo.getServiceName()); String asmServiceEndpointInterfaceClassName = serviceEndpointInterfaceClassName.replace('.', '/'); // CHECKSTYLE:ON // ClassWriter classWriter = new ClassWriter(false); ClassWriter classWriter = new ClassWriter(0); classWriter.visit(V1_5, // version ACC_PUBLIC | ACC_ABSTRACT | ACC_INTERFACE, // access asmServiceEndpointInterfaceClassName, // name null, // signature "java/lang/Object", // superName null); // interfaces // Create Annotation AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(DESC_OF_WEB_SERVICE, true); annotationVisitor.visit("targetNamespace", serviceEndpointInterfaceInfo.getNamespace()); annotationVisitor.visitEnd();//from w w w . jav a2s . c o m annotationVisitor = classWriter.visitAnnotation(DESC_OF_SOAP_BINDING, true); annotationVisitor.visitEnum("parameterStyle", DESC_OF_SOAP_BINDING_PARAMETER_STYLE, "BARE"); // Create Method ServiceParamInfo returnInfo = serviceEndpointInterfaceInfo.getReturnInfo(); Collection<ServiceParamInfo> paramInfos = serviceEndpointInterfaceInfo.getParamInfos(); StringBuffer desc = new StringBuffer("("); StringBuffer signature = new StringBuffer("("); for (ServiceParamInfo info : paramInfos) { Class<?> paramClass = loadClass(info.getType()); org.objectweb.asm.Type paramType = org.objectweb.asm.Type.getType(paramClass); String paramSign = paramType.getDescriptor(); if (info.getMode().equals(OUT) || info.getMode().equals(INOUT)) { if (paramClass.isPrimitive()) { paramClass = wrapperClasses.get(paramClass); paramType = org.objectweb.asm.Type.getType(paramClass); paramSign = paramType.getDescriptor(); } paramClass = Holder.class; paramType = TYPE_OF_HOLDER; paramSign = "Ljavax/xml/ws/Holder<" + paramSign + ">;"; } desc.append(paramType.getDescriptor()); signature.append(paramSign); } desc.append(")"); signature.append(")"); // CHECKSTYLE:OFF org.objectweb.asm.Type returnType = (returnInfo == null ? returnType = org.objectweb.asm.Type.VOID_TYPE : org.objectweb.asm.Type.getType(loadClass(returnInfo.getType()))); // CHECKSTYLE:ON desc.append(returnType.getDescriptor()); signature.append(returnType.getDescriptor()); MethodVisitor methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_ABSTRACT, // access serviceEndpointInterfaceInfo.getOperationName(), // name desc.toString(), // desc signature.toString(), // signature null); // exceptions // @WebMethod annotationVisitor = methodVisitor.visitAnnotation(DESC_OF_WEB_METHOD, true); annotationVisitor.visit("operationName", serviceEndpointInterfaceInfo.getOperationName()); annotationVisitor.visitEnd(); // @WebResult if (returnInfo != null) { annotationVisitor = methodVisitor.visitAnnotation(DESC_OF_WEB_RESULT, true); annotationVisitor.visit("name", returnInfo.getName()); // annotationVisitor.visit("partName", // returnInfo.getName()); annotationVisitor.visit("header", returnInfo.isHeader()); annotationVisitor.visit("targetNamespace", serviceEndpointInterfaceInfo.getNamespace()); annotationVisitor.visitEnd(); } // @WebParam int index = 0; for (ServiceParamInfo info : serviceEndpointInterfaceInfo.getParamInfos()) { annotationVisitor = methodVisitor.visitParameterAnnotation(index, DESC_OF_WEB_PARAM, true); annotationVisitor.visit("name", info.getName()); // annotationVisitor.visit("partName", // info.getName()); annotationVisitor.visitEnum("mode", DESC_OF_WEB_PARAM_MODE, info.getMode().toString()); annotationVisitor.visit("header", info.isHeader()); annotationVisitor.visit("targetNamespace", serviceEndpointInterfaceInfo.getNamespace()); annotationVisitor.visitEnd(); index++; } methodVisitor.visitEnd(); // Class finalize classWriter.visitEnd(); // try // { // DataOutputStream dos = new DataOutputStream( // new FileOutputStream("EgovType" + // serviceInfo.getServiceName() + ".class")); // dos.write(classWriter.toByteArray()); // dos.close(); // } // catch (IOException e) // { // e.printStackTrace(); // } return classWriter.toByteArray(); }
From source file:kilim.analysis.MethodFlow.java
License:Open Source License
@Override /**// ww w.j av a 2s. c o m * Copied verbatim from MethodNode except for the instruction processing. * Unlike MethodNode, we don't keep LabelNodes inline, so we need to * do visitLabel ourselves. * * @param mv a method visitor. */ public void accept(final MethodVisitor mv) { // visits the method attributes int i, j, n; if (annotationDefault != null) { AnnotationVisitor av = mv.visitAnnotationDefault(); acceptAnnotation(av, null, annotationDefault); av.visitEnd(); } n = visibleAnnotations == null ? 0 : visibleAnnotations.size(); for (i = 0; i < n; ++i) { AnnotationNode an = (AnnotationNode) visibleAnnotations.get(i); an.accept(mv.visitAnnotation(an.desc, true)); } n = invisibleAnnotations == null ? 0 : invisibleAnnotations.size(); for (i = 0; i < n; ++i) { AnnotationNode an = (AnnotationNode) invisibleAnnotations.get(i); an.accept(mv.visitAnnotation(an.desc, false)); } n = visibleParameterAnnotations == null ? 0 : visibleParameterAnnotations.length; for (i = 0; i < n; ++i) { List<?> l = visibleParameterAnnotations[i]; if (l == null) { continue; } for (j = 0; j < l.size(); ++j) { AnnotationNode an = (AnnotationNode) l.get(j); an.accept(mv.visitParameterAnnotation(i, an.desc, true)); } } n = invisibleParameterAnnotations == null ? 0 : invisibleParameterAnnotations.length; for (i = 0; i < n; ++i) { List<?> l = invisibleParameterAnnotations[i]; if (l == null) { continue; } for (j = 0; j < l.size(); ++j) { AnnotationNode an = (AnnotationNode) l.get(j); an.accept(mv.visitParameterAnnotation(i, an.desc, false)); } } n = attrs == null ? 0 : attrs.size(); for (i = 0; i < n; ++i) { mv.visitAttribute((Attribute) attrs.get(i)); } // visits the method's code if (instructions.size() > 0) { mv.visitCode(); // visits try catch blocks for (i = 0; i < tryCatchBlocks.size(); ++i) { ((TryCatchBlockNode) tryCatchBlocks.get(i)).accept(mv); } // visits instructions for (i = 0; i < instructions.size(); ++i) { Label l = getLabelAt(i); if (l != null) { mv.visitLabel(l); } ((AbstractInsnNode) instructions.get(i)).accept(mv); } Label l = getLabelAt(instructions.size()); if (l != null) { mv.visitLabel(l); } // visits local variables n = localVariables == null ? 0 : localVariables.size(); for (i = 0; i < n; ++i) { ((LocalVariableNode) localVariables.get(i)).accept(mv); } // visits line numbers /* TODO this was in ASM 2.3.3 but not 3.x or 4.0, find a substitute or remove n = lineNumbers == null ? 0 : lineNumbers.size(); for (i = 0; i < n; ++i) { ((LineNumberNode) lineNumbers.get(i)).accept(mv); } */ // visits maxs mv.visitMaxs(maxStack, maxLocals); } mv.visitEnd(); }
From source file:kilim.analysis.MethodWeaver.java
License:Open Source License
private void visitAttrs(MethodVisitor mv) { MethodFlow mf = methodFlow;//from w w w.j a v a2 s . com // visits the method attributes int i, j, n; if (mf.annotationDefault != null) { AnnotationVisitor av = mv.visitAnnotationDefault(); MethodFlow.acceptAnnotation(av, null, mf.annotationDefault); av.visitEnd(); } n = mf.visibleAnnotations == null ? 0 : mf.visibleAnnotations.size(); for (i = 0; i < n; ++i) { AnnotationNode an = (AnnotationNode) mf.visibleAnnotations.get(i); an.accept(mv.visitAnnotation(an.desc, true)); } n = mf.invisibleAnnotations == null ? 0 : mf.invisibleAnnotations.size(); for (i = 0; i < n; ++i) { AnnotationNode an = (AnnotationNode) mf.invisibleAnnotations.get(i); an.accept(mv.visitAnnotation(an.desc, false)); } n = mf.visibleParameterAnnotations == null ? 0 : mf.visibleParameterAnnotations.length; for (i = 0; i < n; ++i) { List<?> l = mf.visibleParameterAnnotations[i]; if (l == null) { continue; } for (j = 0; j < l.size(); ++j) { AnnotationNode an = (AnnotationNode) l.get(j); an.accept(mv.visitParameterAnnotation(i, an.desc, true)); } } n = mf.invisibleParameterAnnotations == null ? 0 : mf.invisibleParameterAnnotations.length; for (i = 0; i < n; ++i) { List<?> l = mf.invisibleParameterAnnotations[i]; if (l == null) { continue; } for (j = 0; j < l.size(); ++j) { AnnotationNode an = (AnnotationNode) l.get(j); an.accept(mv.visitParameterAnnotation(i, an.desc, false)); } } n = mf.attrs == null ? 0 : mf.attrs.size(); for (i = 0; i < n; ++i) { mv.visitAttribute((Attribute) mf.attrs.get(i)); } }
From source file:org.adjective.stout.writer.ByteCodeWriter.java
License:Apache License
private void writeMethod(ClassVisitor cv, ClassDescriptor cls, MethodDescriptor method) { String[] exceptions = new String[method.getExceptions().length]; for (int i = 0; i < exceptions.length; i++) { exceptions[i] = getInternalName(method.getExceptions()[i]); }// w w w . j av a2s .com String signature = null; // @TODO final MethodVisitor mv = visitMethod(cv, method, exceptions, signature); for (AnnotationDescriptor annotation : method.getAnnotations()) { AnnotationVisitor av = mv.visitAnnotation(Type.getDescriptor(annotation.getType()), annotation.isRuntime()); processAnnotation(annotation, av); } Parameter[] parameters = method.getParameters(); for (int i = 0; i < parameters.length; i++) { Parameter parameter = parameters[i]; AnnotationDescriptor[] annotations = parameter.getAnnotations(); for (AnnotationDescriptor annotation : annotations) { AnnotationVisitor av = mv.visitParameterAnnotation(i, Type.getDescriptor(annotation.getType()), annotation.isRuntime()); processAnnotation(annotation, av); } } Code body = method.getBody(); if (method.getModifiers().contains(ElementModifier.ABSTRACT)) { if (body != null) { throw new IllegalStateException("The abstract method " + method + " cannot have a body"); } } else { if (body == null) { throw new IllegalStateException( "The method " + method + " is not abstract, but does not have a body"); } mv.visitCode(); CodeStack stack = new CodeStack(cls, method, mv); Block block = stack.pushBlock(); if (!isStatic(method)) { block.declareVariable(new Variable("#this", cls)); } for (Parameter parameter : parameters) { block.declareVariable(new Variable(parameter.getName(), parameter.getType())); } InstructionCollector collector = new AbstractInstructionCollector() { private int _line; public void add(Instruction instruction, int line) { if (line != 0 && line != _line) { _line = line; Label label = new Label(); mv.visitLabel(label); mv.visitLineNumber(line, label); } instruction.accept(mv); } }; try { body.getInstructions(stack, collector); } catch (StoutException e) { throw new WriterException("In class " + cls.getPackage() + "." + cls.getName() + ", cannot write method " + method.getName(), e); } stack.popBlock(block); stack.declareVariableInfo(); } mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.apache.felix.ipojo.manipulation.ClassCheckerTestCase.java
License:Apache License
private void assertAnnotationIsAlone(MethodDescriptor method, String desc) { List<ClassChecker.AnnotationDescriptor> annotations = method.getAnnotations(); assertEquals(1, annotations.size()); ClassChecker.AnnotationDescriptor annotationDescriptor = annotations.get(0); MethodVisitor mv = mock(MethodVisitor.class); when(mv.visitAnnotation(desc, true)).thenReturn(new AnnotationVisitor(Opcodes.ASM5) { });/*from w w w . j a v a 2s .c om*/ annotationDescriptor.visitAnnotation(mv); }
From source file:org.apache.felix.ipojo.manipulator.metadata.annotation.model.literal.AnnotationPlayback.java
License:Apache License
public void accept(final MethodVisitor visitor) { AnnotationVisitor av = visitor.visitAnnotation(m_annotationType.getDescriptor(), true); if (av != null) { accept(av);/*w w w . j a v a 2 s . c o m*/ } }