本文整理汇总了Java中org.apache.bcel.classfile.ClassParser.parse方法的典型用法代码示例。如果您正苦于以下问题:Java ClassParser.parse方法的具体用法?Java ClassParser.parse怎么用?Java ClassParser.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.bcel.classfile.ClassParser
的用法示例。
在下文中一共展示了ClassParser.parse方法的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: 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();
}
}
示例3: 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());
}
示例4: 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());
}
}
示例5: 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;
}
示例6: 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);
}
}
}
}
示例7: 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);
}
}
}
示例8: 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);
}
示例9: 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!");
}
示例10: 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!");
}
示例11: lookupClass
import org.apache.bcel.classfile.ClassParser; //导入方法依赖的package包/类
/**
* Look up a class from the classpath.
*
* @param className name of class to look up
* @return the JavaClass object for the class
* @throws ClassNotFoundException if the class couldn't be found
* @throws ClassFormatException if the classfile format is invalid
*/
public JavaClass lookupClass(String className) throws ClassNotFoundException {
String resourceName = className.replace('.', '/') + ".class";
InputStream in = null;
boolean parsedClass = false;
try {
in = getInputStreamForResource(resourceName);
if (in == null)
throw new ClassNotFoundException("Error while looking for class " +
className + ": class not found");
ClassParser classParser = new ClassParser(in, resourceName);
JavaClass javaClass = classParser.parse();
parsedClass = true;
return javaClass;
} catch (IOException e) {
throw new ClassNotFoundException("IOException while looking for class " +
className + ": " + e.toString());
} finally {
if (in != null && !parsedClass) {
try {
in.close();
} catch (IOException ignore) {
// Ignore
}
}
}
}
示例12: debugify
import org.apache.bcel.classfile.ClassParser; //导入方法依赖的package包/类
public static byte[] debugify(String className, byte[] bytes) {
if (!Debugger.INSTRUMENT)
return bytes;
if (Debugger.TRACE_LOADER)
println("debugifying " + className);
ClassParser parser = new ClassParser(new ByteArrayInputStream(bytes),
"<generated>");
JavaClass javaClass;
try {
javaClass = parser.parse();
} catch (IOException e) {
System.out.println("IMPOSSIBLE");
e.printStackTrace();
return bytes;
}
Class clazz;
if (javaClass == null)
return null;
if (Debugger.TRACE_LOADER)
println(spacesExact() + "DebugifyingClassLoader debugifying: "
+ className);
if (Debugger.TRACE_LOADER_STACK)
(new Exception("Just used to get stack trace")).printStackTrace();
long start = System.currentTimeMillis();
javaClass = Debugify.debugifyClass(javaClass, className);
long end = System.currentTimeMillis();
Debugger.timeDebugifying += (end - start);
bytes = javaClass.getBytes();
return bytes;
}
示例13: addGeneratedLibraryClassFiles
import org.apache.bcel.classfile.ClassParser; //导入方法依赖的package包/类
/**
* Add all the generated class files to the Jar file.
*
* @throws IOException
*/
public void addGeneratedLibraryClassFiles() throws IOException {
Set<String> classFiles = new HashSet<String>();
File dir = Paths.libraryClassesOutputDirectory();
File placeholderJar = Paths.placeholderLibraryJarFile();
// Add the class files to the crafted JAR file.
FileUtils.listFiles(dir, new String[] { "class" }, true).stream()
.filter(f -> !relativize(dir, f).equals(Names.AVERROES_LIBRARY_CLASS_BC_SIG + ".class"))
.forEach(file -> {
try {
String className = relativize(dir, file);
add(dir, file);
classFiles.add(className);
} catch (IOException e) {
e.printStackTrace();
}
});
close();
// Now add all those class files in the crafted JAR file to the BCEL
// repository.
for (String classFile : classFiles) {
ClassParser parser = new ClassParser(placeholderJar.getPath(), classFile);
JavaClass cls = parser.parse();
bcelClasses.add(cls);
}
}
示例14: addAverroesLibraryClassFile
import org.apache.bcel.classfile.ClassParser; //导入方法依赖的package包/类
/**
* Add the generated AverroesLibraryClass file to the Jar file.
*
* @throws IOException
* @throws URISyntaxException
*/
public void addAverroesLibraryClassFile() throws IOException, URISyntaxException {
File dir = Paths.libraryClassesOutputDirectory();
File placeholderJar = Paths.placeholderLibraryJarFile();
File averroesLibraryClassJar = Paths.averroesLibraryClassJarFile();
File file = FileUtils.listFiles(dir, new String[] { "class" }, true).stream()
.filter(f -> relativize(dir, f).equals(Names.AVERROES_LIBRARY_CLASS_BC_SIG + ".class"))
.collect(Collectors.toList()).get(0);
String className = relativize(dir, file);
// Add the class file to the separately crafted JAR file.
if (file.isFile()) {
add(dir, file);
} else {
throw new IllegalStateException("cannot find " + Names.AVERROES_LIBRARY_CLASS
+ System.getProperty("line.separator") + "Invalid path given: " + fileName);
}
close();
// Set BCEL's repository class path.
SyntheticRepository rep = SyntheticRepository.getInstance(new ClassPath(averroesLibraryClassJar
+ File.pathSeparator + placeholderJar + File.pathSeparator + Paths.organizedApplicationJarFile()));
Repository.setRepository(rep);
// Now add the class files (including ones from placeholder JAR) to the
// BCEL repository.
ClassParser parser = new ClassParser(averroesLibraryClassJar.getPath(), className);
JavaClass cls = parser.parse();
bcelClasses.add(cls);
// Now we need to add all the BCEL classes (including ones from previous
// placeholder JAR to force BCEL to load
// those crafted files when it looks them up
bcelClasses.forEach(c -> Repository.getRepository().storeClass(c));
// Now verify all the generated class files
verify();
}
示例15: extractClass
import org.apache.bcel.classfile.ClassParser; //导入方法依赖的package包/类
private static JavaClass extractClass( File f, Repository repository )
throws CheckerException
{
InputStream is = null;
try
{
is = new FileInputStream( f );
ClassParser parser = new ClassParser( is, f.getName() );
JavaClass clazz = parser.parse();
clazz.setRepository( repository );
return clazz;
}
catch ( IOException ex )
{
throw new CheckerException( "Cannot read " + f, ex );
}
finally
{
IOUtil.close( is );
}
}