Java Code Examples for org.apache.bcel.generic.FieldInstruction#getClassName()

The following examples show how to use org.apache.bcel.generic.FieldInstruction#getClassName() . 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: Hierarchy.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Look up the field referenced by given FieldInstruction, returning it as
 * an {@link XField XField} object.
 *
 * @param fins
 *            the FieldInstruction
 * @param cpg
 *            the ConstantPoolGen used by the class containing the
 *            instruction
 * @return an XField object representing the field, or null if no such field
 *         could be found
 */
public static @CheckForNull XField findXField(FieldInstruction fins, @Nonnull ConstantPoolGen cpg) {

    String className = fins.getClassName(cpg);
    String fieldName = fins.getFieldName(cpg);
    String fieldSig = fins.getSignature(cpg);

    boolean isStatic = (fins.getOpcode() == Const.GETSTATIC || fins.getOpcode() == Const.PUTSTATIC);

    XField xfield = findXField(className, fieldName, fieldSig, isStatic);
    short opcode = fins.getOpcode();
    if (xfield != null && xfield.isResolved()
            && xfield.isStatic() == (opcode == Const.GETSTATIC || opcode == Const.PUTSTATIC)) {
        return xfield;
    } else {
        return null;
    }
}
 
Example 2
Source File: FindRefComparison.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void handleLoad(FieldInstruction obj) {
    consumeStack(obj);

    Type type = obj.getType(getCPG());
    if (!STRING_SIGNATURE.equals(type.getSignature())) {
        throw new IllegalArgumentException("type is not String: " + type);
    }
    try {
        String className = obj.getClassName(getCPG());
        String fieldName = obj.getName(getCPG());
        Field field = Hierarchy.findField(className, fieldName);

        if (field != null) {
            // If the field is final, we'll assume that the String value
            // is static.
            if (field.isFinal()) {
                pushValue(staticStringTypeInstance);
            } else {
                pushValue(type);
            }

            return;
        }
    } catch (ClassNotFoundException ex) {
        lookupFailureCallback.reportMissingClass(ex);
    }

    pushValue(type);
}
 
Example 3
Source File: XFactory.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static XField createXField(FieldInstruction fieldInstruction, ConstantPoolGen cpg) {
    String className = fieldInstruction.getClassName(cpg);
    String fieldName = fieldInstruction.getName(cpg);
    String fieldSig = fieldInstruction.getSignature(cpg);

    int opcode = fieldInstruction.getOpcode();
    return createXField(className, fieldName, fieldSig, opcode == Const.GETSTATIC || opcode == Const.PUTSTATIC);
}
 
Example 4
Source File: FieldAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Is the given instruction a read of a field?
 *
 * @param ins
 *            the Instruction to check
 * @param cpg
 *            ConstantPoolGen of the method containing the instruction
 * @return the Field if the instruction is a read of a field, null otherwise
 */
public static FieldAnnotation isRead(Instruction ins, ConstantPoolGen cpg) {
    if (ins instanceof GETFIELD || ins instanceof GETSTATIC) {
        FieldInstruction fins = (FieldInstruction) ins;
        String className = fins.getClassName(cpg);
        return new FieldAnnotation(className, fins.getName(cpg), fins.getSignature(cpg), fins instanceof GETSTATIC);
    } else {
        return null;
    }
}
 
Example 5
Source File: FieldAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Is the instruction a write of a field?
 *
 * @param ins
 *            the Instruction to check
 * @param cpg
 *            ConstantPoolGen of the method containing the instruction
 * @return the Field if instruction is a write of a field, null otherwise
 */
public static FieldAnnotation isWrite(Instruction ins, ConstantPoolGen cpg) {
    if (ins instanceof PUTFIELD || ins instanceof PUTSTATIC) {
        FieldInstruction fins = (FieldInstruction) ins;
        String className = fins.getClassName(cpg);
        return new FieldAnnotation(className, fins.getName(cpg), fins.getSignature(cpg), fins instanceof PUTSTATIC);
    } else {
        return null;
    }
}
 
Example 6
Source File: BCELFactory.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitFieldInstruction( final FieldInstruction i ) {
    final short opcode = i.getOpcode();
    final String class_name = i.getClassName(_cp);
    final String field_name = i.getFieldName(_cp);
    final Type type = i.getFieldType(_cp);
    _out.println("il.append(_factory.createFieldAccess(\"" + class_name + "\", \"" + field_name
            + "\", " + BCELifier.printType(type) + ", " + CONSTANT_PREFIX
            + Const.getOpcodeName(opcode).toUpperCase(Locale.ENGLISH) + "));");
}