Java Code Examples for org.apache.bcel.classfile.Field#getName()

The following examples show how to use org.apache.bcel.classfile.Field#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: GenerateStubDialog.java    From j-j-jvm with Apache License 2.0 6 votes vote down vote up
protected String field2str(final Field field) {
  String modifier = "";

  if (field.isPrivate()) {
    modifier = "private ";
  } else if (field.isProtected()) {
    modifier = "protected ";
  } else if (field.isPublic()) {
    modifier = "public ";
  }

  if (field.isStatic()) {
    modifier += "static ";
  }

  if (field.isFinal()) {
    modifier += "final ";
  }

  modifier += field.getType().toString();

  modifier += ' ' + field.getName();

  return modifier;
}
 
Example 2
Source File: FieldGen.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiate from existing field.
 *
 * @param field Field object
 * @param cp constant pool (must contain the same entries as the field's constant pool)
 */
public FieldGen(final Field field, final ConstantPoolGen cp) {
    this(field.getAccessFlags(), Type.getType(field.getSignature()), field.getName(), cp);
    final Attribute[] attrs = field.getAttributes();
    for (final Attribute attr : attrs) {
        if (attr instanceof ConstantValue) {
            setValue(((ConstantValue) attr).getConstantValueIndex());
        } else if (attr instanceof Annotations) {
            final Annotations runtimeAnnotations = (Annotations)attr;
            final AnnotationEntry[] annotationEntries = runtimeAnnotations.getAnnotationEntries();
            for (final AnnotationEntry element : annotationEntries) {
                addAnnotationEntry(new AnnotationEntryGen(element,cp,false));
            }
        } else {
            addAttribute(attr);
        }
    }
}
 
Example 3
Source File: MethodHTML.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Print field of class.
 *
 * @param field field to print
 * @throws java.io.IOException
 */
private void writeField( final Field field ) throws IOException {
    final String type = Utility.signatureToString(field.getSignature());
    final String name = field.getName();
    String access = Utility.accessToString(field.getAccessFlags());
    Attribute[] attributes;
    access = Utility.replace(access, " ", " ");
    file.print("<TR><TD><FONT COLOR=\"#FF0000\">" + access + "</FONT></TD>\n<TD>"
            + Class2HTML.referenceType(type) + "</TD><TD><A NAME=\"field" + name + "\">" + name
            + "</A></TD>");
    attributes = field.getAttributes();
    // Write them to the Attributes.html file with anchor "<name>[<i>]"
    for (int i = 0; i < attributes.length; i++) {
        attribute_html.writeAttribute(attributes[i], name + "@" + i);
    }
    for (int i = 0; i < attributes.length; i++) {
        if (attributes[i].getTag() == Const.ATTR_CONSTANT_VALUE) { // Default value
            final String str = ((ConstantValue) attributes[i]).toString();
            // Reference attribute in _attributes.html
            file.print("<TD>= <A HREF=\"" + className + "_attributes.html#" + name + "@" + i
                    + "\" TARGET=\"Attributes\">" + str + "</TD>\n");
            break;
        }
    }
    file.println("</TR>");
}
 
Example 4
Source File: Naming.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
private boolean badFieldName(Field obj) {
    String fieldName = obj.getName();
    return !obj.isFinal() && Character.isLetter(fieldName.charAt(0)) && !Character.isLowerCase(fieldName.charAt(0))
            && fieldName.indexOf('_') == -1 && Character.isLetter(fieldName.charAt(1))
            && Character.isLowerCase(fieldName.charAt(1));
}
 
Example 5
Source File: FindMaskedFields.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    classFields.clear();

    Field[] fields = obj.getFields();
    String fieldName;
    for (Field field : fields) {
        if (!field.isStatic() && !field.isPrivate()) {
            fieldName = field.getName();
            classFields.put(fieldName, field);
        }
    }

    // Walk up the super class chain, looking for name collisions

    XClass c = getXClass();
    while (true) {
        ClassDescriptor s = c.getSuperclassDescriptor();
        if (s == null || "java/lang/Object".equals(s.getClassName())) {
            break;
        }
        try {
            c = Global.getAnalysisCache().getClassAnalysis(XClass.class, s);
        } catch (CheckedAnalysisException e) {
            break;
        }

        for (XField fld : c.getXFields()) {
            if (!fld.isStatic() && (fld.isPublic() || fld.isProtected())) {
                fieldName = fld.getName();
                if (fieldName.length() == 1) {
                    continue;
                }
                if ("serialVersionUID".equals(fieldName)) {
                    continue;
                }
                String superClassName = s.getClassName();
                if (superClassName.startsWith("java/io")
                        && (superClassName.endsWith("InputStream") && "in".equals(fieldName) || superClassName
                                .endsWith("OutputStream") && "out".equals(fieldName))) {
                    continue;
                }
                if (classFields.containsKey(fieldName)) {
                    Field maskingField = classFields.get(fieldName);
                    String mClassName = getDottedClassName();
                    FieldAnnotation fa = new FieldAnnotation(mClassName, maskingField.getName(), maskingField.getSignature(),
                            maskingField.isStatic());
                    int priority = NORMAL_PRIORITY;
                    if (maskingField.isStatic() || maskingField.isFinal()) {
                        priority++;
                    } else if (fld.getSignature().charAt(0) == 'L' && !fld.getSignature().startsWith("Ljava/lang/")
                            || fld.getSignature().charAt(0) == '[') {
                        priority--;
                    }
                    if (!fld.getSignature().equals(maskingField.getSignature())) {
                        priority += 2;
                    } else if (fld.getAccessFlags() != maskingField.getAccessFlags()) {
                        priority++;
                    }
                    if (fld.isSynthetic() || fld.getName().indexOf('$') >= 0) {
                        priority++;
                    }

                    FieldAnnotation maskedFieldAnnotation = FieldAnnotation.fromFieldDescriptor(fld.getFieldDescriptor());
                    BugInstance bug = new BugInstance(this, "MF_CLASS_MASKS_FIELD", priority).addClass(this).addField(fa)
                            .describe("FIELD_MASKING").addField(maskedFieldAnnotation).describe("FIELD_MASKED");
                    rememberedBugs.add(new RememberedBug(bug, fa, maskedFieldAnnotation));

                }
            }
        }
    }

    super.visit(obj);
}
 
Example 6
Source File: GenerateStubDialog.java    From j-j-jvm with Apache License 2.0 4 votes vote down vote up
protected String makeFieldName(final String className, final Field field) {
  return className + '.' + field.getName() + '.' + field.getSignature();
}
 
Example 7
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitField(final Field obj) {

    if (jc.isClass()) {
        int maxone=0;
        if (obj.isPrivate()) {
            maxone++;
        }
        if (obj.isProtected()) {
            maxone++;
        }
        if (obj.isPublic()) {
            maxone++;
        }
        if (maxone > 1) {
            throw new ClassConstraintException("Field '"+tostring(obj)+
                "' must only have at most one of its ACC_PRIVATE, ACC_PROTECTED, ACC_PUBLIC modifiers set.");
        }

        if (obj.isFinal() && obj.isVolatile()) {
            throw new ClassConstraintException("Field '"+tostring(obj)+
                "' must only have at most one of its ACC_FINAL, ACC_VOLATILE modifiers set.");
        }
    }
    else{ // isInterface!
        if (!obj.isPublic()) {
            throw new ClassConstraintException("Interface field '"+tostring(obj)+
                "' must have the ACC_PUBLIC modifier set but hasn't!");
        }
        if (!obj.isStatic()) {
            throw new ClassConstraintException("Interface field '"+tostring(obj)+
                "' must have the ACC_STATIC modifier set but hasn't!");
        }
        if (!obj.isFinal()) {
            throw new ClassConstraintException("Interface field '"+tostring(obj)+
                "' must have the ACC_FINAL modifier set but hasn't!");
        }
    }

    if ((obj.getAccessFlags() & ~(Const.ACC_PUBLIC|Const.ACC_PRIVATE|Const.ACC_PROTECTED|Const.ACC_STATIC|
                                  Const.ACC_FINAL|Const.ACC_VOLATILE|Const.ACC_TRANSIENT)) > 0) {
        addMessage("Field '"+tostring(obj)+
            "' has access flag(s) other than ACC_PUBLIC, ACC_PRIVATE, ACC_PROTECTED,"+
                " ACC_STATIC, ACC_FINAL, ACC_VOLATILE, ACC_TRANSIENT set (ignored).");
    }

    checkIndex(obj, obj.getNameIndex(), CONST_Utf8);

    final String name = obj.getName();
    if (! validFieldName(name)) {
        throw new ClassConstraintException("Field '"+tostring(obj)+"' has illegal name '"+obj.getName()+"'.");
    }

    // A descriptor is often named signature in BCEL
    checkIndex(obj, obj.getSignatureIndex(), CONST_Utf8);

    final String sig  = ((ConstantUtf8) (cp.getConstant(obj.getSignatureIndex()))).getBytes(); // Field or Method sig.(=descriptor)

    try{
        Type.getType(sig);  /* Don't need the return value */
    }
    catch (final ClassFormatException cfe) {
        throw new ClassConstraintException("Illegal descriptor (==signature) '"+sig+"' used by '"+tostring(obj)+"'.", cfe);
    }

    final String nameanddesc = name+sig;
    if (field_names_and_desc.contains(nameanddesc)) {
        throw new ClassConstraintException("No two fields (like '"+tostring(obj)+
            "') are allowed have same names and descriptors!");
    }
    if (field_names.contains(name)) {
        addMessage("More than one field of name '"+name+
            "' detected (but with different type descriptors). This is very unusual.");
    }
    field_names_and_desc.add(nameanddesc);
    field_names.add(name);

    final Attribute[] atts = obj.getAttributes();
    for (final Attribute att : atts) {
        if ((!(att instanceof ConstantValue)) &&
                (!(att instanceof Synthetic)) &&
                (!(att instanceof Deprecated))) {
            addMessage("Attribute '" + tostring(att) + "' as an attribute of Field '" +
                tostring(obj) + "' is unknown and will therefore be ignored.");
        }
        if (!(att instanceof ConstantValue)) {
            addMessage("Attribute '" + tostring(att) + "' as an attribute of Field '" + tostring(obj) +
                "' is not a ConstantValue and is therefore only of use for debuggers and such.");
        }
    }
}
 
Example 8
Source File: ClassParserUsingBCEL.java    From spotbugs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * @param obj
 *            the field to parse
 * @return a descriptor for the field
 */
protected FieldDescriptor parseField(Field obj) {
    return new FieldDescriptor(slashedClassName, obj.getName(), obj.getSignature(), obj.isStatic());
}
 
Example 9
Source File: FieldAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Factory method. Construct from class name and BCEL Field object.
 *
 * @param className
 *            the name of the class which defines the field
 * @param field
 *            the BCEL Field object
 * @return the FieldAnnotation
 */
public static FieldAnnotation fromBCELField(@DottedClassName String className, Field field) {
    return new FieldAnnotation(className, field.getName(), field.getSignature(), field.isStatic());
}
 
Example 10
Source File: FieldAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Factory method. Construct from class name and BCEL Field object.
 *
 * @param jClass
 *            the class which defines the field
 * @param field
 *            the BCEL Field object
 * @return the FieldAnnotation
 */
public static FieldAnnotation fromBCELField(JavaClass jClass, Field field) {
    return new FieldAnnotation(jClass.getClassName(), field.getName(), field.getSignature(), field.isStatic());
}