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; }
@Override public void visitConstantPool(ConstantPool obj) { super.visitConstantPool(obj); Constant[] constant_pool = obj.getConstantPool(); for (int i = 1; i < constant_pool.length; i++) { constant_pool[i].accept(this); byte tag = constant_pool[i].getTag(); if ((tag == Const.CONSTANT_Double) || (tag == Const.CONSTANT_Long)) { i++; } } }
@Override public void visit(ConstantPool pool) { for (Constant constant : pool.getConstantPool()) { if (constant instanceof ConstantClass) { ConstantClass cc = (ConstantClass) constant; @SlashedClassName String className = cc.getBytes(pool); if ("java/util/Calendar".equals(className) || "java/text/DateFormat".equals(className)) { sawDateClass = true; break; } if (className.charAt(0) != '[') { try { ClassDescriptor cDesc = DescriptorFactory.createClassDescriptor(className); if (subtypes2.isSubtype(cDesc, calendarType) || subtypes2.isSubtype(cDesc, dateFormatType)) { sawDateClass = true; break; } } catch (ClassNotFoundException e) { reporter.reportMissingClass(e); } } } } }
private boolean hasInterestingConstant(ConstantPool cp) { for(Constant constant : cp.getConstantPool()) { if(constant instanceof ConstantFloat) { float val = ((ConstantFloat)constant).getBytes(); if(isInteresting(val, val)) { return true; } } if(constant instanceof ConstantDouble) { double val = ((ConstantDouble)constant).getBytes(); if(isInteresting(val, val)) { return true; } } } return false; }
@Override public void visitClassContext(ClassContext classContext) { JavaClass javaClass = classContext.getJavaClass(); if (!javaClass.getPackageName().contains(".pages")) { return; } //The package contains ".pages" and has some references to tapestry // then it must be an endpoint. //The constants pool contains all references that are reused in the bytecode // including full class name and interface name. if (javaClass.getPackageName().contains(".pages")) { ConstantPool constants = javaClass.getConstantPool(); for (Constant c : constants.getConstantPool()) { if (c instanceof ConstantUtf8) { ConstantUtf8 utf8 = (ConstantUtf8) c; String constantValue = String.valueOf(utf8.getBytes()); if (constantValue.startsWith("Lorg/apache/tapestry5/annotations")) { bugReporter.reportBug(new BugInstance(this, TAPESTRY_ENDPOINT_TYPE, Priorities.LOW_PRIORITY) // .addClass(javaClass)); return; } } } } }
boolean skip = false; ConstantPool constantPool = javaClass.getConstantPool(); for(Constant c : constantPool.getConstantPool() ) { if (c instanceof ConstantMethodref || c instanceof ConstantInterfaceMethodref) { ConstantCP m = (ConstantCP) c;
for(Constant constant : cp.getConstantPool()) { if(constant instanceof ConstantMethodHandle) { int kind = ((ConstantMethodHandle) constant).getReferenceKind();
/** * 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; }
@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); } }
ConstantPool pool = javaClass.getConstantPool(); boolean found = false; for (Constant constantEntry : pool.getConstantPool()) { if (constantEntry instanceof ConstantNameAndType) { ConstantNameAndType nt = (ConstantNameAndType) constantEntry;
@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"); } }
compute(); ConstantPool cp = obj.getConstantPool(); Constant[] constants = cp.getConstantPool(); checkConstant: for (int i = 0; i < constants.length; i++) { Constant co = constants[i];
/** * Initialize with given constant pool. */ public ConstantPoolGen(ConstantPool cp) { this(cp.getConstantPool()); }
/** * Initialize with given constant pool. */ public ConstantPoolGen(final ConstantPool cp) { this(cp.getConstantPool()); }
public void visitConstantPool(ConstantPool cp) { stack.push(cp); cp.accept(visitor); Constant[] constants = cp.getConstantPool(); for(int i=1; i < constants.length; i++) { if(constants[i] != null) constants[i].accept(this); } stack.pop(); }
@Override public void visitConstantPool(final ConstantPool cp) { stack.push(cp); cp.accept(visitor); final Constant[] constants = cp.getConstantPool(); for (int i = 1; i < constants.length; i++) { if (constants[i] != null) { constants[i].accept(this); } } stack.pop(); }
ClassParser cp = new ClassParser("TestClass.class"); JavaClass jc = cp.parse(); ConstantPool constantPool = jc.getConstantPool(); // Get the constant pool here. for (Constant c : constantPool.getConstantPool()) { System.out.println(c); // Do what you need to do with all the constants. }
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; }
@Override public void visitConstantPool(ConstantPool obj) { super.visitConstantPool(obj); Constant[] constant_pool = obj.getConstantPool(); for (int i = 1; i < constant_pool.length; i++) { constant_pool[i].accept(this); byte tag = constant_pool[i].getTag(); if ((tag == CONSTANT_Double) || (tag == CONSTANT_Long)) { i++; } } }
private boolean hasInterestingConstant(ConstantPool cp) { for(Constant constant : cp.getConstantPool()) { if(constant instanceof ConstantFloat) { float val = ((ConstantFloat)constant).getBytes(); if(isInteresting(val, val)) { return true; } } if(constant instanceof ConstantDouble) { double val = ((ConstantDouble)constant).getBytes(); if(isInteresting(val, val)) { return true; } } } return false; }