use of org.apache.bcel.generic.ClassGen in project qpid-broker-j by apache.
the class BrokerTestHelper method createSubClassByteCode.
private static byte[] createSubClassByteCode(final String className, final Class<?>[] interfaces) {
String[] ifnames = new String[interfaces.length];
for (int i = 0; i < interfaces.length; i++) {
ifnames[i] = interfaces[i].getName();
}
ClassGen classGen = new ClassGen(className, "java.lang.Object", "<generated>", ACC_PUBLIC | ACC_SUPER | ACC_INTERFACE, ifnames);
JavaClass javaClass = classGen.getJavaClass();
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
javaClass.dump(out);
return out.toByteArray();
} catch (IOException ioex) {
throw new IllegalStateException("Could not write to a ByteArrayOutputStream - should not happen", ioex);
}
}
use of org.apache.bcel.generic.ClassGen in project core by s4.
the class OverloadDispatcherGenerator method generate.
public Class<Object> generate() {
Random rand = new Random(System.currentTimeMillis());
String dispatcherClassName = "OverloadDispatcher" + (Math.abs(rand.nextInt() % 3256));
String interfaceName = "io.s4.processor.OverloadDispatcher";
if (forSlot) {
interfaceName = "io.s4.processor.OverloadDispatcherSlot";
}
ClassGen cg = new ClassGen(dispatcherClassName, "java.lang.Object", dispatcherClassName + ".java", Constants.ACC_PUBLIC | Constants.ACC_SUPER, new String[] { interfaceName });
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>", dispatcherClassName, 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 dispatch method
il = new InstructionList();
Type[] dispatchArgumentTypes = null;
String[] dispatchArgumentNames = null;
int postArgumentVariableSlot = 3;
if (forSlot) {
dispatchArgumentTypes = new Type[] { ObjectType.OBJECT, ObjectType.OBJECT, ObjectType.LONG, abstractWindowingPEType };
dispatchArgumentNames = new String[] { "slot", "event", "slotTime", "pe" };
postArgumentVariableSlot = 6;
} else {
dispatchArgumentTypes = new Type[] { ObjectType.OBJECT, ObjectType.OBJECT };
dispatchArgumentNames = new String[] { "pe", "event" };
}
MethodGen method = new MethodGen(Constants.ACC_PUBLIC, Type.VOID, dispatchArgumentTypes, dispatchArgumentNames, "dispatch", dispatcherClassName, il, cp);
List<InstructionHandle> targetInstructions = new ArrayList<InstructionHandle>();
List<BranchInstruction> branchInstructions = new ArrayList<BranchInstruction>();
List<BranchInstruction> gotoInstructions = new ArrayList<BranchInstruction>();
ObjectType peType = new ObjectType(targetClass.getName());
il.append(InstructionFactory.createLoad(Type.OBJECT, 1));
il.append(instFactory.createCheckCast(peType));
il.append(InstructionFactory.createStore(peType, postArgumentVariableSlot));
for (int i = 0; i < hierarchies.size(); i++) {
Hierarchy hierarchy = hierarchies.get(i);
ObjectType hierarchyTop = new ObjectType(hierarchy.getTop().getName());
InstructionHandle ih = il.append(InstructionFactory.createLoad(Type.OBJECT, 2));
if (i > 0) {
targetInstructions.add(ih);
}
il.append(new INSTANCEOF(cp.addClass(hierarchyTop)));
BranchInstruction bi = InstructionFactory.createBranchInstruction(Constants.IFEQ, null);
il.append(bi);
branchInstructions.add(bi);
il.append(InstructionFactory.createLoad(peType, postArgumentVariableSlot));
il.append(InstructionFactory.createLoad(hierarchyTop, 2));
il.append(instFactory.createCheckCast(hierarchyTop));
if (forSlot) {
il.append(InstructionFactory.createLoad(ObjectType.LONG, 3));
il.append(InstructionFactory.createLoad(abstractWindowingPEType, 5));
}
Type[] argumentTypes = null;
if (forSlot) {
argumentTypes = new Type[] { hierarchyTop, ObjectType.LONG, abstractWindowingPEType };
} else {
argumentTypes = new Type[] { hierarchyTop };
}
il.append(instFactory.createInvoke(targetClass.getName(), "processEvent", Type.VOID, argumentTypes, Constants.INVOKEVIRTUAL));
// no branch needed for last check
if (i < (hierarchies.size() - 1)) {
bi = InstructionFactory.createBranchInstruction(Constants.GOTO, null);
il.append(bi);
gotoInstructions.add(bi);
}
}
InstructionHandle returnInstruction = il.append(InstructionFactory.createReturn(Type.VOID));
for (int i = 0; i < targetInstructions.size(); i++) {
branchInstructions.get(i).setTarget(targetInstructions.get(i));
}
branchInstructions.get(branchInstructions.size() - 1).setTarget(returnInstruction);
for (BranchInstruction gotoInstruction : gotoInstructions) {
gotoInstruction.setTarget(returnInstruction);
}
method.setMaxStack();
method.setMaxLocals();
cg.addMethod(method.getMethod());
il.dispose();
JavaClass jc = cg.getJavaClass();
OverloadDispatcherClassLoader cl = new OverloadDispatcherClassLoader();
// debug
if (classDumpFile != null) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(classDumpFile);
fos.write(jc.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null)
try {
fos.close();
} catch (Exception e) {
}
}
}
return cl.loadClassFromBytes(dispatcherClassName, jc.getBytes());
}
use of org.apache.bcel.generic.ClassGen 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.generic.ClassGen in project jop by jop-devel.
the class AppInfo method tryLoadClass.
private ClassInfo tryLoadClass(String className) throws IOException {
loadLogger.debug("Loading class " + className);
InputStream is = classPath.getInputStream(className);
JavaClass javaClass = new ClassParser(is, className).parse();
is.close();
if (javaClass.getMajor() > 50) {
// instruction (requires patching of BCEL code similar to Classpath and InstructionFinder)
throw new JavaClassFormatError("Classfiles with versions 51.0 (Java 7) and above are currently not supported!");
}
return new ClassInfo(new ClassGen(javaClass));
}
use of org.apache.bcel.generic.ClassGen in project jop by jop-devel.
the class AppInfo method createClassInfo.
private ClassInfo createClassInfo(String className, String superClassName, boolean isInterface) {
String filename = className.replace(".", File.separator) + ".class";
int af = Constants.ACC_PUBLIC;
if (isInterface) {
af |= Constants.ACC_INTERFACE;
}
ClassGen clsGen = new ClassGen(className, superClassName, filename, af, new String[0]);
return new ClassInfo(clsGen);
}
Aggregations