public static boolean hasInterestingClass(ConstantPool cp, Collection<String> classes) { for(Constant c : cp.getConstantPool()) { if(c instanceof ConstantClass) { String className = ((ConstantUtf8)cp.getConstant(((ConstantClass)c).getNameIndex())).getBytes(); if(classes.contains(className)) { return true; } } } return false; }
protected String getStringFromIndex(int i) { ConstantUtf8 name = (ConstantUtf8) constantPool.getConstant(i); return name.getBytes(); }
/** * Returns true if given constant pool probably has a reference to any of supplied methods * Useful to exclude from analysis uninteresting classes * @param cp constant pool * @param methods methods collection * @return true if method is found */ public static boolean hasInterestingMethod(ConstantPool cp, Collection<MethodDescriptor> methods) { for(Constant c : cp.getConstantPool()) { if(c instanceof ConstantMethodref || c instanceof ConstantInterfaceMethodref) { ConstantCP desc = (ConstantCP)c; ConstantNameAndType nameAndType = (ConstantNameAndType) cp.getConstant(desc.getNameAndTypeIndex()); String className = cp.getConstantString(desc.getClassIndex(), Const.CONSTANT_Class); String name = ((ConstantUtf8)cp.getConstant(nameAndType.getNameIndex())).getBytes(); String signature = ((ConstantUtf8)cp.getConstant(nameAndType.getSignatureIndex())).getBytes(); // We don't know whether method is static thus cannot use equals int hash = FieldOrMethodDescriptor.getNameSigHashCode(name, signature); for(MethodDescriptor method : methods) { if (method.getNameSigHashCode() == hash && (method.getSlashedClassName().isEmpty() || method.getSlashedClassName().equals(className)) && method.getName().equals(name) && method.getSignature().equals(signature)) { return true; } } } } return false; }
Constant constant = jclass.getConstantPool().getConstant(i); String className = null; if (constant instanceof ConstantMethodref) {
private String getMemberName(JavaClass c, String className, int memberNameIndex, int signatureIndex) { return className + "." + ((ConstantUtf8) c.getConstantPool().getConstant(memberNameIndex, Const.CONSTANT_Utf8)).getBytes() + " : " + ((ConstantUtf8) c.getConstantPool().getConstant(signatureIndex, Const.CONSTANT_Utf8)).getBytes(); }
@Override public void visitClassContext(ClassContext classContext) { JavaClass jclass = classContext.getJavaClass(); // We can ignore classes that were compiled for anything // less than JDK 1.5. This should avoid lots of unnecessary work // when analyzing code for older VM targets. if (BCELUtil.preTiger(jclass)) { return; } boolean sawUtilConcurrentLocks = false; for (Constant c : jclass.getConstantPool().getConstantPool()) { if (c instanceof ConstantMethodref) { ConstantMethodref m = (ConstantMethodref) c; ConstantClass cl = (ConstantClass) jclass.getConstantPool().getConstant(m.getClassIndex()); ConstantUtf8 name = (ConstantUtf8) jclass.getConstantPool().getConstant(cl.getNameIndex()); String nameAsString = name.getBytes(); if (nameAsString.startsWith("java/util/concurrent/locks")) { sawUtilConcurrentLocks = true; } } } if (sawUtilConcurrentLocks) { super.visitClassContext(classContext); } }
private String getStringFromIndex(DismantleBytecode dbc, int i) { ConstantUtf8 name = (ConstantUtf8) dbc.getConstantPool().getConstant(i); return name.getBytes(); }
ConstantCP m = (ConstantCP) c; @DottedClassName String clazz = m.getClass(constantPool); ConstantNameAndType nt = (ConstantNameAndType) constantPool.getConstant(m.getNameAndTypeIndex(), Const.CONSTANT_NameAndType); String name = nt.getName(constantPool); if ("setAttribute".equals(name) && "javax.servlet.http.HttpSession".equals(clazz) || ("writeObject".equals(name)
@Override public void visit(ConstantValue s) { if (!visitingField()) { return; } int i = s.getConstantValueIndex(); Constant c = getConstantPool().getConstant(i); if (c instanceof ConstantString) { String value = ((ConstantString) c).getBytes(getConstantPool()); if (value.length() < SIZE_OF_HUGE_CONSTANT) { return; } String key = getStringKey(value); definition.put(key, XFactory.createXField(this)); stringSize.put(key, value.length()); } }
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();
public static @CheckForNull CodeException getSurroundingTryBlock(ConstantPool constantPool, Code code, @CheckForNull String vmNameOfExceptionClass, int pc) { int size = Integer.MAX_VALUE; if (code.getExceptionTable() == null) { return null; } CodeException result = null; for (CodeException catchBlock : code.getExceptionTable()) { if (vmNameOfExceptionClass != null) { Constant catchType = constantPool.getConstant(catchBlock.getCatchType()); if (catchType == null && !vmNameOfExceptionClass.isEmpty() || catchType instanceof ConstantClass && !((ConstantClass) catchType).getBytes(constantPool).equals(vmNameOfExceptionClass)) { continue; } } int startPC = catchBlock.getStartPC(); int endPC = catchBlock.getEndPC(); if (pc >= startPC && pc <= endPC) { int thisSize = endPC - startPC; if (size > thisSize) { size = thisSize; result = catchBlock; } } } return result; }
/** * 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); } }
public static String getExceptionSig(DismantleBytecode dbc, CodeException e) { if (e.getCatchType() == 0) { return "Ljava/lang/Throwable;"; } Constant c = dbc.getConstantPool().getConstant(e.getCatchType()); if (c instanceof ConstantClass) { return "L" + ((ConstantClass) c).getBytes(dbc.getConstantPool()) + ";"; } return "Ljava/lang/Throwable;"; }
@Override public void visit(CodeException obj) { int type = obj.getCatchType(); if (type == 0) { return; } String name = getConstantPool().constantToString(getConstantPool().getConstant(type)); if (DEBUG) { String msg = "Catching " + name + " in " + getFullyQualifiedMethodName(); if (msgs.add(msg)) { System.out.println(msg); } } if ("java.lang.IllegalMonitorStateException".equals(name)) { bugReporter.reportBug(new BugInstance(this, "IMSE_DONT_CATCH_IMSE", HIGH_PRIORITY).addClassAndMethod(this) .addSourceLine(this.classContext, this, obj.getHandlerPC())); } }
public void setupVisitorForClass(JavaClass obj) { constantPool = obj.getConstantPool(); thisClass = obj; ConstantClass c = (ConstantClass) constantPool.getConstant(obj.getClassNameIndex()); className = getStringFromIndex(c.getNameIndex()); dottedClassName = className.replace('/', '.'); packageName = obj.getPackageName(); sourceFile = obj.getSourceFileName(); dottedSuperclassName = obj.getSuperclassName(); superclassName = dottedSuperclassName.replace('.', '/'); ClassDescriptor cDesc = DescriptorFactory.createClassDescriptor(className); if (!FindBugs.isNoAnalysis()) { try { thisClassInfo = (ClassInfo) Global.getAnalysisCache().getClassAnalysis(XClass.class, cDesc); } catch (CheckedAnalysisException e) { throw new AssertionError("Can't find ClassInfo for " + cDesc); } } super.visitJavaClass(obj); }
ConstantPool cp = getConstantPool(); int nameAndTypeIdx = fieldRef.getNameAndTypeIndex(); ConstantNameAndType ntc = (ConstantNameAndType) cp.getConstant(nameAndTypeIdx); int nameIdx = ntc.getNameIndex(); if (fld.getNameIndex() == nameIdx) { if (!fld.isStatic()) { ConstantUtf8 nameCons = (ConstantUtf8) cp.getConstant(nameIdx); ConstantUtf8 typeCons = (ConstantUtf8) cp.getConstant(ntc.getSignatureIndex());
/** * Determine the outer class of obj. * * @param obj * @return JavaClass for outer class, or null if obj is not an outer class * @throws ClassNotFoundException */ @CheckForNull public static JavaClass getOuterClass(JavaClass obj) throws ClassNotFoundException { for (Attribute a : obj.getAttributes()) { if (a instanceof InnerClasses) { for (InnerClass ic : ((InnerClasses) a).getInnerClasses()) { if (obj.getClassNameIndex() == ic.getInnerClassIndex()) { // System.out.println("Outer class is " + // ic.getOuterClassIndex()); ConstantClass oc = (ConstantClass) obj.getConstantPool().getConstant(ic.getOuterClassIndex()); String ocName = oc.getBytes(obj.getConstantPool()); return Repository.lookupClass(ocName); } } } } return null; }
case Const.ITEM_Object: int index = t.getIndex(); ConstantClass c = (ConstantClass) t.getConstantPool().getConstant(index); String name = c.getBytes(t.getConstantPool()); if (name.charAt(0) != '[') {
@Override public void visit(Field field) { ConstantValue value = field.getConstantValue(); if (value == null) { return; } Constant c = getConstantPool().getConstant(value.getConstantValueIndex()); if (testingEnabled && c instanceof ConstantLong && ((ConstantLong)c).getBytes() == MICROS_PER_DAY_OVERFLOWED_AS_INT) { bugReporter.reportBug( new BugInstance(this, "TESTING", HIGH_PRIORITY).addClass(this).addField(this) .addString("Did you mean MICROS_PER_DAY") .addInt(MICROS_PER_DAY_OVERFLOWED_AS_INT) .describe(IntAnnotation.INT_VALUE)); } } @Override
return; String name = getConstantPool().constantToString(getConstantPool().getConstant(type));