本文整理匯總了Java中org.apache.bcel.classfile.ClassParser類的典型用法代碼示例。如果您正苦於以下問題:Java ClassParser類的具體用法?Java ClassParser怎麽用?Java ClassParser使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
ClassParser類屬於org.apache.bcel.classfile包,在下文中一共展示了ClassParser類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: fillFromJar
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
private ClassNode fillFromJar(File file) {
ClassNode rootNode = new ClassNode(file.getName());
try {
JarFile theJar = new JarFile(file);
Enumeration<? extends JarEntry> en = theJar.entries();
while (en.hasMoreElements()) {
JarEntry entry = en.nextElement();
if (entry.getName().endsWith(".class")) {
ClassParser cp = new ClassParser(
theJar.getInputStream(entry), entry.getName());
JavaClass jc = cp.parse();
ClassInfo classInfo = new ClassInfo(jc.getClassName(),
jc.getMethods().length);
rootNode.add(classInfo);
}
}
} catch (IOException e) {
System.err.println("Error reading file: " + file + ". " + e.getMessage());
e.printStackTrace(System.err);
}
return rootNode;
}
示例2: addClass
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
public void addClass(final InputStream classStream, final boolean doNotify) throws IOException {
final JavaClass jclazz = new ClassParser(classStream, null).parse();
String packageName = jclazz.getPackageName();
if (packageName == null || packageName.length() == 0) {
packageName = "<default>";
}
PackageItem packageItem = getPackageForName(packageName);
if (packageItem == null) {
packageItem = addPackage(packageName);
}
packageItem.addClass(new ClassItem(packageItem, jclazz));
if (doNotify) {
for (final TreeModelListener l : this.treeModelListeners) {
l.treeStructureChanged(new TreeModelEvent(this, new TreePath(this)));
}
}
}
示例3: downgrade
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
void downgrade(String filename) throws IOException
{
byte[] b = new byte[(int) new File(filename).length()];
InputStream in = new FileInputStream(filename);
new DataInputStream(in).readFully(b);
in.close();
ClassParser parser = new ClassParser(new ByteArrayInputStream(b), filename);
JavaClass jc = parser.parse();
boolean changed;
changed = downgrade(jc);
if (changed)
{
b = jc.getBytes();
FileOutputStream out = new FileOutputStream(filename);
out.write(b);
out.close();
}
}
示例4: printClass
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
private static void printClass(ClassParser parser) throws IOException {
JavaClass java_class;
java_class = parser.parse();
if (superClasses) {
try {
while (java_class != null) {
System.out.print(java_class.getClassName() + " ");
java_class = java_class.getSuperClass();
}
} catch (ClassNotFoundException e) {
System.out.println(e.getMessage());
}
System.out.println();
return;
}
if (constants || code)
System.out.println(java_class); // Dump the contents
if (constants) // Dump the constant pool ?
System.out.println(java_class.getConstantPool());
if (code) // Dump the method code ?
printCode(java_class.getMethods());
}
示例5: embeddedNameMismatch
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
private boolean embeddedNameMismatch(ZipFile zipInputFile, ZipEntry ze) throws IOException {
InputStream zipIn = zipInputFile.getInputStream(ze);
String name = ze.getName();
JavaClass j = new ClassParser(zipIn, name).parse();
zipIn.close();
String className = j.getClassName();
String computedFileName = ClassName.toSlashedClassName(className)+".class";
if (name.charAt(0) == '1') {
System.out.println(name);
System.out.println(" " + className);
}
if (computedFileName.equals(name))
return false;
System.out.println("In " + name + " found " + className);
return true;
}
示例6: analyze
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
private <T extends IPersistenceDAO> T analyze(Class<T> klass)
throws Exception {
DAOAnalysisSimple analysis = amap.get(klass);
T instance = null;
if (analysis == null) {
analysis = new DAOAnalysisSimple(klass);
analysis.setDebug(true);
amap.put(klass, analysis);
instance = klass.cast(createInstance(analysis));
JavaClass parse =
new ClassParser(analysis.getClassFile(), instance.getClass()
.getCanonicalName()).parse();
Repository.addClass(parse);
// new BCELifier(parse, System.out).start();
// Verifier.main(new String[] { parse.getClassName() });
} else {
instance = klass.cast(createInstance(analysis));
}
return instance;
}
示例7: loadClass
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
/**
* Lookup a JavaClass object from the Class Name provided.
*/
public JavaClass loadClass( String className ) throws ClassNotFoundException {
String classFile = className.replace('.', '/');
JavaClass RC = findClass(className);
if (RC != null) {
return RC;
}
try {
InputStream is = loader.getResourceAsStream(classFile + ".class");
if (is == null) {
throw new ClassNotFoundException(className + " not found.");
}
ClassParser parser = new ClassParser(is, className);
RC = parser.parse();
storeClass(RC);
return RC;
} catch (IOException e) {
throw new ClassNotFoundException(e.toString());
}
}
示例8: createClass
import org.apache.bcel.classfile.ClassParser; //導入依賴的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;
}
示例9: getJavaClassFromJar
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
/**
* Get JavaClass object list from a jar file.
*
* @param jarPath the jar file path that we get java classes from
* @return the java classes we get from jarPath
*/
private Vector<JavaClass> getJavaClassFromJar(final File jarPath) {
Vector<JavaClass> jarClasses = new Vector<JavaClass>();
if (!jarPath.getPath().endsWith(".jar")) {
return null;
}
try {
JarFile jarFile = new JarFile(jarPath);
Enumeration<JarEntry> entries = jarFile.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.endsWith(".class")) {
jarClasses.add(new ClassParser(
jarFile.getInputStream(entry),
name.substring(name.lastIndexOf('/') + 1)).parse());
}
}
} catch (IOException e) {
System.err.println("IO problem encountered "
+ "when trying to get classes from " + jarPath + ".");
e.printStackTrace();
}
return jarClasses;
}
示例10: iterateDirectory
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
private static void iterateDirectory(File directory, HashMap<String, HashSet<String>> dependencies,
HashSet<String> tests, boolean isTestDirectory) throws IOException, ClassNotFoundException {
if (false == directory.isDirectory()) {
throw new RuntimeException("passed directory is not directory");
}
for (File file : directory.listFiles()) {
if (file.isDirectory()) {
iterateDirectory(file, dependencies, tests, isTestDirectory);
} else if (file.getName().endsWith(".class")) {
ClassParser parser = new ClassParser(file.getAbsolutePath());
JavaClass javaClass = parser.parse();
DependencyVisitor dependencyVisitor = new DependencyVisitor(javaClass);
dependencyVisitor.addDependencies(dependencies);
if (isTestDirectory) {
TestVisitor testVisitor = new TestVisitor(javaClass);
testVisitor.flagTests(tests);
}
}
}
}
示例11: JavaClass
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
/**
* Read class definition from an input stream.
*
* @param filename
* the name of the class file (used to determine the class name)
* @param is
* the input stream to read the class file from
* @throws IOException
* if I/O exception occurs while reading from the input stream
*/
public JavaClass(String filename, InputStream is) throws IOException {
ClassParser parser = new ClassParser(is, filename);
org.apache.bcel.classfile.JavaClass clazz = parser.parse();
ConstantPool cp = clazz.getConstantPool();
name = clazz.getClassName();
for (Constant c : cp.getConstantPool()) {
if (c instanceof ConstantClass) {
ConstantClass cc = (ConstantClass) c;
ConstantUtf8 cs = (ConstantUtf8) cp.getConstant(cc.getNameIndex());
String cn = new String(cs.getBytes());
if (cn.contains("["))
continue;
cn = cn.replaceAll("^\\[L", "");
cn = cn.replaceAll(";", "");
cn = cn.replaceAll("/", ".");
getDependencies().add(cn);
}
}
}
示例12: loadClass
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
private JavaClass loadClass(InputStream is, String className)
throws ClassNotFoundException {
JavaClass clazz = findClass(className);
if (clazz != null) {
return clazz;
}
try {
if (is != null) {
ClassParser parser = new ClassParser(is, className);
clazz = parser.parse();
storeClass(clazz);
return clazz;
}
} catch (IOException e) {
throw new ClassNotFoundException("Exception while looking for class " +
className + ": " + e.toString());
}
throw new ClassNotFoundException("SyntheticRepository could not load " +
className);
}
示例13: updateEditPanes
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
private void updateEditPanes() {
internalFrame = (BrowserInternalFrame) services;
ClassFile classFile = services.getClassFile();
MethodInfo[] methods = classFile.getMethods();
String contents;
try {
JavaClass javaClass = new ClassParser(internalFrame.getFileName()).parse();
ClassGen cg = new ClassGen(javaClass);
ByteArrayOutputStream out = new ByteArrayOutputStream();
BCELifier v = new BCELifier(javaClass, out);
v.start();
contents = out.toString();
} catch (Exception e) {
contents = "Failed to load/bcelify file";
}
for (int i = 0; i < methods.length; i++) {
String methodIndex = Integer.toString(i);
addEditPane(contents, methodIndex);
}
}
示例14: main1
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
public static void main1(String[] args) throws Throwable {
ClassParser cp = new ClassParser("/home/kinow/Development/java/apache/tests-for-commons/target/classes/br/eti/kinoshita/commons/bcel/Test.class");
ClassGen cg = new ClassGen(cp.parse());
MethodGen mg = new MethodGen(cg.getMethodAt(0), cg.getClassName(), cg.getConstantPool());
mg.getAnnotationsOnParameter(0);
System.out.println("OK!");
}
示例15: main
import org.apache.bcel.classfile.ClassParser; //導入依賴的package包/類
public static void main(String[] args) throws Throwable {
ClassParser cp = new ClassParser("/home/kinow/Development/java/apache/tests-for-commons/target/classes/br/eti/kinoshita/commons/bcel/Test$Inner.class");
ClassGen cg = new ClassGen(cp.parse());
MethodGen mg = new MethodGen(cg.getMethodAt(0), cg.getClassName(), cg.getConstantPool());
// here..args.
System.out.println(mg.getAnnotationsOnParameter(0));
System.out.println("OK!");
}