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 visit(ConstantClass obj) { @SlashedClassName String className = obj.getBytes(javaClass.getConstantPool()); if (className.indexOf('[') >= 0) { ClassParser.extractReferencedClassesFromSignature(referencedClassSet, className); } else if (ClassName.isValidClassName(className)) { referencedClassSet.add(DescriptorFactory.instance().getClassDescriptor(className)); } }
private void pushByConstant(DismantleBytecode dbc, Constant c) { if (c instanceof ConstantClass) { push(new Item("Ljava/lang/Class;", ((ConstantClass) c).getConstantValue(dbc.getConstantPool()))); } else if (c instanceof ConstantInteger) { push(new Item("I", Integer.valueOf(((ConstantInteger) c).getBytes()))); } else if (c instanceof ConstantString) { int s = ((ConstantString) c).getStringIndex(); push(new Item("Ljava/lang/String;", getStringFromIndex(dbc, s))); } else if (c instanceof ConstantFloat) { push(new Item("F", Float.valueOf(((ConstantFloat) c).getBytes()))); } else if (c instanceof ConstantDouble) { push(new Item("D", Double.valueOf(((ConstantDouble) c).getBytes()))); } else if (c instanceof ConstantLong) { push(new Item("J", Long.valueOf(((ConstantLong) c).getBytes()))); } else { throw new UnsupportedOperationException("StaticConstant type not expected"); } }
@Override public void visitConstantClass(final ConstantClass obj) { if (obj.getTag() != Const.CONSTANT_Class) { throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'."); } checkIndex(obj, obj.getNameIndex(), CONST_Utf8); } @Override
private int addClass_( final String clazz ) { int ret; if ((ret = lookupClass(clazz)) != -1) { return ret; // Already in CP } adjustSize(); final ConstantClass c = new ConstantClass(addUtf8(clazz)); ret = index; constants[index++] = c; if (!class_table.containsKey(clazz)) { class_table.put(clazz, new Index(ret)); } return ret; }
public void visitConstantClass(ConstantClass constant) { stack.push(constant); constant.accept(visitor); stack.pop(); }
public void visitConstantClass(ConstantClass obj){ if (obj.getTag() != Constants.CONSTANT_Class){ throw new ClassConstraintException("Wrong constant tag in '"+tostring(obj)+"'."); } checkIndex(obj, obj.getNameIndex(), CONST_Utf8); } public void visitConstantFieldref(ConstantFieldref obj){
private int addClass_(String clazz) { int ret; if((ret = lookupClass(clazz)) != -1) return ret; // Already in CP adjustSize(); ConstantClass c = new ConstantClass(addUtf8(clazz)); ret = index; constants[index++] = c; class_table.put(clazz, new Index(ret)); return ret; }
@Override public void visitConstantClass(final ConstantClass constant) { stack.push(constant); constant.accept(visitor); stack.pop(); }
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; }
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); }
Constant constant = cp.getConstant(catchTypeIndex); if (constant instanceof ConstantClass) { String exClassName = (String) ((ConstantClass) constant).getConstantValue(cp); if ("java/lang/OutOfMemoryError".equals(exClassName)) { outOfMemoryHandler = true;
/** * Read one constant from the given file, the type depends on a tag byte. * * @param file Input stream * @return Constant object */ static final Constant readConstant(DataInputStream file) throws IOException, ClassFormatException { byte b = file.readByte(); // Read tag byte switch(b) { case Constants.CONSTANT_Class: return new ConstantClass(file); case Constants.CONSTANT_Fieldref: return new ConstantFieldref(file); case Constants.CONSTANT_Methodref: return new ConstantMethodref(file); case Constants.CONSTANT_InterfaceMethodref: return new ConstantInterfaceMethodref(file); case Constants.CONSTANT_String: return new ConstantString(file); case Constants.CONSTANT_Integer: return new ConstantInteger(file); case Constants.CONSTANT_Float: return new ConstantFloat(file); case Constants.CONSTANT_Long: return new ConstantLong(file); case Constants.CONSTANT_Double: return new ConstantDouble(file); case Constants.CONSTANT_NameAndType: return new ConstantNameAndType(file); case Constants.CONSTANT_Utf8: return new ConstantUtf8(file); default: throw new ClassFormatException("Invalid byte tag in constant pool: " + b); } } }
@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); } } } } }
@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); } }
/** @return dereferenced string */ public String getBytes(ConstantPool cp) { return (String)getConstantValue(cp); }
switch (b) { case Const.CONSTANT_Class: return new ConstantClass(dataInput); case Const.CONSTANT_Fieldref: return new ConstantFieldref(dataInput);
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;"; }
if (constantRefOperand instanceof ConstantClass) { ConstantClass clazz = (ConstantClass) constantRefOperand; classConstantOperand = getStringFromIndex(clazz.getNameIndex()); referencedClass = DescriptorFactory.createClassDescriptor(classConstantOperand); ConstantCP cp = (ConstantCP) constantRefOperand; ConstantClass clazz = (ConstantClass) getConstantPool().getConstant(cp.getClassIndex()); classConstantOperand = getStringFromIndex(clazz.getNameIndex()); referencedClass = DescriptorFactory.createClassDescriptor(classConstantOperand); referencedXClass = null;
/** @return dereferenced string */ public String getBytes( final ConstantPool cp ) { return (String) getConstantValue(cp); }