Java Code Examples for org.objectweb.asm.FieldVisitor

The following examples show how to use org.objectweb.asm.FieldVisitor. These examples are extracted from open source projects. 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 Project: bazel   Source File: IntArrayFieldInitializer.java    License: 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 2
Source Project: we-cmdb   Source File: DynamicEntityGenerator.java    License: 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 3
@Override
public FieldVisitor visitField(
       final int access,
       final String name,
       final String desc,
       final String signature,
       final Object value)
   {
   	if ((access & ACC_SYNTHETIC) == 0) {
        JvmFieldBuilder fieldBuilder = new JvmFieldBuilder(
        		result,
        		(access & ACC_STATIC) == 0 ? typeParameters : null,
        		proxies,
   				access,
        		name,
        		desc,
        		signature,
        		value);
        return fieldBuilder;
   	}
   	return null;
   }
 
Example 4
Source Project: Concurnas   Source File: ClassRemapper.java    License: MIT License 6 votes vote down vote up
@Override
public FieldVisitor visitField(
    final int access,
    final String name,
    final String descriptor,
    final String signature,
    final Object value) {
  FieldVisitor fieldVisitor =
      super.visitField(
          access,
          remapper.mapFieldName(className, name, descriptor),
          remapper.mapDesc(descriptor),
          remapper.mapSignature(signature, true),
          (value == null) ? null : remapper.mapValue(value));
  return fieldVisitor == null ? null : createFieldRemapper(fieldVisitor);
}
 
Example 5
Source Project: JAADAS   Source File: SootClassBuilder.java    License: GNU General Public License v3.0 6 votes vote down vote up
@Override
public FieldVisitor visitField(int access, String name,
		String desc, String signature, Object value) {
	soot.Type type = AsmUtil.toJimpleType(desc);
	addDep(type);
	SootField field = new SootField(name, type, access);
	Tag tag;
	if (value instanceof Integer)
		tag = new IntegerConstantValueTag((Integer) value);
	else if (value instanceof Float)
		tag = new FloatConstantValueTag((Float) value);
	else if (value instanceof Long)
		tag = new LongConstantValueTag((Long) value);
	else if (value instanceof Double)
		tag = new DoubleConstantValueTag((Double) value);
	else if (value instanceof String)
		tag = new StringConstantValueTag(value.toString());
	else
		tag = null;
	if (tag != null)
		field.addTag(tag);
	if (signature != null)
		field.addTag(new SignatureTag(signature));
	klass.addField(field);
	return new FieldBuilder(field, this);
}
 
Example 6
Source Project: buck   Source File: ClassVisitorDriverFromElement.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Void visitVariable(VariableElement e, ClassVisitor classVisitor) {
  if (e.getModifiers().contains(Modifier.PRIVATE)) {
    return null;
  }

  FieldVisitor fieldVisitor =
      classVisitor.visitField(
          accessFlagsUtils.getAccessFlags(e),
          e.getSimpleName().toString(),
          descriptorFactory.getDescriptor(e),
          signatureFactory.getSignature(e),
          e.getConstantValue());
  visitAnnotations(e, fieldVisitor::visitAnnotation);
  fieldVisitor.visitEnd();

  return null;
}
 
Example 7
Source Project: bazel   Source File: InterfaceDesugaring.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public FieldVisitor visitField(
    int access, String name, String desc, String signature, Object value) {
  if (legacyJaCoCo
      && isInterface()
      && BitFlags.isSet(access, Opcodes.ACC_FINAL)
      && "$jacocoData".equals(name)) {
    // Move $jacocoData field to companion class and remove final modifier. We'll rewrite field
    // accesses accordingly. Code generated by older JaCoCo versions tried to assign to this
    // final field in methods, and interface fields have to be private, so we move the field
    // to a class, which ends up looking pretty similar to what JaCoCo generates for classes.
    access &= ~Opcodes.ACC_FINAL;
    return companion().visitField(access, name, desc, signature, value);
  } else {
    return super.visitField(access, name, desc, signature, value);
  }
}
 
Example 8
@Override
public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) {
  checkState();
  checkAccess(access, Opcodes.ACC_PUBLIC + Opcodes.ACC_PRIVATE + Opcodes.ACC_PROTECTED + Opcodes.ACC_STATIC + Opcodes.ACC_FINAL
      + Opcodes.ACC_VOLATILE + Opcodes.ACC_TRANSIENT + Opcodes.ACC_SYNTHETIC + Opcodes.ACC_ENUM + Opcodes.ACC_DEPRECATED + 0x40000); // ClassWriter.ACC_SYNTHETIC_ATTRIBUTE
  CheckMethodAdapter.checkUnqualifiedName(version, name, "field name");
  CheckMethodAdapter.checkDesc(desc, false);
  if (signature != null) {
    checkFieldSignature(signature);
  }
  if (value != null) {
    CheckMethodAdapter.checkConstant(value);
  }
  FieldVisitor av = super.visitField(access, name, desc, signature, value);
  return new CheckFieldAdapter(av);
}
 
Example 9
Source Project: AVM   Source File: ConstantVisitor.java    License: MIT License 6 votes vote down vote up
@Override
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
    // The special case we want to handle is a field with the following properties:
    // -static
    // -non-null value
    // -String
    Object filteredValue = value;
    if ((0 != (access & Opcodes.ACC_STATIC))
            && (null != value)
            && postRenameStringDescriptor.equals(descriptor)
    ) {
        // We need to do something special in this case.
        this.staticFieldNamesToConstantValues.put(name, (String)value);
        filteredValue = null;
    }
    return super.visitField(access, name, descriptor, signature, filteredValue);
}
 
Example 10
Source Project: AVM   Source File: InterfaceFieldClassGeneratorTest.java    License: MIT License 6 votes vote down vote up
public static byte[] getNestedInterfaceCalledFIELDSLevelOne() {
    ClassWriter classWriter = new ClassWriter(0);
    FieldVisitor fieldVisitor;

    classWriter.visit(V10, ACC_ABSTRACT | ACC_INTERFACE, "NestedInterfaces$FIELDS", null, "java/lang/Object", null);

    classWriter.visitSource("NestedInterfaces.java", null);

    classWriter.visitInnerClass("NestedInterfaces$FIELDS", "NestedInterfaces", "FIELDS", ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

    classWriter.visitInnerClass("NestedInterfaces$FIELDS$FIELDS", "NestedInterfaces$FIELDS", "FIELDS", ACC_PUBLIC | ACC_STATIC | ACC_ABSTRACT | ACC_INTERFACE);

    {
        fieldVisitor = classWriter.visitField(ACC_PUBLIC | ACC_FINAL | ACC_STATIC, "a", "I", null, Integer.valueOf(101));
        fieldVisitor.visitEnd();
    }
    classWriter.visitEnd();

    return classWriter.toByteArray();
}
 
Example 11
Source Project: JReFrameworker   Source File: RemappingClassAdapter.java    License: MIT License 6 votes vote down vote up
@Override
public FieldVisitor visitField(
    final int access,
    final String name,
    final String descriptor,
    final String signature,
    final Object value) {
  FieldVisitor fieldVisitor =
      super.visitField(
          access,
          remapper.mapFieldName(className, name, descriptor),
          remapper.mapDesc(descriptor),
          remapper.mapSignature(signature, true),
          remapper.mapValue(value));
  return fieldVisitor == null ? null : createRemappingFieldAdapter(fieldVisitor);
}
 
Example 12
Source Project: Bats   Source File: CheckClassVisitorFsm.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FieldVisitor visitField(final int access, final String name,
    final String desc, final String signature, final Object value) {
  fsmCursor.transition("visitField");
  final FieldVisitor fieldVisitor = super.visitField(access, name, desc,
      signature, value);
  return fieldVisitor; // TODO: add CheckFieldVisitorFsm
}
 
Example 13
/**
 * Remaps strings in fields.
 */
@Override
public FieldVisitor visitField(int access, String name, String descriptor, String signature, Object value) {
	if (value instanceof String) {
		value = remap((String) value);
	}

	return super.visitField(access, name, descriptor, signature, value);
}
 
Example 14
Source Project: groovy   Source File: AsmClassGenerator.java    License: 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 15
FieldScanner(FieldVisitor parent, int access, String name, String descriptor) {
	super(Opcodes.ASM7, parent);

	this.access = access;
	this.name = name;
	this.descriptor = descriptor;
}
 
Example 16
Source Project: buck   Source File: DalvikStatsTool.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FieldVisitor visitField(
    int access, String name, String desc, String signature, Object value) {
  // For non-static fields, Field objects are 16 bytes.
  if ((access & Opcodes.ACC_STATIC) == 0) {
    footprint += 16;
  }

  Objects.requireNonNull(className, "Must not call visitField before visit");
  fieldReferenceBuilder.add(DalvikMemberReference.of(className, name, desc));

  return fieldVisitor;
}
 
Example 17
Source Project: JReFrameworker   Source File: SerialVersionUIDAdder.java    License: MIT License 5 votes vote down vote up
@Override
public FieldVisitor visitField(
    final int access,
    final String name,
    final String desc,
    final String signature,
    final Object value) {
  // Get the class field information for step 4 of the algorithm. Also determine if the class
  // already has a SVUID.
  if (computeSvuid) {
    if ("serialVersionUID".equals(name)) {
      // Since the class already has SVUID, we won't be computing it.
      computeSvuid = false;
      hasSvuid = true;
    }
    // Collect the non private fields. Only the ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED,
    // ACC_STATIC, ACC_FINAL, ACC_VOLATILE, and ACC_TRANSIENT flags are used when computing
    // serialVersionUID values.
    if ((access & Opcodes.ACC_PRIVATE) == 0
        || (access & (Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT)) == 0) {
      int mods =
          access
              & (Opcodes.ACC_PUBLIC
                  | Opcodes.ACC_PRIVATE
                  | Opcodes.ACC_PROTECTED
                  | Opcodes.ACC_STATIC
                  | Opcodes.ACC_FINAL
                  | Opcodes.ACC_VOLATILE
                  | Opcodes.ACC_TRANSIENT);
      svuidFields.add(new Item(name, mods, desc));
    }
  }

  return super.visitField(access, name, desc, signature, value);
}
 
Example 18
Source Project: JByteMod-Beta   Source File: JarOptimizer.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public FieldVisitor visitField(final int access, final String name, final String desc, final String signature, final Object value) {
  if (owner.startsWith("java/")) {
    System.out.println(owner + ' ' + name);
  }
  return null;
}
 
Example 19
Source Project: JReFrameworker   Source File: TraceClassVisitor.java    License: MIT License 5 votes vote down vote up
@Override
public FieldVisitor visitField(
    final int access,
    final String name,
    final String descriptor,
    final String signature,
    final Object value) {
  Printer fieldPrinter = p.visitField(access, name, descriptor, signature, value);
  return new TraceFieldVisitor(
      super.visitField(access, name, descriptor, signature, value), fieldPrinter);
}
 
Example 20
Source Project: byte-buddy   Source File: FieldAttributeAppender.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
public void apply(FieldVisitor fieldVisitor, FieldDescription fieldDescription, AnnotationValueFilter annotationValueFilter) {
    AnnotationAppender appender = new AnnotationAppender.Default(new AnnotationAppender.Target.OnField(fieldVisitor));
    for (AnnotationDescription annotation : annotations) {
        appender = appender.append(annotation, annotationValueFilter);
    }
}
 
Example 21
Source Project: Concurnas   Source File: OffHeapAugmentor.java    License: MIT License 5 votes vote down vote up
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value){
	if(!Modifier.isStatic(access)){
		if(!Modifier.isTransient(access)){
			fieldNameToDesc.put(name, desc);
		}
		fieldNameToDescIncTrans.put(name, desc);
	}
		
		
	return super.visitField(access, name, desc, signature, value);
}
 
Example 22
Source Project: Concurnas   Source File: InitConverter2.java    License: MIT License 5 votes vote down vote up
@Override
public FieldVisitor visitField(int access, final String name, final String descriptor, final String signature, final Object value) {
	if (Modifier.isFinal(access) && !Modifier.isStatic(access)) {
		access &= ~Opcodes.ACC_FINAL;
	}

	return cv.visitField(access, name, descriptor, signature, value);
}
 
Example 23
Source Project: cglib   Source File: ClassEmitter.java    License: Apache License 2.0 5 votes vote down vote up
public FieldVisitor visitField(int access,
                               String name,
                               String desc,
                               String signature,
                               Object value) {
    declare_field(access, name, Type.getType(desc), value);
    return null; // TODO
}
 
Example 24
Source Project: Concurnas   Source File: SerialVersionUIDAdder.java    License: MIT License 5 votes vote down vote up
@Override
public FieldVisitor visitField(
    final int access,
    final String name,
    final String desc,
    final String signature,
    final Object value) {
  // Get the class field information for step 4 of the algorithm. Also determine if the class
  // already has a SVUID.
  if (computeSvuid) {
    if ("serialVersionUID".equals(name)) {
      // Since the class already has SVUID, we won't be computing it.
      computeSvuid = false;
      hasSvuid = true;
    }
    // Collect the non private fields. Only the ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED,
    // ACC_STATIC, ACC_FINAL, ACC_VOLATILE, and ACC_TRANSIENT flags are used when computing
    // serialVersionUID values.
    if ((access & Opcodes.ACC_PRIVATE) == 0
        || (access & (Opcodes.ACC_STATIC | Opcodes.ACC_TRANSIENT)) == 0) {
      int mods =
          access
              & (Opcodes.ACC_PUBLIC
                  | Opcodes.ACC_PRIVATE
                  | Opcodes.ACC_PROTECTED
                  | Opcodes.ACC_STATIC
                  | Opcodes.ACC_FINAL
                  | Opcodes.ACC_VOLATILE
                  | Opcodes.ACC_TRANSIENT);
      svuidFields.add(new Item(name, mods, desc));
    }
  }

  return super.visitField(access, name, desc, signature, value);
}
 
Example 25
Source Project: CodenameOne   Source File: RefVerifier.java    License: GNU General Public License v2.0 5 votes vote down vote up
public FieldVisitor visitField(
        final int access,
        final String name,
        final String desc,
        final String signature,
        final Object value) {
	if (!methdodMatcher && name.equals(searchedName) && desc.equals(searchedDesc)) {
		throw new Success();
 }
    return null;
}
 
Example 26
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature,
                               Object value) {
    final String fieldType = readTypeDescription(desc);

    // add initial field value if any
    if (value != null) {
        final String fieldValueType = getObjectValueType(value);
    }
    processSignature(signature);

    return fieldVisitor;
}
 
Example 27
Source Project: cs652   Source File: GenNerdClass.java    License: 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 28
Source Project: vespa   Source File: AnalyzeClassVisitor.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) {
    Analyze.getClassName(Type.getType(desc)).ifPresent(imports::add);

    AnalyzeSignatureVisitor.analyzeField(signature, this);
    return new AnalyzeFieldVisitor(this);
}
 
Example 29
Source Project: Concurnas   Source File: TraceClassVisitor.java    License: MIT License 5 votes vote down vote up
@Override
public FieldVisitor visitField(
    final int access,
    final String name,
    final String descriptor,
    final String signature,
    final Object value) {
  Printer fieldPrinter = p.visitField(access, name, descriptor, signature, value);
  return new TraceFieldVisitor(
      super.visitField(access, name, descriptor, signature, value), fieldPrinter);
}
 
Example 30
public FieldVisitor visitField(int access,
                               String name,
                               String desc,
                               String signature,
                               Object value)
{
  addReferencedType(Type.getType(desc));

  return null;
}