Java Code Examples for org.objectweb.asm.FieldVisitor#visitEnd()

The following examples show how to use org.objectweb.asm.FieldVisitor#visitEnd() . 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: DynamicEntityGenerator.java    From we-cmdb with Apache License 2.0 6 votes vote down vote up
private static void writeField(ClassWriter classWriter, FieldNode field) {
    FieldVisitor fieldVisitor;
    if (field.isJoinNode()) {
        if (EntityRelationship.ManyToOne.equals(field.getEntityRelationship())) {
            fieldVisitor = classWriter.visitField(ACC_PRIVATE, field.getName(), field.getTypeDesc(), null, null);
        } else { // OneToMany or ManyToMany
            fieldVisitor = classWriter.visitField(ACC_PRIVATE, field.getName(), "Ljava/util/Set;", getTypeSiganitureForOneToMany(field.getTypeDesc()), null);
        }
        // ignore join field for json
        AnnotationVisitor annotationVisitor0 = fieldVisitor.visitAnnotation("Lcom/fasterxml/jackson/annotation/JsonIgnore;", true);
        annotationVisitor0.visitEnd();
    } else {
        fieldVisitor = classWriter.visitField(ACC_PRIVATE, field.getName(), typeMap.get(field.getType()), null, null);
    }
    fieldVisitor.visitEnd();
}
 
Example 2
Source File: IntArrayFieldInitializer.java    From bazel with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: MethodOptimizer.java    From JByteMod-Beta with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void visitLdcInsn(Object cst) {
  if (!(cst instanceof Type)) {
    super.visitLdcInsn(cst);
    return;
  }

  // transform Foo.class for 1.2 compatibility
  String ldcName = ((Type) cst).getInternalName();
  String fieldName = "class$" + ldcName.replace('/', '$');
  if (!classOptimizer.syntheticClassFields.contains(ldcName)) {
    classOptimizer.syntheticClassFields.add(ldcName);
    FieldVisitor fv = classOptimizer.syntheticFieldVisitor(ACC_STATIC | ACC_SYNTHETIC, fieldName, "Ljava/lang/Class;");
    fv.visitEnd();
  }

  String clsName = classOptimizer.clsName;
  mv.visitFieldInsn(GETSTATIC, clsName, fieldName, "Ljava/lang/Class;");
}
 
Example 4
Source File: AsmTest.java    From caravan with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
  ClassWriter cw = new ClassWriter(0);
  cw.visit(V1_6, ACC_PUBLIC + ACC_SUPER, "HelloGen", null, "java/lang/Object", null);

  FieldVisitor fv = cw.visitField(1, "_key", Type.getDescriptor(String.class), null, null);
  fv.visitEnd();

  fv = cw.visitField(1, "_value", Type.getDescriptor(InnerClass.class), null, null);
  fv.visitEnd();

  Class<?> clazz = new DynamicClassLoader().defineClass("HelloGen", cw.toByteArray());
  for(Field f : clazz.getDeclaredFields()) {
    System.out.println(f.getName());
  }
}
 
Example 5
Source File: InnerClass2Test.java    From JAADAS with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void generate(TraceClassVisitor cw) {
	MethodVisitor mv;
	FieldVisitor fv;

	cw.visit(V1_1, ACC_SUPER, "soot/asm/backend/targets/InnerClass$1", null, "java/lang/Object",
			new String[] { "soot/asm/backend/targets/Measurable" });
	
	cw.visitSource("InnerClass.java", null);

	cw.visitOuterClass("soot/asm/backend/targets/InnerClass", "doInner", "()V");

	cw.visitInnerClass("soot/asm/backend/targets/InnerClass$1", null, null, 0);

	{
		fv = cw.visitField(ACC_FINAL + ACC_SYNTHETIC, "this$0",
				"Lsoot/asm/backend/targets/InnerClass;", null, null);
		fv.visitEnd();
	}
	{
		mv = cw.visitMethod(0, "<init>", "(Lsoot/asm/backend/targets/InnerClass;)V", null, null);
		mv.visitCode();
		mv.visitVarInsn(ALOAD, 0);
		mv.visitVarInsn(ALOAD, 1);
		mv.visitFieldInsn(PUTFIELD, "soot/asm/backend/targets/InnerClass$1", "this$0",
				"Lsoot/asm/backend/targets/InnerClass;");
		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 6
Source File: GenNerdClass.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	ClassWriter cw = new ClassWriter(0);
	TraceClassVisitor tracer =
			new TraceClassVisitor(cw, new PrintWriter(System.out));
	ClassVisitor cv = tracer;
	String name = "Nerd";
	String generics = null;
	String superName = "java/lang/Object";
	String[] interfaces = null;
	int access = ACC_PUBLIC + ACC_INTERFACE;
	int version = V1_5;
	cv.visit(version, access, name, generics, superName, interfaces);

	int fieldAccess = ACC_PUBLIC + ACC_FINAL + ACC_STATIC;
	String shortDescriptor = Type.SHORT_TYPE.getDescriptor();
	FieldVisitor hair = cv.visitField(fieldAccess, "hair", shortDescriptor,
									  null, new Integer(0));
	hair.visitEnd();

	MethodVisitor playVideoGame =
		cv.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "playVideoGame",
					   "()V", null, null);
	// no code to define, just finish it up
	playVideoGame.visitEnd();
	cv.visitEnd(); // prints if using tracer
	byte[] b = cw.toByteArray();
	// can define or write to file:
	// defineClass(name, b, 0, b.length)
	// from findClass() in subclass of ClassLoader

	FileOutputStream fos = new FileOutputStream("Nerd.class");
	fos.write(b);
	fos.close();
}
 
Example 7
Source File: SerialVersionUIDAdder.java    From JReFrameworker with MIT License 5 votes vote down vote up
/**
 * Adds a final static serialVersionUID field to the class, with the given value.
 *
 * @param svuid the serialVersionUID field value.
 */
// DontCheck(AbbreviationAsWordInName): can't be renamed (for backward binary compatibility).
protected void addSVUID(final long svuid) {
  FieldVisitor fieldVisitor =
      super.visitField(
          Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "serialVersionUID", "J", null, svuid);
  if (fieldVisitor != null) {
    fieldVisitor.visitEnd();
  }
}
 
Example 8
Source File: GenNerdClass.java    From cs652 with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	ClassWriter cw = new ClassWriter(0);
	TraceClassVisitor tracer =
			new TraceClassVisitor(cw, new PrintWriter(System.out));
	ClassVisitor cv = tracer;
	String name = "Nerd";
	String generics = null;
	String superName = "java/lang/Object";
	String[] interfaces = null;
	int access = ACC_PUBLIC + ACC_INTERFACE;
	int version = V1_5;
	cv.visit(version, access, name, generics, superName, interfaces);

	int fieldAccess = ACC_PUBLIC + ACC_FINAL + ACC_STATIC;
	String shortDescriptor = Type.SHORT_TYPE.getDescriptor();
	FieldVisitor hair = cv.visitField(fieldAccess, "hair", shortDescriptor,
									  null, new Integer(0));
	hair.visitEnd();

	MethodVisitor playVideoGame =
		cv.visitMethod(ACC_PUBLIC + ACC_ABSTRACT, "playVideoGame",
					   "()V", null, null);
	// no code to define, just finish it up
	playVideoGame.visitEnd();
	cv.visitEnd(); // prints if using tracer
	byte[] b = cw.toByteArray();
	// can define or write to file:
	// defineClass(name, b, 0, b.length)
	// from findClass() in subclass of ClassLoader

	FileOutputStream fos = new FileOutputStream("Nerd.class");
	fos.write(b);
	fos.close();
}
 
Example 9
Source File: AsmClassGenerator.java    From groovy with Apache License 2.0 5 votes vote down vote up
@Override
public void visitField(final FieldNode fieldNode) {
    onLineNumber(fieldNode, "visitField: " + fieldNode.getName());
    ClassNode t = fieldNode.getType();
    String signature = BytecodeHelper.getGenericsBounds(t);

    Expression initialValueExpression = fieldNode.getInitialValueExpression();
    ConstantExpression cexp = initialValueExpression instanceof ConstantExpression? (ConstantExpression) initialValueExpression :null;
    if (cexp!=null) {
        cexp = Verifier.transformToPrimitiveConstantIfPossible(cexp);
    }
    Object value = cexp != null && ClassHelper.isStaticConstantInitializerType(cexp.getType())
            && cexp.getType().equals(t) && fieldNode.isStatic() && fieldNode.isFinal()
            ? cexp.getValue() : null; // GROOVY-5150
    if (value != null) {
        // byte, char and short require an extra cast
        if (ClassHelper.byte_TYPE.equals(t) || ClassHelper.short_TYPE.equals(t)) {
            value = ((Number) value).intValue();
        } else if (ClassHelper.char_TYPE.equals(t)) {
            value = Integer.valueOf((Character)value);
        }
    }
    FieldVisitor fv = classVisitor.visitField(
            fieldNode.getModifiers(),
            fieldNode.getName(),
            BytecodeHelper.getTypeDescription(t),
            signature,
            value);
    visitAnnotations(fieldNode, fv);
    fv.visitEnd();
}
 
Example 10
Source File: FilterAdapter.java    From HttpSessionReplacer with MIT License 5 votes vote down vote up
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
  super.visit(version, access, name, signature, superName, interfaces);
  abstractClass = (access & ACC_ABSTRACT) != 0;
  filterClass = name;
  superClass = superName;
  FieldVisitor fv = cv.visitField(ACC_PUBLIC, "$$injected_servletContext", "Ljavax/servlet/ServletContext;", null,
      null);
  fv.visitEnd();
  fv = cv.visitField(ACC_PUBLIC, "$$injected_superDoFilter", "Ljava/lang/invoke/MethodHandle;", null, null);
  fv.visitEnd();
  fv = cv.visitField(ACC_PUBLIC, "$$injected_superInit", "Ljava/lang/invoke/MethodHandle;", null, null);
  fv.visitEnd();
}
 
Example 11
Source File: SerialVersionUIDAdder.java    From JByteMod-Beta with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds a final static serialVersionUID field to the class, with the given value.
 *
 * @param svuid the serialVersionUID field value.
 */
protected void addSVUID(final long svuid) {
  FieldVisitor fieldVisitor =
      super.visitField(
          Opcodes.ACC_FINAL + Opcodes.ACC_STATIC, "serialVersionUID", "J", null, svuid);
  if (fieldVisitor != null) {
    fieldVisitor.visitEnd();
  }
}
 
Example 12
Source File: DynamicClassFactory.java    From caravan with Apache License 2.0 5 votes vote down vote up
private Class<?> createClass(ClassPair classPair) {
  String className = generateClassName(classPair);
  String classStr = className.replaceAll("\\.", "/");

  String keyClassType = Type.getDescriptor(classPair.keyClass);
  String valueClassType = Type.getDescriptor(classPair.valueClass);

  ClassWriter cw = new ClassWriter(0);
  cw.visit(V1_7, ACC_PUBLIC + ACC_SUPER, className, null, "java/lang/Object", new String[]{interfaceName});

  AnnotationVisitor anno = cw.visitAnnotation(Type.getDescriptor(JsonPropertyOrder.class), true);
  AnnotationVisitor aa = anno.visitArray("value");
  aa.visit("", "key");
  aa.visit("", "value");
  aa.visitEnd();
  anno.visitEnd();

  FieldVisitor keyField = cw.visitField(ACC_PRIVATE, "key", keyClassType, null, null);
  keyField.visitEnd();

  FieldVisitor valueField = cw.visitField(ACC_PRIVATE, "value", valueClassType, null, null);
  valueField.visitEnd();

  MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
  mv.visitMaxs(2, 1);
  mv.visitVarInsn(ALOAD, 0);
  mv.visitMethodInsn(INVOKESPECIAL, Type.getInternalName(Object.class), "<init>", "()V", false); // call the constructor of super class
  mv.visitInsn(RETURN);
  mv.visitEnd();

  addGetterSetter(classPair, className, classStr, keyClassType, valueClassType, cw);
  addKVPairMethods(classPair, className, classStr, keyClassType, valueClassType, cw);

  cw.visitEnd();

  return defineClass(className, cw.toByteArray());
}
 
Example 13
Source File: MonitorTest.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void generate(TraceClassVisitor cw) {
	FieldVisitor fv;
	MethodVisitor mv;

	cw.visit(V1_1, ACC_PUBLIC + ACC_SUPER, "soot/asm/backend/targets/Monitor", null, "java/lang/Object", null);
	cw.visitSource("Monitor.java", null);

	{
	fv = cw.visitField(0, "o", "Ljava/lang/Object;", null, null);
	fv.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();
	}
	if (targetCompiler == TargetCompiler.eclipse) {
	mv = cw.visitMethod(ACC_PUBLIC, "doSth", "()V", null, null);
	mv.visitCode();
	mv.visitVarInsn(ALOAD, 0);
	mv.visitFieldInsn(GETFIELD, "soot/asm/backend/targets/Monitor", "o", "Ljava/lang/Object;");
	mv.visitInsn(DUP);
	mv.visitInsn(MONITORENTER);
	mv.visitInsn(MONITOREXIT);
	mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
	mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "()V", false);
	mv.visitInsn(RETURN);
	mv.visitMaxs(0, 0);
	mv.visitEnd();
	}
	else {
		mv = cw.visitMethod(ACC_PUBLIC, "doSth", "()V", null, null);
		mv.visitCode();
		Label l0 = new Label();
		Label l1 = new Label();
		Label l2 = new Label();
		mv.visitTryCatchBlock(l0, l1, l2, "java/lang/Throwable");
		Label l3 = new Label();
		mv.visitTryCatchBlock(l2, l3, l2, "java/lang/Throwable");
		mv.visitVarInsn(ALOAD, 0);
		mv.visitFieldInsn(GETFIELD, "soot/asm/backend/targets/Monitor", "o", "Ljava/lang/Object;");
		mv.visitInsn(DUP);
		mv.visitVarInsn(ASTORE, 0);
		mv.visitInsn(MONITORENTER);
		mv.visitLabel(l0);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitInsn(MONITOREXIT);
		mv.visitLabel(l1);
		Label l4 = new Label();
		mv.visitJumpInsn(GOTO, l4);
		mv.visitLabel(l2);
		mv.visitVarInsn(ASTORE, 1);
		mv.visitVarInsn(ALOAD, 0);
		mv.visitInsn(MONITOREXIT);
		mv.visitLabel(l3);
		mv.visitVarInsn(ALOAD, 1);
		mv.visitInsn(ATHROW);
		mv.visitLabel(l4);
		mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
		mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "()V", false);
		mv.visitInsn(RETURN);
		mv.visitMaxs(0, 0);
		mv.visitEnd();
	}
	cw.visitEnd();
	
}
 
Example 14
Source File: RetroWeaver.java    From CodenameOne with GNU General Public License v2.0 4 votes vote down vote up
public void visitEnd() {
    if (isEnum) {
    	cv.visitField(ACC_PRIVATE + ACC_STATIC + ACC_FINAL + ACC_SYNTHETIC,
    			SERIAL_ID_FIELD,
    			SERIAL_ID_SIGNATURE,
    			null, new Long(0L));
    }
    if (!classLiteralCalls.isEmpty()) {
		// generate synthetic fields and class$ method
		for(String fieldName: classLiteralCalls) {
			FieldVisitor fv = visitField(ACC_STATIC + ACC_SYNTHETIC
					+ (isInterface?ACC_PUBLIC:ACC_PRIVATE),
 					fieldName,
 					CLASS_FIELD_DESC,
 					null, null);
			fv.visitEnd();
		}
            if (!isInterface) {
                 // "class$" method
 		MethodVisitor mv = cv.visitMethod(ACC_STATIC+ACC_SYNTHETIC,
                                                        "class$",
                                                        "(Ljava/lang/String;)Ljava/lang/Class;",
                                                        null, null);
	
 		/*mv.visitCode();

                Label beginTry = new Label();
                Label endTry = new Label();
                Label catchBlock = new Label();
                Label finished = new Label();
                mv.visitTryCatchBlock(beginTry, endTry, catchBlock, "java/lang/Exception");
                mv.visitLabel(beginTry);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");        
                mv.visitLabel(endTry);
                mv.visitJumpInsn(GOTO, finished);
                mv.visitLabel(catchBlock);
                mv.visitInsn(POP);
                mv.visitLabel(finished);

                mv.visitInsn(ARETURN);
 
         mv.visitMaxs(0, 0);
         mv.visitEnd();*/
                mv.visitCode();
                Label l0 = new Label();
                Label l1 = new Label();
                Label l2 = new Label();
                mv.visitTryCatchBlock(l0, l1, l2, "java/lang/ClassNotFoundException");
                mv.visitLabel(l0);
                mv.visitVarInsn(ALOAD, 0);
                mv.visitMethodInsn(INVOKESTATIC, "java/lang/Class", "forName", "(Ljava/lang/String;)Ljava/lang/Class;");
                mv.visitLabel(l1);
                mv.visitInsn(ARETURN);
                mv.visitLabel(l2);
                mv.visitVarInsn(ASTORE, 1);
                mv.visitInsn(ACONST_NULL);
                mv.visitInsn(ARETURN);
                /*mv.visitTypeInsn(NEW, "java/lang/NoClassDefFoundError");
                mv.visitInsn(DUP);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/NoClassDefFoundError", "<init>", "()V");
                mv.visitVarInsn(ALOAD, 1);
                mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/NoClassDefFoundError", "initCause", "(Ljava/lang/Throwable;)Ljava/lang/Throwable;");
                mv.visitInsn(ATHROW);*/
                mv.visitMaxs(2, 2);
                mv.visitEnd();

		}                
	}

    if (!stripAttributes) {
    	RetroWeaverAttribute a = new RetroWeaverAttribute(Weaver.getBuildNumber(), originalClassVersion);        
    	cv.visitAttribute(a);
    }

    cv.visitEnd();
}
 
Example 15
Source File: AsmTest.java    From atlas with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Throwable {

    ClassReader cr = new ClassReader(Config.class.getName());
    ClassWriter cw = new ClassWriter(cr, ClassWriter.COMPUTE_MAXS);
    ClassVisitor cv = new MethodChangeClassAdapter(cw);
    cr.accept(cv, Opcodes.ASM5);

    //Add a new method
    MethodVisitor mw = cw.visitMethod(ACC_PUBLIC + ACC_STATIC,
                                      "add",
                                      "([Ljava/lang/String;)V",
                                      null,
                                      null);

    // pushes the 'out' field (of type PrintStream) of the System class
    mw.visitFieldInsn(GETSTATIC,
                      "java/lang/System",
                      "out",
                      "Ljava/io/PrintStream;");
    // pushes the "Hello World!" String constant
    mw.visitLdcInsn("this is add method print!");
    // invokes the 'println' method (defined in the PrintStream class)
    mw.visitMethodInsn(INVOKEVIRTUAL,
                       "java/io/PrintStream",
                       "println",
                       "(Ljava/lang/String;)V");
    mw.visitInsn(RETURN);
    // this code uses a maximum of two stack elements and two local
    // variables
    mw.visitMaxs(0, 0);
    mw.visitEnd();


    //Type.getDescriptor(AdviceFlowOuterHolder.class)
    FieldVisitor fv = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
                                     "age",
                                    Type.INT_TYPE.toString(),
                                     null,
                                     1);
    fv.visitEnd();

    FieldVisitor fv2 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
                                    "name2",
                                    Type.getDescriptor(String.class),
                                    null,
                                    "name2");
    fv2.visitEnd();


    ModifyClassVisiter cv2 = new ModifyClassVisiter(Opcodes.ASM5);
    cv2.addRemoveField("name");
    cr.accept(cv2, Opcodes.ASM5);

    FieldVisitor fv3 = cw.visitField(ACC_PUBLIC + ACC_STATIC + ACC_FINAL,
                                     "name",
                                     Type.getDescriptor(String.class),
                                     null,
                                     "name");
    fv3.visitEnd();




    byte[] code = cw.toByteArray();
    File file = new File("Config.class");
    System.out.println(file.getAbsolutePath());
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(code);
    fos.close();

}
 
Example 16
Source File: ASMClassWriter.java    From gravel with Apache License 2.0 4 votes vote down vote up
private void createField(JVMField field) {
	FieldVisitor fv = cw.visitField(ACC_PUBLIC | (field.isStatic() ? ACC_STATIC : 0), field.varName(), field
			.type().descriptorString(), null, null);
	fv.visitEnd();
}
 
Example 17
Source File: ModifiersTest.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void generate(TraceClassVisitor cw) {
	MethodVisitor mv;
	FieldVisitor fv;
	

	cw.visit(V1_3, ACC_PUBLIC + ACC_SUPER + ACC_ABSTRACT, "soot/asm/backend/targets/Modifiers", null, "java/lang/Object", null);
	cw.visitSource("Modifiers.java", null);

	{
	fv = cw.visitField(ACC_PRIVATE + ACC_VOLATILE, "i", "I", null, null);
	fv.visitEnd();
	}
	{
	fv = cw.visitField(ACC_PRIVATE + ACC_FINAL, "j", "I", null, new Integer(213));
	fv.visitEnd();
	}
	{
	fv = cw.visitField(ACC_PRIVATE + ACC_TRANSIENT, "k", "I", null, null);
	fv.visitEnd();
	}
	{
	mv = cw.visitMethod(ACC_PUBLIC + ACC_STRICT, "<init>", "()V", null, null);
	mv.visitCode();
	mv.visitVarInsn(ALOAD, 0);
	mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
	mv.visitVarInsn(ALOAD, 0);
	mv.visitIntInsn(SIPUSH, 213);
	mv.visitFieldInsn(PUTFIELD, "soot/asm/backend/targets/Modifiers", "j", "I");
	mv.visitInsn(RETURN);
	mv.visitMaxs(0, 0);
	mv.visitEnd();
	}
	{
	mv = cw.visitMethod(ACC_PUBLIC + ACC_FINAL + ACC_STRICT, "a", "()V", null, null);
	mv.visitCode();
	mv.visitInsn(RETURN);
	mv.visitMaxs(0, 0);
	mv.visitEnd();
	}
	{
	mv = cw.visitMethod(ACC_PUBLIC + ACC_SYNCHRONIZED + ACC_STRICT, "b", "()V", null, null);
	mv.visitCode();
	mv.visitInsn(RETURN);
	mv.visitMaxs(0, 0);
	mv.visitEnd();
	}
	{
	mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC + ACC_STRICT, "c", "()V", null, null);
	mv.visitCode();
	mv.visitInsn(RETURN);
	mv.visitMaxs(0, 0);
	mv.visitEnd();
	}
	{
	mv = cw.visitMethod(ACC_STRICT, "d", "()V", null, null);
	mv.visitCode();
	mv.visitInsn(RETURN);
	mv.visitMaxs(0, 0);
	mv.visitEnd();
	}
	{
	mv = cw.visitMethod(ACC_PROTECTED + ACC_STRICT, "e", "()V", null, null);
	mv.visitCode();
	mv.visitInsn(RETURN);
	mv.visitMaxs(0, 0);
	mv.visitEnd();
	}
	{
	mv = cw.visitMethod(ACC_ABSTRACT, "f", "()V", null, null);
	mv.visitEnd();
	}
	{
		if (targetCompiler == TargetCompiler.eclipse)
			mv = cw.visitMethod(ACC_PRIVATE + ACC_NATIVE, "g", "()V", null, null);
		else
			mv = cw.visitMethod(ACC_PRIVATE + ACC_NATIVE + ACC_STRICT, "g", "()V", null, null);
	mv.visitEnd();
	}
	cw.visitEnd();


}
 
Example 18
Source File: TestEnumForEclipseCompilerDump.java    From AVM with MIT License 4 votes vote down vote up
public static byte[] generateBytecode() {
    ClassWriter classWriter = new ClassWriter(0);
    FieldVisitor fieldVisitor;
    MethodVisitor methodVisitor;

    classWriter.visit(V10, ACC_PUBLIC | ACC_FINAL | ACC_SUPER | ACC_ENUM, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "Ljava/lang/Enum<L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;>;", "java/lang/Enum", null);

    {
        fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC | ACC_ENUM, "TEST", "L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null);
        fieldVisitor.visitEnd();
    }
    {
        fieldVisitor = classWriter.visitField(ACC_PRIVATE | ACC_FINAL | ACC_STATIC | ACC_SYNTHETIC, "ENUM$VALUES", "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null);
        fieldVisitor.visitEnd();
    }
    {
        methodVisitor = classWriter.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
        methodVisitor.visitCode();
        methodVisitor.visitTypeInsn(NEW, PACKAGE_NAME_INTERNAL + "/TestEnumForValues");
        methodVisitor.visitInsn(DUP);
        methodVisitor.visitLdcInsn("TEST");
        methodVisitor.visitInsn(ICONST_0);
        methodVisitor.visitMethodInsn(INVOKESPECIAL, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "<init>", "(Ljava/lang/String;I)V", false);
        methodVisitor.visitFieldInsn(PUTSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "TEST", "L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;");
        methodVisitor.visitInsn(ICONST_1);
        methodVisitor.visitTypeInsn(ANEWARRAY, PACKAGE_NAME_INTERNAL + "/TestEnumForValues");
        methodVisitor.visitInsn(DUP);
        methodVisitor.visitInsn(ICONST_0);
        methodVisitor.visitFieldInsn(GETSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "TEST", "L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;");
        methodVisitor.visitInsn(AASTORE);
        methodVisitor.visitFieldInsn(PUTSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "ENUM$VALUES", "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;");
        methodVisitor.visitInsn(RETURN);
        methodVisitor.visitMaxs(4, 0);
        methodVisitor.visitEnd();
    }
    {
        methodVisitor = classWriter.visitMethod(ACC_PRIVATE, "<init>", "(Ljava/lang/String;I)V", null, null);
        methodVisitor.visitCode();
        methodVisitor.visitVarInsn(ALOAD, 0);
        methodVisitor.visitVarInsn(ALOAD, 1);
        methodVisitor.visitVarInsn(ILOAD, 2);
        methodVisitor.visitMethodInsn(INVOKESPECIAL, "java/lang/Enum", "<init>", "(Ljava/lang/String;I)V", false);
        methodVisitor.visitInsn(RETURN);
        methodVisitor.visitMaxs(3, 3);
        methodVisitor.visitEnd();
    }
    {
        methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "values", "()[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null);
        methodVisitor.visitCode();
        methodVisitor.visitFieldInsn(GETSTATIC, PACKAGE_NAME_INTERNAL + "/TestEnumForValues", "ENUM$VALUES", "[L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;");
        methodVisitor.visitInsn(DUP);
        methodVisitor.visitVarInsn(ASTORE, 0);
        methodVisitor.visitInsn(ICONST_0);
        methodVisitor.visitVarInsn(ALOAD, 0);
        methodVisitor.visitInsn(ARRAYLENGTH);
        methodVisitor.visitInsn(DUP);
        methodVisitor.visitVarInsn(ISTORE, 1);
        methodVisitor.visitTypeInsn(ANEWARRAY, PACKAGE_NAME_INTERNAL + "/TestEnumForValues");
        methodVisitor.visitInsn(DUP);
        methodVisitor.visitVarInsn(ASTORE, 2);
        methodVisitor.visitInsn(ICONST_0);
        methodVisitor.visitVarInsn(ILOAD, 1);
        methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/System", "arraycopy", "(Ljava/lang/Object;ILjava/lang/Object;II)V", false);
        methodVisitor.visitVarInsn(ALOAD, 2);
        methodVisitor.visitInsn(ARETURN);
        methodVisitor.visitMaxs(5, 3);
        methodVisitor.visitEnd();
    }
    {
        methodVisitor = classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "valueOf", "(Ljava/lang/String;)L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;", null, null);
        methodVisitor.visitCode();
        methodVisitor.visitLdcInsn(Type.getType("L" + PACKAGE_NAME_INTERNAL + "/TestEnumForValues;"));
        methodVisitor.visitVarInsn(ALOAD, 0);
        methodVisitor.visitMethodInsn(INVOKESTATIC, "java/lang/Enum", "valueOf", "(Ljava/lang/Class;Ljava/lang/String;)Ljava/lang/Enum;", false);
        methodVisitor.visitTypeInsn(CHECKCAST, PACKAGE_NAME_INTERNAL + "/TestEnumForValues");
        methodVisitor.visitInsn(ARETURN);
        methodVisitor.visitMaxs(2, 1);
        methodVisitor.visitEnd();
    }
    classWriter.visitEnd();

    return classWriter.toByteArray();
}
 
Example 19
Source File: AnnotatedFieldTest.java    From JAADAS with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected void generate(TraceClassVisitor cw) {

	FieldVisitor fv;
	MethodVisitor mv;
	AnnotationVisitor av0;

	cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "soot/asm/backend/targets/AnnotatedField",
			null, "java/lang/Object", null);
	cw.visitSource("AnnotatedField.java", null);
	
	{
	fv = cw.visitField(0, "a", "Ljava/lang/String;", null, null);
	{
	av0 = fv.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();
	}
	fv.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: Globalizer.java    From Concurnas with MIT License 4 votes vote down vote up
public void visitEnd() {
  	if(!visitedClinit   ){//didnt have a clinit, so create a simple init and getter now...
  		if(!this.staticFinalVars.isEmpty()) {
  			addInitforStaticFinalVars();
  			createInializerAndSupportMethods(true);
  		}else {
   		createInializerAndSupportMethods(false);
  		}
  		//TODO: this code is only really useful in case where u do this class X{ fromouter = 99 class Y{ y = 1 + fromouter }} //since fromouter is from psar which is static...
  		addEmptyInit();
  	}
  	
  	
if(!shareInitsToMake.isEmpty()) {

	String origClassName = this.fromName;//this.className.peek();
	String globalClass = this.globName;//this.origClassClinitToNewOne.get(origClassName);
	
	for(String sharedinit : shareInitsToMake) {
		String initOnceVarName = sharedinit + "$onceVar";
		{
			FieldVisitor fv = super.visitField(ACC_PRIVATE + ACC_VOLATILE + ACC_STATIC, initOnceVarName, "Ljava/lang/Object;", null, null);
		    fv.visitEnd();
		}
		
		{
			MethodVisitor mv = super.visitMethod(ACC_PUBLIC, sharedinit, "()V", null, null);
			mv.visitCode();
			Label l0 = new Label();
			Label l1 = new Label();
			Label l2 = new Label();
			mv.visitTryCatchBlock(l0, l1, l2, null);
			Label l3 = new Label();
			mv.visitTryCatchBlock(l2, l3, l2, null);
			Label l4 = new Label();
			mv.visitLabel(l4);
			mv.visitFieldInsn(GETSTATIC, globalClass, initOnceVarName, "Ljava/lang/Object;");
			Label l5 = new Label();
			mv.visitJumpInsn(IFNONNULL, l5);
			Label l6 = new Label();
			mv.visitLabel(l6);
			mv.visitVarInsn(ALOAD, 0);
			mv.visitInsn(DUP);
			mv.visitVarInsn(ASTORE, 1);
			mv.visitInsn(MONITORENTER);
			mv.visitLabel(l0);
			mv.visitFieldInsn(GETSTATIC, globalClass, initOnceVarName, "Ljava/lang/Object;");
			Label l7 = new Label();
			mv.visitJumpInsn(IFNONNULL, l7);
			Label l8 = new Label();
			mv.visitLabel(l8);
			mv.visitIntInsn(BIPUSH, 111);
			mv.visitMethodInsn(INVOKESTATIC, "java/lang/Integer", "valueOf", "(I)Ljava/lang/Integer;", false);
			mv.visitFieldInsn(PUTSTATIC, globalClass, initOnceVarName, "Ljava/lang/Object;");
			Label l9 = new Label();
			mv.visitLabel(l9);
			mv.visitMethodInsn(INVOKESTATIC, origClassName, sharedinit, "()V", false);//this is called only once
			mv.visitLabel(l7);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitInsn(MONITOREXIT);
			mv.visitLabel(l1);
			mv.visitJumpInsn(GOTO, l5);
			mv.visitLabel(l2);
			mv.visitVarInsn(ALOAD, 1);
			mv.visitInsn(MONITOREXIT);
			mv.visitLabel(l3);
			mv.visitInsn(ATHROW);
			mv.visitLabel(l5);
			mv.visitInsn(RETURN);
			mv.visitMaxs(2, 2);
			mv.visitEnd();
		}
	}
}
  	
  	super.visitEnd();
  }