Java Code Examples for javassist.bytecode.FieldInfo#getName()

The following examples show how to use javassist.bytecode.FieldInfo#getName() . 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: ClassPathScanner.java    From Bats with Apache License 2.0 6 votes vote down vote up
@Override
public void scan(final Object cls) {
  final ClassFile classFile = (ClassFile)cls;
  AnnotationsAttribute annotations = ((AnnotationsAttribute)classFile.getAttribute(AnnotationsAttribute.visibleTag));
  if (annotations != null) {
    boolean isAnnotated = false;
    for (javassist.bytecode.annotation.Annotation a : annotations.getAnnotations()) {
      if (annotationsToScan.contains(a.getTypeName())) {
        isAnnotated = true;
      }
    }
    if (isAnnotated) {
      List<AnnotationDescriptor> classAnnotations = getAnnotationDescriptors(annotations);
      List<FieldInfo> classFields = classFile.getFields();
      List<FieldDescriptor> fieldDescriptors = new ArrayList<>(classFields.size());
      for (FieldInfo field : classFields) {
        String fieldName = field.getName();
        AnnotationsAttribute fieldAnnotations = ((AnnotationsAttribute) field.getAttribute(AnnotationsAttribute.visibleTag));
        fieldDescriptors.add(new FieldDescriptor(fieldName, field.getDescriptor(), getAnnotationDescriptors(fieldAnnotations)));
      }
      functions.add(new AnnotatedClassDescriptor(classFile.getName(), classAnnotations, fieldDescriptors));
    }
  }
}
 
Example 2
Source File: ClassFileJavassist.java    From jbse with GNU General Public License v3.0 6 votes vote down vote up
private ArrayList<Signature> getDeclaredFields(boolean areStatic) {
    if ((areStatic ? this.fieldsStatic : this.fieldsObject) == null) {
        final ArrayList<Signature> fields = new ArrayList<Signature>();
        final List<FieldInfo> fieldsJA = this.cf.getFields();
        for (FieldInfo fld : fieldsJA) {
            if (Modifier.isStatic(AccessFlag.toModifier(fld.getAccessFlags())) == areStatic) {
                final Signature sig = new Signature(getClassName(), fld.getDescriptor(), fld.getName());
                fields.add(sig);
            }
        }
        if (areStatic) {
            this.fieldsStatic = fields;
        } else {
            this.fieldsObject = fields;
        }
    }
    return (areStatic ? this.fieldsStatic : this.fieldsObject);
}
 
Example 3
Source File: ClassPathScanner.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public void scan(final Object cls) {
  final ClassFile classFile = (ClassFile)cls;
  AnnotationsAttribute annotations = ((AnnotationsAttribute)classFile.getAttribute(AnnotationsAttribute.visibleTag));
  if (annotations != null) {
    boolean isAnnotated = false;
    for (javassist.bytecode.annotation.Annotation a : annotations.getAnnotations()) {
      if (annotationsToScan.contains(a.getTypeName())) {
        isAnnotated = true;
      }
    }
    if (isAnnotated) {
      List<AnnotationDescriptor> classAnnotations = getAnnotationDescriptors(annotations);
      @SuppressWarnings("unchecked")
      List<FieldInfo> classFields = classFile.getFields();
      List<FieldDescriptor> fieldDescriptors = new ArrayList<>(classFields.size());
      for (FieldInfo field : classFields) {
        String fieldName = field.getName();
        AnnotationsAttribute fieldAnnotations = ((AnnotationsAttribute)field.getAttribute(AnnotationsAttribute.visibleTag));
        final List<AnnotationDescriptor> annotationDescriptors =
            (fieldAnnotations != null) ? getAnnotationDescriptors(fieldAnnotations) : Collections.<AnnotationDescriptor>emptyList();
        fieldDescriptors.add(new FieldDescriptor(fieldName, field.getDescriptor(), annotationDescriptors));
      }
      functions.add(new AnnotatedClassDescriptor(classFile.getName(), classAnnotations, fieldDescriptors));
    }
  }
}
 
Example 4
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void addReadMethod(ClassFile classfile, FieldInfo finfo)
		throws CannotCompileException {
	ConstPool cp = classfile.getConstPool();
	int this_class_index = cp.getThisClassInfo();
	String desc = "()" + finfo.getDescriptor();
	MethodInfo minfo = new MethodInfo(cp, EACH_READ_METHOD_PREFIX
	                                      + finfo.getName(), desc);
	/* local variables | target obj | each oldvalue | */
	Bytecode code = new Bytecode(cp, 5, 3);
	// aload_0
	code.addAload(0);
	// getfield // get each field
	code.addOpcode(Opcode.GETFIELD);
	int base_field_index = cp.addFieldrefInfo(this_class_index, finfo
			.getName(), finfo.getDescriptor());
	code.addIndex(base_field_index);
	// aload_0
	code.addAload(0);
	// invokeinterface // invoke Enabled.getInterceptFieldCallback()
	int enabled_class_index = cp.addClassInfo(FIELD_HANDLED_TYPE_NAME);
	code.addInvokeinterface(enabled_class_index,
	                        GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
	                        1);
	// ifnonnull
	code.addOpcode(Opcode.IFNONNULL);
	code.addIndex(4);
	// *return // each type
	addTypeDependDataReturn(code, finfo.getDescriptor());
	// *store_1 // each type
	addTypeDependDataStore(code, finfo.getDescriptor(), 1);
	// aload_0
	code.addAload(0);
	// invokeinterface // invoke Enabled.getInterceptFieldCallback()
	code.addInvokeinterface(enabled_class_index,
	                        GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
	                        1);
	// aload_0
	code.addAload(0);
	// ldc // name of the field
	code.addLdc(finfo.getName());
	// *load_1 // each type
	addTypeDependDataLoad(code, finfo.getDescriptor(), 1);
	// invokeinterface // invoke Callback.read*() // each type
	addInvokeFieldHandlerMethod(classfile, code, finfo.getDescriptor(),
	                            true);
	// *return // each type
	addTypeDependDataReturn(code, finfo.getDescriptor());

	minfo.setCodeAttribute(code.toCodeAttribute());
	minfo.setAccessFlags(AccessFlag.PUBLIC);
	classfile.addMethod(minfo);
}
 
Example 5
Source File: FieldTransformer.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private void addWriteMethod(ClassFile classfile, FieldInfo finfo)
		throws CannotCompileException {
	ConstPool cp = classfile.getConstPool();
	int this_class_index = cp.getThisClassInfo();
	String desc = "(" + finfo.getDescriptor() + ")V";
	MethodInfo minfo = new MethodInfo(cp, EACH_WRITE_METHOD_PREFIX
	                                      + finfo.getName(), desc);
	/* local variables | target obj | each oldvalue | */
	Bytecode code = new Bytecode(cp, 6, 3);
	// aload_0
	code.addAload(0);
	// invokeinterface // enabled.getInterceptFieldCallback()
	int enabled_class_index = cp.addClassInfo(FIELD_HANDLED_TYPE_NAME);
	code.addInvokeinterface(enabled_class_index,
	                        GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
	                        1);
	// ifnonnull (label1)
	code.addOpcode(Opcode.IFNONNULL);
	code.addIndex(9);
	// aload_0
	code.addAload(0);
	// *load_1
	addTypeDependDataLoad(code, finfo.getDescriptor(), 1);
	// putfield
	code.addOpcode(Opcode.PUTFIELD);
	int base_field_index = cp.addFieldrefInfo(this_class_index, finfo
			.getName(), finfo.getDescriptor());
	code.addIndex(base_field_index);
	code.growStack(-Descriptor.dataSize(finfo.getDescriptor()));
	// return ;
	code.addOpcode(Opcode.RETURN);
	// aload_0
	code.addAload(0);
	// dup
	code.addOpcode(Opcode.DUP);
	// invokeinterface // enabled.getInterceptFieldCallback()
	code.addInvokeinterface(enabled_class_index,
	                        GETFIELDHANDLER_METHOD_NAME, GETFIELDHANDLER_METHOD_DESCRIPTOR,
	                        1);
	// aload_0
	code.addAload(0);
	// ldc // field name
	code.addLdc(finfo.getName());
	// aload_0
	code.addAload(0);
	// getfield // old value of the field
	code.addOpcode(Opcode.GETFIELD);
	code.addIndex(base_field_index);
	code.growStack(Descriptor.dataSize(finfo.getDescriptor()) - 1);
	// *load_1
	addTypeDependDataLoad(code, finfo.getDescriptor(), 1);
	// invokeinterface // callback.write*(..)
	addInvokeFieldHandlerMethod(classfile, code, finfo.getDescriptor(),
	                            false);
	// putfield // new value of the field
	code.addOpcode(Opcode.PUTFIELD);
	code.addIndex(base_field_index);
	code.growStack(-Descriptor.dataSize(finfo.getDescriptor()));
	// return
	code.addOpcode(Opcode.RETURN);

	minfo.setCodeAttribute(code.toCodeAttribute());
	minfo.setAccessFlags(AccessFlag.PUBLIC);
	classfile.addMethod(minfo);
}
 
Example 6
Source File: JavassistAdapter.java    From panda with Apache License 2.0 4 votes vote down vote up
@Override
public String getFieldName(FieldInfo field) {
    return field.getName();
}
 
Example 7
Source File: JField.java    From jadira with Apache License 2.0 4 votes vote down vote up
protected JField(FieldInfo fieldInfo, JClass jClass, ClasspathResolver resolver) {
    super(fieldInfo.getName(), resolver);
    this.fieldInfo = fieldInfo;
    this.jClass = jClass;
}