Refine search
JavaClass javaClass = classContext.getJavaClass(); boolean skip = false; ConstantPool constantPool = javaClass.getConstantPool(); for(Constant c : constantPool.getConstantPool() ) { if (c instanceof ConstantMethodref || c instanceof ConstantInterfaceMethodref) { 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) && ("java.io.ObjectOutput".equals(clazz) System.out.println(this.getClass().getSimpleName() + " Checking " + javaClass.getClassName()); Method[] methodList = javaClass.getMethods();
calledMethods = new HashSet<>(); calledMethodNames = new HashSet<>(); className = classContext.getJavaClass().getClassName(); String[] parts = className.split("[$+.]"); String simpleClassName = parts[parts.length - 1]; ConstantPool cp = classContext.getJavaClass().getConstantPool(); for(Constant constant : cp.getConstantPool()) { if(constant instanceof ConstantMethodHandle) { int kind = ((ConstantMethodHandle) constant).getReferenceKind(); if(kind >= 5 && kind <= 9) { Constant ref = cp.getConstant(((ConstantMethodHandle)constant).getReferenceIndex()); if(ref instanceof ConstantCP) { String className = cp.getConstantString(((ConstantCP) ref).getClassIndex(), Const.CONSTANT_Class); ConstantNameAndType nameAndType = (ConstantNameAndType) cp.getConstant(((ConstantCP) ref).getNameAndTypeIndex()); String name = ((ConstantUtf8)cp.getConstant(nameAndType.getNameIndex())).getBytes(); String signature = ((ConstantUtf8)cp.getConstant(nameAndType.getSignatureIndex())).getBytes(); MethodAnnotation called = new MethodAnnotation(ClassName.toDottedClassName(className), name, signature, kind==6 /* invokestatic */); calledMethods.add(called);
/** * 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); } }
@Override public void visit(ConstantNameAndType obj) { String methodName = obj.getName(getConstantPool()); String methodSig = obj.getSignature(getConstantPool()); if (!"clone".equals(methodName)) { return; } if (!methodSig.startsWith("()")) { return; } referencesCloneMethod = true; }
private void init(JavaClass jclass) { ConstantPool cp = jclass.getConstantPool(); int numConstants = cp.getLength(); for (int i = 0; i < numConstants; ++i) { try { Constant c = cp.getConstant(i); if (c instanceof ConstantMethodref) { ConstantMethodref cmr = (ConstantMethodref) c; ConstantNameAndType cnat = (ConstantNameAndType) cp.getConstant(cmr.getNameAndTypeIndex(), Const.CONSTANT_NameAndType); String methodName = ((ConstantUtf8) cp.getConstant(cnat.getNameIndex(), Const.CONSTANT_Utf8)).getBytes(); String className = cp.getConstantString(cmr.getClassIndex(), Const.CONSTANT_Class).replace('/', '.'); String methodSig = ((ConstantUtf8) cp.getConstant(cnat.getSignatureIndex(), Const.CONSTANT_Utf8)).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; }
@Nullable private String getAnonymousName(ConstantMethodHandle cmh) { if ((cmh == null) || (cmh.getReferenceKind() != REF_invokeStatic)) { return null; } ConstantPool cp = getConstantPool(); ConstantCP methodRef = (ConstantCP) cp.getConstant(cmh.getReferenceIndex()); String clsName = methodRef.getClass(cp); if (!clsName.equals(cls.getClassName())) { return null; } ConstantNameAndType nameAndType = (ConstantNameAndType) cp.getConstant(methodRef.getNameAndTypeIndex()); String signature = nameAndType.getSignature(cp); if (signature.endsWith("V")) { return null; } String methodName = nameAndType.getName(cp); if (!isSynthetic(methodName, nameAndType.getSignature(cp))) { return null; } return methodName; }
/** @return signature of referenced method/field. */ public String getSignature(ConstantPoolGen cpg) { ConstantPool cp = cpg.getConstantPool(); ConstantCP cmr = (ConstantCP)cp.getConstant(index); ConstantNameAndType cnat = (ConstantNameAndType)cp.getConstant(cmr.getNameAndTypeIndex()); return ((ConstantUtf8)cp.getConstant(cnat.getSignatureIndex())).getBytes(); }
private String getFieldName(ConstantFieldref fieldRef) { ConstantNameAndType nameAndType= (ConstantNameAndType) constantPool.getConstant(fieldRef.getNameAndTypeIndex()); return nameAndType.getName(constantPool); }
case Constants.CONSTANT_Class: i = ((ConstantClass)c).getNameIndex(); c = getConstant(i, Constants.CONSTANT_Utf8); str = Utility.compactClassName(((ConstantUtf8)c).getBytes(), false); break; c = getConstant(i, Constants.CONSTANT_Utf8); str = "\"" + escape(((ConstantUtf8)c).getBytes()) + "\""; break; case Constants.CONSTANT_Utf8: str = ((ConstantUtf8)c).getBytes(); break; case Constants.CONSTANT_Double: str = "" + ((ConstantDouble)c).getBytes(); break; case Constants.CONSTANT_Float: str = "" + ((ConstantFloat)c).getBytes(); break; str = (constantToString(((ConstantNameAndType)c).getNameIndex(), Constants.CONSTANT_Utf8) + " " + constantToString(((ConstantNameAndType)c).getSignatureIndex(), Constants.CONSTANT_Utf8)); break;
/** @return name of referenced method/field. */ public String getName(ConstantPoolGen cpg) { ConstantPool cp = cpg.getConstantPool(); ConstantCP cmr = (ConstantCP)cp.getConstant(index); ConstantNameAndType cnat = (ConstantNameAndType)cp.getConstant(cmr.getNameAndTypeIndex()); return ((ConstantUtf8)cp.getConstant(cnat.getNameIndex())).getBytes(); }
final Constant[] constants = cp.getConstantPool().getConstantPool(); switch (c.getTag()) { case Const.CONSTANT_String: { final ConstantString s = (ConstantString) c; final ConstantUtf8 u8 = (ConstantUtf8) constants[s.getStringIndex()]; return addString(u8.getBytes()); return addClass(u8.getBytes()); final ConstantUtf8 u8 = (ConstantUtf8) constants[n.getNameIndex()]; final ConstantUtf8 u8_2 = (ConstantUtf8) constants[n.getSignatureIndex()]; return addNameAndType(u8.getBytes(), u8_2.getBytes()); ConstantUtf8 u8 = (ConstantUtf8) constants[clazz.getNameIndex()]; final String class_name = u8.getBytes().replace('/', '.'); u8 = (ConstantUtf8) constants[n.getNameIndex()]; final String name = u8.getBytes(); u8 = (ConstantUtf8) constants[n.getSignatureIndex()]; final String signature = u8.getBytes(); switch (c.getTag()) {
@Override public void visitClass(ClassDescriptor classDescriptor) throws CheckedAnalysisException { IAnalysisCache analysisCache = Global.getAnalysisCache(); ObligationFactory factory = database.getFactory(); JavaClass jclass = analysisCache.getClassAnalysis(JavaClass.class, classDescriptor); for (Constant c : jclass.getConstantPool().getConstantPool()) { if (c instanceof ConstantNameAndType) { ConstantNameAndType cnt = (ConstantNameAndType) c; String signature = cnt.getSignature(jclass.getConstantPool()); if (factory.signatureInvolvesObligations(signature)) { super.visitClass(classDescriptor); return; } } else if (c instanceof ConstantClass) { String className = ((ConstantClass) c).getBytes(jclass.getConstantPool()); if (factory.signatureInvolvesObligations(className)) { super.visitClass(classDescriptor); return; } } } if (DEBUG) { System.out.println(classDescriptor + " isn't interesting for obligation analysis"); } }
ConstantMethodref c = (ConstantMethodref)constant_pool.getConstant(index, CONSTANT_Methodref); class_index = c.getClassIndex(); name_index = c.getNameAndTypeIndex(); ConstantInterfaceMethodref c1 = (ConstantInterfaceMethodref)constant_pool.getConstant(index, CONSTANT_InterfaceMethodref); class_index = c1.getClassIndex(); name_index = c1.getNameAndTypeIndex(); String method_name = constant_pool.constantToString(name_index, CONSTANT_NameAndType); String html_method_name = Class2HTML.toHTML(method_name); ConstantNameAndType c2 = (ConstantNameAndType)constant_pool.getConstant(name_index, CONSTANT_NameAndType); String signature = constant_pool.constantToString(c2.getSignatureIndex(), CONSTANT_Utf8); ConstantNameAndType c6 = (ConstantNameAndType)constant_pool.getConstant(index, CONSTANT_NameAndType); name_index = c6.getNameIndex(); int signature_index = c6.getSignatureIndex(); file.println("<P><TT>" + Class2HTML.toHTML(constant_pool.constantToString(index, tag)) + "</TT><UL>" +
case Const.PUTSTATIC: index = bytes.readShort(); final ConstantFieldref c1 = (ConstantFieldref) constant_pool.getConstant(index, Const.CONSTANT_Fieldref); class_index = c1.getClassIndex(); name = constant_pool.getConstantString(class_index, Const.CONSTANT_Class); name = Utility.compactClassName(name, false); index = c1.getNameAndTypeIndex(); final String field_name = constant_pool.constantToString(index, Const.CONSTANT_NameAndType); if (name.equals(class_name)) { // Local field buf.append("<A HREF=\"").append(class_name).append("_methods.html#field") final ConstantNameAndType c2 = (ConstantNameAndType) constant_pool.getConstant(index, Const.CONSTANT_NameAndType); signature = constant_pool.constantToString(c2.getSignatureIndex(), Const.CONSTANT_Utf8); final String[] args = Utility.methodSignatureArgumentTypes(signature, false); final String type = Utility.methodSignatureReturnType(signature, false);
ConstantUtf8 u8 = (ConstantUtf8)constants[s.getStringIndex()]; string_table.put(u8.getBytes(), new Index(i)); } else if(c instanceof ConstantClass) { ConstantClass s = (ConstantClass)c; ConstantUtf8 u8 = (ConstantUtf8)constants[s.getNameIndex()]; class_table.put(u8.getBytes(), new Index(i)); } else if(c instanceof ConstantNameAndType) { ConstantNameAndType n = (ConstantNameAndType)c; ConstantUtf8 u8 = (ConstantUtf8)constants[n.getNameIndex()]; ConstantUtf8 u8_2 = (ConstantUtf8)constants[n.getSignatureIndex()]; n_a_t_table.put(u8.getBytes() + NAT_DELIM + u8_2.getBytes(), new Index(i)); } else if(c instanceof ConstantUtf8) { ConstantUtf8 u = (ConstantUtf8)c; String class_name = u8.getBytes().replace('/', '.'); u8 = (ConstantUtf8)constants[n.getNameIndex()]; String method_name = u8.getBytes(); u8 = (ConstantUtf8)constants[n.getSignatureIndex()]; String signature = u8.getBytes();
break; case M_CP: constantRefOperand = getConstantPool().getConstant(v); if (constantRefOperand instanceof ConstantClass) { ConstantClass clazz = (ConstantClass) constantRefOperand; } else if (constantRefOperand instanceof ConstantInvokeDynamic) { ConstantInvokeDynamic id = (ConstantInvokeDynamic) constantRefOperand; ConstantNameAndType sig = (ConstantNameAndType) getConstantPool().getConstant( id.getNameAndTypeIndex()); nameConstantOperand = getStringFromIndex(sig.getNameIndex()); sigConstantOperand = getStringFromIndex(sig.getSignatureIndex()); } else if (constantRefOperand instanceof ConstantCP) { ConstantCP cp = (ConstantCP) constantRefOperand; ConstantClass clazz = (ConstantClass) getConstantPool().getConstant(cp.getClassIndex()); classConstantOperand = getStringFromIndex(clazz.getNameIndex()); referencedClass = DescriptorFactory.createClassDescriptor(classConstantOperand); ConstantNameAndType sig = (ConstantNameAndType) getConstantPool().getConstant( cp.getNameAndTypeIndex()); nameConstantOperand = getStringFromIndex(sig.getNameIndex()); sigConstantOperand = getStringFromIndex(sig.getSignatureIndex()); refConstantOperand = null;
public static MethodBinding lookup(int index, ConstantPool constantPool) { ConstantCP methodRef= (ConstantCP) constantPool.getConstant(index); ConstantNameAndType nameAndType= (ConstantNameAndType) constantPool.getConstant(methodRef.getNameAndTypeIndex(), Constants.CONSTANT_NameAndType); String name= nameAndType.getName(constantPool); String signature= nameAndType.getSignature(constantPool); return lookup(methodRef.getClass(constantPool), name, signature); }
@Override public void visit(ConstantNameAndType obj) { String signature = obj.getSignature(javaClass.getConstantPool()); ClassParser.extractReferencedClassesFromSignature(referencedClassSet, signature); } });
/** * Verify for an inner class declared inside the 'main' method that the * enclosing method attribute is set correctly. */ public void testCheckMethodLevelNamedInnerClass() throws ClassNotFoundException { final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.AttributeTestClassEM01$1S"); final ConstantPool pool = clazz.getConstantPool(); final Attribute[] encMethodAttrs = findAttribute("EnclosingMethod", clazz); assertTrue("Expected 1 EnclosingMethod attribute but found " + encMethodAttrs.length, encMethodAttrs.length == 1); final EnclosingMethod em = (EnclosingMethod) encMethodAttrs[0]; final String enclosingClassName = em.getEnclosingClass().getBytes(pool); final String enclosingMethodName = em.getEnclosingMethod().getName(pool); assertTrue( "Expected class name to be '"+PACKAGE_BASE_SIG+"/data/AttributeTestClassEM01' but was " + enclosingClassName, enclosingClassName .equals(PACKAGE_BASE_SIG+"/data/AttributeTestClassEM01")); assertTrue("Expected method name to be 'main' but was " + enclosingMethodName, enclosingMethodName.equals("main")); }