use of org.apache.bcel.classfile.JavaClass in project core by s4.
the class ClonerGenerator method generate.
public Class generate(Class clazz) {
String className = clazz.getName();
Random rand = new Random(System.currentTimeMillis());
String clonerClassname = "Cloner" + (Math.abs(rand.nextInt() % 3256));
ClassGen cg = new ClassGen(clonerClassname, "java.lang.Object", clonerClassname + ".java", Constants.ACC_PUBLIC | Constants.ACC_SUPER, new String[] { "io.s4.util.Cloner" });
ConstantPoolGen cp = cg.getConstantPool();
InstructionFactory instFactory = new InstructionFactory(cg, cp);
InstructionList il = new InstructionList();
// build constructor method for new class
MethodGen constructor = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", clonerClassname, il, cp);
il.append(InstructionFactory.createLoad(Type.OBJECT, 0));
il.append(instFactory.createInvoke("java.lang.Object", "<init>", Type.VOID, Type.NO_ARGS, Constants.INVOKESPECIAL));
il.append(InstructionFactory.createReturn(Type.VOID));
constructor.setMaxStack();
constructor.setMaxLocals();
cg.addMethod(constructor.getMethod());
il.dispose();
// build clone method
il = new InstructionList();
MethodGen method = new MethodGen(Constants.ACC_PUBLIC, Type.OBJECT, new Type[] { Type.OBJECT }, new String[] { "arg0" }, "clone", clonerClassname, il, cp);
il.append(InstructionConstants.ACONST_NULL);
il.append(InstructionFactory.createStore(Type.OBJECT, 2));
il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
il.append(new INSTANCEOF(cp.addClass(new ObjectType(className))));
BranchInstruction ifeq_6 = InstructionFactory.createBranchInstruction(Constants.IFEQ, null);
il.append(ifeq_6);
il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
il.append(instFactory.createCheckCast(new ObjectType(className)));
il.append(InstructionFactory.createStore(Type.OBJECT, 2));
InstructionHandle ih_14 = il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
il.append(new INSTANCEOF(cp.addClass(new ObjectType("java.lang.Cloneable"))));
BranchInstruction ifne_18 = InstructionFactory.createBranchInstruction(Constants.IFNE, null);
il.append(ifne_18);
il.append(instFactory.createFieldAccess("java.lang.System", "out", new ObjectType("java.io.PrintStream"), Constants.GETSTATIC));
il.append(new PUSH(cp, "Not cloneable!"));
il.append(instFactory.createInvoke("java.io.PrintStream", "println", Type.VOID, new Type[] { Type.STRING }, Constants.INVOKEVIRTUAL));
il.append(InstructionConstants.ACONST_NULL);
il.append(InstructionFactory.createReturn(Type.OBJECT));
InstructionHandle ih_31 = il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
il.append(instFactory.createInvoke(className, "clone", Type.OBJECT, Type.NO_ARGS, Constants.INVOKEVIRTUAL));
il.append(InstructionFactory.createReturn(Type.OBJECT));
ifeq_6.setTarget(ih_14);
ifne_18.setTarget(ih_31);
method.setMaxStack();
method.setMaxLocals();
cg.addMethod(method.getMethod());
il.dispose();
JavaClass jc = cg.getJavaClass();
ClonerClassLoader cl = new ClonerClassLoader();
return cl.loadClassFromBytes(clonerClassname, jc.getBytes());
}
use of org.apache.bcel.classfile.JavaClass in project candle-decompiler by bradsdavis.
the class CandleDecompiler method decompile.
public void decompile(String clazzName, Writer writer) throws DecompilerException {
Validate.isTrue(org.apache.commons.lang.StringUtils.isNotBlank(clazzName), "Classname is required field, but was not provided.");
Validate.notNull(writer, "Writer is required, but not provided.");
JavaClass clz;
try {
clz = Repository.lookupClass(clazzName);
decompile(clz, writer);
} catch (ClassNotFoundException e) {
throw new DecompilerException("Exception while decompiling.", e);
}
}
use of org.apache.bcel.classfile.JavaClass in project android-classyshark by google.
the class RootBuilder method fillFromJar.
private ClassNode fillFromJar(File file) {
if (JayceReader.isJackAndJillArchive(file)) {
return fillFromJayce(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;
}
use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.
the class ReplaceNativeAndCPIdx method replace.
private Method replace(Method method) {
MethodGen mg = new MethodGen(method, clazz.getClassName(), cpoolgen);
InstructionList il = mg.getInstructionList();
InstructionFinder f = new InstructionFinder(il);
String methodId = method.getName() + method.getSignature();
OldMethodInfo mi = getCli().getMethodInfo(methodId);
// find invokes first and replace call to Native by
// JOP native instructions.
String invokeStr = "InvokeInstruction";
for (Iterator i = f.search(invokeStr); i.hasNext(); ) {
InstructionHandle[] match = (InstructionHandle[]) i.next();
InstructionHandle first = match[0];
InvokeInstruction ii = (InvokeInstruction) first.getInstruction();
if (ii.getClassName(cpoolgen).equals(JOPizer.nativeClass)) {
short opid = (short) JopInstr.getNative(ii.getMethodName(cpoolgen));
if (opid == -1) {
System.err.println(method.getName() + ": cannot locate " + ii.getMethodName(cpoolgen) + ". Replacing with NOP.");
first.setInstruction(new NOP());
} else {
first.setInstruction(new NativeInstruction(opid, (short) 1));
((JOPizer) ai).outTxt.println("\t" + first.getPosition());
// then we remove pc+2 and pc+1 from the MGCI info
if (JOPizer.dumpMgci) {
il.setPositions();
int pc = first.getPosition();
// important: take the high one first
GCRTMethodInfo.removePC(pc + 2, mi);
GCRTMethodInfo.removePC(pc + 1, mi);
}
}
}
if (ii instanceof INVOKESPECIAL) {
// not an initializer
if (!ii.getMethodName(cpoolgen).equals("<init>")) {
// check if this is a super invoke
// TODO this is just a hack, use InvokeSite.isInvokeSuper() when this is ported to the new framework!
boolean isSuper = false;
String declaredType = ii.getClassName(cpoolgen);
JopClassInfo cls = getCli();
OldClassInfo superClass = cls.superClass;
while (superClass != null) {
if (superClass.clazz.getClassName().equals(declaredType)) {
isSuper = true;
break;
}
if ("java.lang.Object".equals(superClass.clazz.getClassName())) {
break;
}
superClass = superClass.superClass;
}
if (isSuper) {
Integer idx = ii.getIndex();
int new_index = getCli().cpoolUsed.indexOf(idx) + 1;
first.setInstruction(new JOPSYS_INVOKESUPER((short) new_index));
// System.err.println("invokesuper "+ii.getClassName(cpoolgen)+"."+ii.getMethodName(cpoolgen));
}
}
}
}
if (JOPizer.CACHE_INVAL) {
f = new InstructionFinder(il);
// find volatile reads and insert cache invalidation bytecode
String fieldInstr = "GETFIELD|GETSTATIC|PUTFIELD|PUTSTATIC";
for (Iterator i = f.search(fieldInstr); i.hasNext(); ) {
InstructionHandle[] match = (InstructionHandle[]) i.next();
InstructionHandle ih = match[0];
FieldInstruction fi = (FieldInstruction) ih.getInstruction();
JavaClass jc = JOPizer.jz.cliMap.get(fi.getClassName(cpoolgen)).clazz;
Field field = null;
while (field == null) {
Field[] fields = jc.getFields();
for (int k = 0; k < fields.length; k++) {
if (fields[k].getName().equals(fi.getFieldName(cpoolgen))) {
field = fields[k];
break;
}
}
if (field == null) {
try {
jc = jc.getSuperClass();
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new Error();
}
}
}
if (field.isVolatile()) {
if (field.getType().getSize() < 2) {
if (fi instanceof GETFIELD || fi instanceof GETSTATIC) {
ih.setInstruction(new InvalidateInstruction());
ih = il.append(ih, fi);
}
} else {
// this only works because we do not throw a
// NullPointerException for monitorenter/-exit!
ih.setInstruction(new ACONST_NULL());
ih = il.append(ih, new MONITORENTER());
ih = il.append(ih, fi);
ih = il.append(ih, new ACONST_NULL());
ih = il.append(ih, new MONITOREXIT());
}
}
}
}
f = new InstructionFinder(il);
// find instructions that access the constant pool
// and replace the index by the new value from ClassInfo
String cpInstr = "CPInstruction";
for (Iterator it = f.search(cpInstr); it.hasNext(); ) {
InstructionHandle[] match = (InstructionHandle[]) it.next();
InstructionHandle ih = match[0];
CPInstruction cpii = (CPInstruction) ih.getInstruction();
int index = cpii.getIndex();
// we have to grab the information before we change
// the CP index.
FieldInstruction fi = null;
Type ft = null;
if (cpii instanceof FieldInstruction) {
fi = (FieldInstruction) ih.getInstruction();
ft = fi.getFieldType(cpoolgen);
}
Integer idx = new Integer(index);
// pos is the new position in the reduced constant pool
// idx is the position in the 'original' unresolved cpool
int pos = getCli().cpoolUsed.indexOf(idx);
int new_index = pos + 1;
// and putfield and by address for getstatic and putstatic
if (cpii instanceof GETFIELD || cpii instanceof PUTFIELD || cpii instanceof GETSTATIC || cpii instanceof PUTSTATIC) {
// we use the offset instead of the CP index
new_index = getFieldOffset(cp, index);
} else {
if (pos == -1) {
System.out.println("Error: constant " + index + " " + cpoolgen.getConstant(index) + " not found");
System.out.println("new cpool: " + getCli().cpoolUsed);
System.out.println("original cpool: " + cpoolgen);
System.exit(-1);
}
}
// set new index, position starts at
// 1 as cp points to the length of the pool
cpii.setIndex(new_index);
if (cpii instanceof FieldInstruction) {
boolean isRef = ft instanceof ReferenceType;
boolean isLong = ft == BasicType.LONG || ft == BasicType.DOUBLE;
if (fi instanceof GETSTATIC) {
if (isRef) {
ih.setInstruction(new GETSTATIC_REF((short) new_index));
} else if (isLong) {
ih.setInstruction(new GETSTATIC_LONG((short) new_index));
}
} else if (fi instanceof PUTSTATIC) {
if (isRef) {
if (!com.jopdesign.build.JOPizer.USE_RTTM) {
ih.setInstruction(new PUTSTATIC_REF((short) new_index));
}
} else if (isLong) {
ih.setInstruction(new PUTSTATIC_LONG((short) new_index));
}
} else if (fi instanceof GETFIELD) {
if (isRef) {
ih.setInstruction(new GETFIELD_REF((short) new_index));
} else if (isLong) {
ih.setInstruction(new GETFIELD_LONG((short) new_index));
}
} else if (fi instanceof PUTFIELD) {
if (isRef) {
if (!com.jopdesign.build.JOPizer.USE_RTTM) {
ih.setInstruction(new PUTFIELD_REF((short) new_index));
}
} else if (isLong) {
ih.setInstruction(new PUTFIELD_LONG((short) new_index));
}
}
}
}
Method m = mg.getMethod();
il.dispose();
return m;
}
use of org.apache.bcel.classfile.JavaClass in project jop by jop-devel.
the class TransitiveHull method add.
private void add(String class_name) {
class_name = class_name.replace('/', '.');
// }
for (int i = 0; i < excluded.length; ++i) {
if (excluded[i].equals(class_name)) {
return;
}
}
// we ignore array classes
if (class_name.startsWith("[")) {
return;
}
JavaClass clazz;
try {
clazz = Repository.lookupClass(class_name);
} catch (ClassNotFoundException e) {
e.printStackTrace();
throw new Error();
}
if (clazz == null) {
System.out.println("lookupClass(" + class_name + ") failed in TransitiveHull");
System.exit(1);
}
if (clazz != null && _set.add(clazz)) {
_queue.enqueue(clazz);
}
}
Aggregations