org.apache.bcel.classfile.ConstantFieldref Java Examples

The following examples show how to use org.apache.bcel.classfile.ConstantFieldref. 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: InstConstraintVisitor.java    From ApkToolPlus with Apache License 2.0 6 votes vote down vote up
/**
* Ensures the general preconditions of a FieldInstruction instance.
*/
public void visitFieldInstruction(FieldInstruction o){
	// visitLoadClass(o) has been called before: Every FieldOrMethod
	// implements LoadClass.
	// visitCPInstruction(o) has been called before.
// A FieldInstruction may be: GETFIELD, GETSTATIC, PUTFIELD, PUTSTATIC 
	Constant c = cpg.getConstant(o.getIndex());
	if (!(c instanceof ConstantFieldref)){
		constraintViolated(o, "Index '"+o.getIndex()+"' should refer to a CONSTANT_Fieldref_info structure, but refers to '"+c+"'.");
	}
	// the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
	Type t = o.getType(cpg);
	if (t instanceof ObjectType){
		String name = ((ObjectType)t).getClassName();
		Verifier v = VerifierFactory.getVerifier( name );
		VerificationResult vr = v.doPass2();
		if (vr.getStatus() != VerificationResult.VERIFIED_OK){
			constraintViolated((Instruction) o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
		}
	}
}
 
Example #2
Source File: InnerClassAccessMap.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Called to indicate that a field load or store was encountered.
 *
 * @param cpIndex
 *            the constant pool index of the fieldref
 * @param isStatic
 *            true if it is a static field access
 * @param isLoad
 *            true if the access is a load
 */
private void setField(int cpIndex, boolean isStatic, boolean isLoad) {
    // We only allow one field access for an accessor method.
    accessCount++;
    if (accessCount != 1) {
        access = null;
        return;
    }

    ConstantPool cp = javaClass.getConstantPool();
    ConstantFieldref fieldref = (ConstantFieldref) cp.getConstant(cpIndex);

    ConstantClass cls = (ConstantClass) cp.getConstant(fieldref.getClassIndex());
    String className = cls.getBytes(cp).replace('/', '.');

    ConstantNameAndType nameAndType = (ConstantNameAndType) cp.getConstant(fieldref.getNameAndTypeIndex());
    String fieldName = nameAndType.getName(cp);
    String fieldSig = nameAndType.getSignature(cp);


    XField xfield = Hierarchy.findXField(className, fieldName, fieldSig, isStatic);
    if (xfield != null && xfield.isStatic() == isStatic && isValidAccessMethod(methodSig, xfield, isLoad)) {
        access = new InnerClassAccess(methodName, methodSig, xfield, isLoad);
    }

}
 
Example #3
Source File: ConstantPoolGen.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Add a new Fieldref constant to the ConstantPool, if it is not already
 * in there.
 *
 * @param class_name class name string to add
 * @param field_name field name string to add
 * @param signature signature string to add
 * @return index of entry
 */
public int addFieldref( final String class_name, final String field_name, final String signature ) {
    int ret;
    int class_index;
    int name_and_type_index;
    if ((ret = lookupFieldref(class_name, field_name, signature)) != -1) {
        return ret; // Already in CP
    }
    adjustSize();
    class_index = addClass(class_name);
    name_and_type_index = addNameAndType(field_name, signature);
    ret = index;
    constants[index++] = new ConstantFieldref(class_index, name_and_type_index);
    final String key = class_name + FIELDREF_DELIM + field_name + FIELDREF_DELIM + signature;
    if (!cpTable.containsKey(key)) {
        cpTable.put(key, new Index(ret));
    }
    return ret;
}
 
Example #4
Source File: InstConstraintVisitor.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * Ensures the general preconditions of a FieldInstruction instance.
 */
 @Override
public void visitFieldInstruction(final FieldInstruction o) {
     // visitLoadClass(o) has been called before: Every FieldOrMethod
     // implements LoadClass.
     // visitCPInstruction(o) has been called before.
    // A FieldInstruction may be: GETFIELD, GETSTATIC, PUTFIELD, PUTSTATIC
        final Constant c = cpg.getConstant(o.getIndex());
        if (!(c instanceof ConstantFieldref)) {
            constraintViolated(o,
                "Index '"+o.getIndex()+"' should refer to a CONSTANT_Fieldref_info structure, but refers to '"+c+"'.");
        }
        // the o.getClassType(cpg) type has passed pass 2; see visitLoadClass(o).
        final Type t = o.getType(cpg);
        if (t instanceof ObjectType) {
            final String name = ((ObjectType)t).getClassName();
            final Verifier v = VerifierFactory.getVerifier( name );
            final VerificationResult vr = v.doPass2();
            if (vr.getStatus() != VerificationResult.VERIFIED_OK) {
                constraintViolated(o, "Class '"+name+"' is referenced, but cannot be loaded and resolved: '"+vr+"'.");
            }
        }
 }
 
Example #5
Source File: ClassDumper.java    From cloud-opensource-java with Apache License 2.0 5 votes vote down vote up
private static FieldSymbol makeSymbol(
    ConstantFieldref constantFieldref, ConstantPool constantPool) {
  // Either a class type or an interface type
  String className = constantFieldref.getClass(constantPool);
  ConstantNameAndType constantNameAndType = constantNameAndType(constantFieldref, constantPool);
  String fieldName = constantNameAndType.getName(constantPool);
  String descriptor = constantNameAndType.getSignature(constantPool);
  return new FieldSymbol(className, fieldName, descriptor);
}
 
Example #6
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantFieldref(final ConstantFieldref obj) {
    if (obj.getTag() != Const.CONSTANT_Fieldref) {
        throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'.");
    }
    checkIndex(obj, obj.getClassIndex(), CONST_Class);
    checkIndex(obj, obj.getNameAndTypeIndex(), CONST_NameAndType);
}
 
Example #7
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
@Override
public void visitConstantFieldref(final ConstantFieldref obj) {
    if (obj.getTag() != Const.CONSTANT_Fieldref) {
        throw new ClassConstraintException("ConstantFieldref '"+tostring(obj)+"' has wrong tag!");
    }
    final int name_and_type_index = obj.getNameAndTypeIndex();
    final ConstantNameAndType cnat = (ConstantNameAndType) (cp.getConstant(name_and_type_index));
    final String name = ((ConstantUtf8) (cp.getConstant(cnat.getNameIndex()))).getBytes(); // Field or Method name
    if (!validFieldName(name)) {
        throw new ClassConstraintException("Invalid field name '"+name+"' referenced by '"+tostring(obj)+"'.");
    }

    final int class_index = obj.getClassIndex();
    final ConstantClass cc = (ConstantClass) (cp.getConstant(class_index));
    final String className = ((ConstantUtf8) (cp.getConstant(cc.getNameIndex()))).getBytes(); // Class Name in internal form
    if (! validClassName(className)) {
        throw new ClassConstraintException("Illegal class name '"+className+"' used by '"+tostring(obj)+"'.");
    }

    final String sig  = ((ConstantUtf8) (cp.getConstant(cnat.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);
    }
}
 
Example #8
Source File: ClassDumper.java    From cloud-opensource-java with Apache License 2.0 4 votes vote down vote up
private static SymbolReferences.Builder findSymbolReferences(
    ClassFile source, JavaClass javaClass) {
  SymbolReferences.Builder builder = new SymbolReferences.Builder();

  ConstantPool constantPool = javaClass.getConstantPool();
  Constant[] constants = constantPool.getConstantPool();
  for (Constant constant : constants) {
    if (constant == null) {
       continue;
    }

    byte constantTag = constant.getTag();
    switch (constantTag) {
      case Const.CONSTANT_Class:
        ConstantClass constantClass = (ConstantClass) constant;
        ClassSymbol classSymbol = makeSymbol(constantClass, constantPool, javaClass);
        // skip array class because it is provided by runtime
        if (classSymbol.getClassBinaryName().startsWith("[")) {
          break;
        }
        builder.addClassReference(source, classSymbol);
        break;
      case Const.CONSTANT_Methodref:
      case Const.CONSTANT_InterfaceMethodref:
        // Both ConstantMethodref and ConstantInterfaceMethodref are subclass of ConstantCP
        ConstantCP constantMethodref = (ConstantCP) constant;
        builder.addMethodReference(source, makeSymbol(constantMethodref, constantPool));
        break;
      case Const.CONSTANT_Fieldref:
        ConstantFieldref constantFieldref = (ConstantFieldref) constant;
        builder.addFieldReference(source, makeSymbol(constantFieldref, constantPool));
        break;
      default:
        break;
    }
  }

  for (String interfaceName : javaClass.getInterfaceNames()) {
    builder.addClassReference(source, new InterfaceSymbol(interfaceName));
  }

  return builder;
}
 
Example #9
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void visit(ConstantFieldref obj) {
    visit((ConstantCP) obj);
}
 
Example #10
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitConstantFieldref(ConstantFieldref obj) {
    visit(obj);
}
 
Example #11
Source File: ResolveAllReferences.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visit(JavaClass obj) {
    compute();
    ConstantPool cp = obj.getConstantPool();
    Constant[] constants = cp.getConstantPool();
    checkConstant: for (int i = 0; i < constants.length; i++) {
        Constant co = constants[i];
        if (co instanceof ConstantDouble || co instanceof ConstantLong) {
            i++;
        }
        if (co instanceof ConstantClass) {
            String ref = getClassName(obj, i);
            if ((ref.startsWith("java") || ref.startsWith("org.w3c.dom")) && !defined.contains(ref)) {
                bugReporter.reportBug(new BugInstance(this, "VR_UNRESOLVABLE_REFERENCE", NORMAL_PRIORITY).addClass(obj)
                        .addString(ref));
            }

        } else if (co instanceof ConstantFieldref) {
            // do nothing until we handle static fields defined in
            // interfaces

        } else if (co instanceof ConstantInvokeDynamic) {
            // ignore. BCEL puts garbage data into ConstantInvokeDynamic
        } else if (co instanceof ConstantCP) {
            ConstantCP co2 = (ConstantCP) co;
            String className = getClassName(obj, co2.getClassIndex());

            // System.out.println("checking " + ref);
            if (className.equals(obj.getClassName()) || !defined.contains(className)) {
                // System.out.println("Skipping check of " + ref);
                continue checkConstant;
            }
            ConstantNameAndType nt = (ConstantNameAndType) cp.getConstant(co2.getNameAndTypeIndex());
            String name = ((ConstantUtf8) obj.getConstantPool().getConstant(nt.getNameIndex(), Const.CONSTANT_Utf8)).getBytes();
            String signature = ((ConstantUtf8) obj.getConstantPool().getConstant(nt.getSignatureIndex(), Const.CONSTANT_Utf8))
                    .getBytes();

            try {
                JavaClass target = Repository.lookupClass(className);
                if (!find(target, name, signature)) {
                    bugReporter.reportBug(new BugInstance(this, "VR_UNRESOLVABLE_REFERENCE", NORMAL_PRIORITY).addClass(obj)
                            .addString(getMemberName(target.getClassName(), name, signature)));
                }

            } catch (ClassNotFoundException e) {
                bugReporter.reportMissingClass(e);
            }
        }

    }
}
 
Example #12
Source File: TransitiveHull.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantFieldref(final ConstantFieldref cfr) {
    visitRef(cfr, false);
}
 
Example #13
Source File: StringRepresentation.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantFieldref(final ConstantFieldref obj) {
    tostring = toString(obj);
}
 
Example #14
Source File: CounterVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitConstantFieldref(final ConstantFieldref obj)
{
    constantFieldrefCount++;
}