本文整理汇总了Java中org.apache.bcel.classfile.ConstantPool类的典型用法代码示例。如果您正苦于以下问题:Java ConstantPool类的具体用法?Java ConstantPool怎么用?Java ConstantPool使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ConstantPool类属于org.apache.bcel.classfile包,在下文中一共展示了ConstantPool类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: visitConstantMethodref
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
public void visitConstantMethodref(ConstantMethodref ref)
{
ConstantPool pool = javaClass.getConstantPool();
String cstr = ref.getClass(pool);
if (cstr.equals("java.lang.Class"))
{
int iname = ref.getNameAndTypeIndex();
String name = ((ConstantNameAndType)pool.getConstant(iname)).getName(pool);
if (name.equals("forName")) {
System.out.println("found Class.forName('" + javaClass.getClassName() + "')");
ConstantNameAndType cnat = (ConstantNameAndType)pool.getConstant(iname);
String cfnStr = cnat.getName(pool);
if (lastConst != null) {
refClasses.add(lastConst.replace('.', '/'));
lastConst = null;
}
}
}
}
示例2: processConstant
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
private void processConstant(ConstantPool pool, Constant c, int i) {
if (c == null) // don't know why, but constant[0] seems to be always
// null.
return;
log(" const[" + i + "]=" + pool.constantToString(c) + " inst=" + c.getClass().getName(),
Project.MSG_DEBUG);
byte tag = c.getTag();
switch (tag) {
// reverse engineered from ConstantPool.constantToString..
case Constants.CONSTANT_Class:
int ind = ((ConstantClass) c).getNameIndex();
c = pool.getConstant(ind, Constants.CONSTANT_Utf8);
String className = Utility.compactClassName(((ConstantUtf8) c).getBytes(), false);
log(" classNamePre=" + className, Project.MSG_DEBUG);
className = getRidOfArray(className);
String firstLetter = className.charAt(0) + "";
if (primitives.contains(firstLetter))
return;
log(" className=" + className, Project.MSG_VERBOSE);
design.checkClass(className);
break;
default:
}
}
示例3: createDependencyList
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
/**
* Create a list containing all referenced classes.
*
* @return the list of all classes the given class depends on.
*/
private List<String> createDependencyList(JavaClass clazz) {
Set<String> result = new HashSet<String>();
ConstantPool cp = clazz.getConstantPool();
String className = clazz.getClassName();
for (Constant c : cp.getConstantPool()) {
if (c instanceof ConstantClass) {
String usedClassName = cp.constantToString(c);
usedClassName = JavaLibrary
.ignoreArtificialPrefix(usedClassName);
if (!usedClassName.equals("") && !isBlacklisted(usedClassName)
&& !className.equals(usedClassName)) {
result.add(usedClassName);
}
}
}
return new ArrayList<String>(result);
}
示例4: analyze
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
/**
* For each method in the given class, look at each statement; if it is a
* 'new' instruction, the class of the created object is added to the
* CREATION dependency list.
*/
@Override
protected void analyze(IJavaElement classElement, JavaClass clazz) {
ConstantPool cp = clazz.getConstantPool();
ConstantPoolGen cpg = new ConstantPoolGen(cp);
List<String> list = new ArrayList<String>();
for (Method m : clazz.getMethods()) {
Code c = m.getCode();
if (c != null) {
// not an abstract method
handleCodeFragment(list, cpg, c);
}
}
classElement.setValue(CREATION_KEY, list);
}
示例5: createDependencyList
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
/** Create a list of dependencies for a given class. */
private ArrayList<String> createDependencyList(JavaClass clazz) {
ArrayList<String> depList = new ArrayList<String>();
ConstantPool cp = clazz.getConstantPool();
String className = clazz.getClassName();
for (Constant constant : cp.getConstantPool()) {
if (constant instanceof ConstantClass) {
String usedClassName = cp.constantToString(constant);
// don't include self-reference
if (!className.equals(usedClassName)) {
depList.add(usedClassName);
}
}
}
return depList;
}
示例6: getSurroundingTryBlock
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
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 || 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;
}
示例7: visit
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
@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 (className.equals("java/util/Calendar") || className.equals("java/text/DateFormat")) {
sawDateClass = true;
break;
}
try {
ClassDescriptor cDesc = DescriptorFactory.createClassDescriptor(className);
if (subtypes2.isSubtype(cDesc, calendarType) || subtypes2.isSubtype(cDesc, dateFormatType)) {
sawDateClass = true;
break;
}
} catch (ClassNotFoundException e) {
reporter.reportMissingClass(e);
}
}
}
}
示例8: isCaught
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
/**
* @param classContext
* @param method
* @param entryFact
* @param paramVN
* @return
*/
public boolean isCaught(ClassContext classContext, Method method, UnconditionalValueDerefSet entryFact, ValueNumber paramVN) {
boolean caught = true;
Set<Location> dereferenceSites
= entryFact.getDerefLocationSet(paramVN);
if (dereferenceSites != null && !dereferenceSites.isEmpty()) {
ConstantPool cp = classContext.getJavaClass().getConstantPool();
for(Location loc : dereferenceSites) {
if (!FindNullDeref.catchesNull(cp, method.getCode(), loc))
caught = false;
}
}
return caught;
}
示例9: safeCallToPrimateParseMethod
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
private boolean safeCallToPrimateParseMethod(XMethod calledMethod, Location location) {
int position = location.getHandle().getPosition();
if (calledMethod.getClassName().equals("java.lang.Integer")) {
ConstantPool constantPool = classContext.getJavaClass().getConstantPool();
Code code = method.getCode();
int catchSize;
catchSize = Util.getSizeOfSurroundingTryBlock(constantPool, code, "java/lang/NumberFormatException", position);
if (catchSize < Integer.MAX_VALUE)
return true;
catchSize = Util.getSizeOfSurroundingTryBlock(constantPool, code, "java/lang/IllegalArgumentException", position);
if (catchSize < Integer.MAX_VALUE)
return true;
catchSize = Util.getSizeOfSurroundingTryBlock(constantPool, code, "java/lang/RuntimeException", position);
if (catchSize < Integer.MAX_VALUE)
return true;
catchSize = Util.getSizeOfSurroundingTryBlock(constantPool, code, "java/lang/Exception", position);
if (catchSize < Integer.MAX_VALUE)
return true;
}
return false;
}
示例10: setField
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
/**
* 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);
}
}
示例11: setField
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
/**
* 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);
}
}
示例12: main
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
/**
* Main class, to find strings in class files.
* @param args Arguments to program.
*/
public static void main(String[] args) {
try {
JavaClass clazz = Repository.lookupClass(args[0]);
ConstantPool cp = clazz.getConstantPool();
Constant[] consts = cp.getConstantPool();
for (int i = 0; i < consts.length; i++) {
if (consts[i] instanceof ConstantString) {
System.out.println("Found String: " +
((ConstantString)consts[i]).getBytes(cp));
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
示例13: createClass
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
/**
* Override this method to create you own classes on the fly. The
* name contains the special token $$BCEL$$. Everything before that
* token is consddered to be a package name. You can encode you own
* arguments into the subsequent string. You must regard however not
* to use any "illegal" characters, i.e., characters that may not
* appear in a Java class name too<br>
*
* The default implementation interprets the string as a encoded compressed
* Java class, unpacks and decodes it with the Utility.decode() method, and
* parses the resulting byte array and returns the resulting JavaClass object.
*
* @param class_name compressed byte code with "$$BCEL$$" in it
*/
protected JavaClass createClass( String class_name ) {
int index = class_name.indexOf("$$BCEL$$");
String real_name = class_name.substring(index + 8);
JavaClass clazz = null;
try {
byte[] bytes = Utility.decode(real_name, true);
ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes), "foo");
clazz = parser.parse();
} catch (Throwable e) {
e.printStackTrace();
return null;
}
// Adapt the class name to the passed value
ConstantPool cp = clazz.getConstantPool();
ConstantClass cl = (ConstantClass) cp.getConstant(clazz.getClassNameIndex(),
Constants.CONSTANT_Class);
ConstantUtf8 name = (ConstantUtf8) cp.getConstant(cl.getNameIndex(),
Constants.CONSTANT_Utf8);
name.setBytes(class_name.replace('.', '/'));
return clazz;
}
示例14: ConstantHTML
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
ConstantHTML(String dir, String class_name, String class_package, Method[] methods,
ConstantPool constant_pool) throws IOException {
this.class_name = class_name;
this.class_package = class_package;
this.constant_pool = constant_pool;
this.methods = methods;
constants = constant_pool.getConstantPool();
file = new PrintWriter(new FileOutputStream(dir + class_name + "_cp.html"));
constant_ref = new String[constants.length];
constant_ref[0] = "<unknown>";
file.println("<HTML><BODY BGCOLOR=\"#C0C0C0\"><TABLE BORDER=0>");
// Loop through constants, constants[0] is reserved
for (int i = 1; i < constants.length; i++) {
if (i % 2 == 0) {
file.print("<TR BGCOLOR=\"#C0C0C0\"><TD>");
} else {
file.print("<TR BGCOLOR=\"#A0A0A0\"><TD>");
}
if (constants[i] != null) {
writeConstant(i);
}
file.print("</TD></TR>\n");
}
file.println("</TABLE></BODY></HTML>");
file.close();
}
示例15: flagTests
import org.apache.bcel.classfile.ConstantPool; //导入依赖的package包/类
public void flagTests(HashSet<String> tests) {
if (true == _clazz.isAbstract()) {
return;
}
ConstantPool pool = _clazz.getConstantPool();
for (int i = 0; i < pool.getLength(); ++i) {
Constant constant = pool.getConstant(i);
if (null == constant) {
continue;
}
Byte tag = constant.getTag();
if (Constants.CONSTANT_Utf8 != tag) {
continue;
}
String constantAsString = pool.constantToString(constant);
if (constantAsString.contains("Lorg/junit/Test")) {
tests.add(_clazz.getClassName());
}
}
}