import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.bcel.classfile.ClassParser;
import org.apache.bcel.classfile.Constant;
import org.apache.bcel.classfile.ConstantPool;
import org.apache.bcel.classfile.ConstantString;
import org.apache.bcel.classfile.ConstantUtf8;
import org.apache.bcel.classfile.JavaClass;
import org.apache.bcel.generic.ClassGen;
import org.apache.bcel.generic.ConstantPoolGen;
public class CompilerString
{
ClassParser parser = null;
ClassGen gen = null;
JavaClass original = null;
JavaClass optimized = null;
public CompilerString(String classFilePath)
pool
contains
ConstantUtf8
javap)
{
try
{
this.parser = new ClassParser(classFilePath);
this.original = this.parser.parse();
} }
{
// load the original class into a class generator
ClassGen cgen = new ClassGen(original);
ConstantPoolGen cpgen = cgen.getConstantPool();
// get the current constant pool
ConstantPool cp = cpgen.getConstantPool();
// get the constants in the pool
Constant[] constants = cp.getConstantPool();
for (int i = 0; i < constants.length; i++){// string constants take two entries in the// the first one is of ConstantString, which// an index to the second entry, which is// (displayed Asciz when disassembled by//}catch (IOException e){ e.printStackTrace();private void optimize() // ConstantUtf8 (Asciz) entries are used tostore method names, etc // whereas we are only interested in Stringconstantsentry, which we then replace// So we first look for ConstantString entry,// then retrieve the index of ConstantUtf8 constants[i];new ConstantUtf8(“Compiler”));} }} }{} }// TODO Auto-generated catch blocke.printStackTrace();if (constants[i] instanceof ConstantString){ConstantString cs = (ConstantString)cp.setConstant(cs.getStringIndex(),// we generate a new class with modifications// and store it in a member variablethis.optimized = cgen.getJavaClass();}public void write(String optimisedFilePath){ {FileOutputStream(new File(optimisedFilePath)); this.optimized.dump(out); catch (FileNotFoundException e) // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e)this.optimize();try} {FileOutputStream out = newpublic static void main(String args[]){System.out.println(args[0]);CompilerString optimizer = new CompilerString(args[0]);optimizer.write(args[1]);
Programming
[SOLVED] Java compiler import java.io.File;
$25
File Name: Java_compiler_import_java.io.File;.zip
File Size: 320.28 KB
Only logged in customers who have purchased this product may leave a review.
Reviews
There are no reviews yet.