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

The following examples show how to use org.objectweb.asm.FieldVisitor#visitAnnotation() . 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: IntFieldInitializer.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, 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 4
Source File: FieldCreatorImpl.java    From gizmo with Apache License 2.0 5 votes vote down vote up
@Override
public void write(ClassVisitor file) {
    FieldVisitor fieldVisitor = file.visitField(modifiers, fieldDescriptor.getName(), fieldDescriptor.getType(), signature, null);
    for(AnnotationCreatorImpl annotation : annotations) {
        AnnotationVisitor av = fieldVisitor.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();
    }
    fieldVisitor.visitEnd();
}
 
Example 5
Source File: Field.java    From OSRS-Deobfuscator with BSD 2-Clause "Simplified" License 5 votes vote down vote up
public void accept(FieldVisitor visitor)
{
	for (Annotation annotation : annotations.getAnnotations())
	{
		AnnotationVisitor av = visitor.visitAnnotation(annotation.getType().toString(), true);
		annotation.accept(av);
	}

	visitor.visitEnd();
}
 
Example 6
Source File: ClassReaderTest.java    From turbine with Apache License 2.0 4 votes vote down vote up
@Test
public void fields() {
  ClassWriter cw = new ClassWriter(0);
  cw.visit(
      52,
      Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER,
      "test/Hello",
      "<X:Ljava/lang/Object;>Ljava/lang/Object;",
      "java/lang/Object",
      null);
  FieldVisitor fv = cw.visitField(Opcodes.ACC_PUBLIC, "x", "I", null, null);
  fv.visitAnnotation("Ljava/lang/Deprecated;", true);
  cw.visitField(
      Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC,
      "y",
      "I",
      null,
      Integer.valueOf(42));
  cw.visitField(Opcodes.ACC_PUBLIC, "z", "Ljava/util/List;", "Ljava/util/List<TX;>;", null);
  cw.visitEnd();
  byte[] bytes = cw.toByteArray();

  ClassFile classFile = com.google.turbine.bytecode.ClassReader.read(null, bytes);

  assertThat(classFile.fields()).hasSize(3);

  ClassFile.FieldInfo x = classFile.fields().get(0);
  assertThat(x.access()).isEqualTo(TurbineFlag.ACC_PUBLIC);
  assertThat(x.name()).isEqualTo("x");
  assertThat(x.descriptor()).isEqualTo("I");
  assertThat(x.signature()).isNull();
  assertThat(x.value()).isNull();
  assertThat(x.annotations()).hasSize(1);
  ClassFile.AnnotationInfo annotation = Iterables.getOnlyElement(x.annotations());
  assertThat(annotation.typeName()).isEqualTo("Ljava/lang/Deprecated;");

  ClassFile.FieldInfo y = classFile.fields().get(1);
  assertThat(y.access())
      .isEqualTo(TurbineFlag.ACC_PUBLIC | TurbineFlag.ACC_STATIC | TurbineFlag.ACC_FINAL);
  assertThat(y.name()).isEqualTo("y");
  assertThat(y.descriptor()).isEqualTo("I");
  assertThat(y.value().constantTypeKind()).isEqualTo(TurbineConstantTypeKind.INT);
  assertThat(((Const.IntValue) y.value()).value()).isEqualTo(42);
  assertThat(y.annotations()).isEmpty();

  ClassFile.FieldInfo z = classFile.fields().get(2);
  assertThat(z.name()).isEqualTo("z");
  assertThat(z.descriptor()).isEqualTo("Ljava/util/List;");
  assertThat(z.signature()).isEqualTo("Ljava/util/List<TX;>;");
  assertThat(z.annotations()).isEmpty();
}
 
Example 7
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 8
Source File: ASMClassParser.java    From TickDynamic with MIT License 4 votes vote down vote up
protected void parseField(int access, String signature) throws Exception {
	String value = getCurrentToken();
	
	String desc = value;
	String name = nextToken();
	Object fieldValue = null;
	
	//Check for preset value
	if(nextToken().equals("="))
		fieldValue = parseValue(nextToken()).value;
	else
		currentToken--; //Go back from our peek
	
	FieldVisitor field = cl.visitField(access, name, desc, signature, fieldValue);
	//System.out.println("Field: " + Integer.toHexString(access).toUpperCase() + " | " + name + " | " + desc + " | " + fieldValue);
	
	if(!nextToken().equals("\n"))
		throw new Exception(getCurrentTokenLine() + ": Error: Expected newline while parsing field: " + name + "! Got: " + getCurrentToken());
	
	//Read any annotations for the field
	while(true)
	{
		value = nextToken();
		if(value.startsWith("@"))
		{
			int valuesOffset = value.indexOf("(");
			String valuesToken = "";
			if(valuesOffset != -1)
			{
				valuesToken = value.substring(valuesOffset);
				valuesToken += getLine(); //There is whitespace inside the (), so we have to combine the tokens
			}
			desc = value.substring(1, valuesOffset);
			
			AnnotationVisitor av = field.visitAnnotation(desc, true);
			parseAnnotation(av, valuesToken);
			av.visitEnd();
		}
		else //End of field if we reach anything else
		{
			currentToken--;
			break;
		}
	}
	
	field.visitEnd();
}