Refine search
ClassGen cg = new ClassGen(CLASS_NAME_SERIALIZABLE_IMPL_WITH_VALUE, CLASS_NAME_SERIALIZABLE_IMPL, GENERATED, Constants.ACC_PUBLIC | Constants.ACC_SUPER, new String[] {Valuable.class.getName()}); ConstantPoolGen cp = cg.getConstantPool(); InstructionFactory fac = new InstructionFactory(cg, cp); cg.addField(field); cg.addEmptyConstructor(Constants.ACC_PUBLIC); InstructionHandle ctor_ih_0 = ctor.append(fac.createLoad(Type.OBJECT, 0)); ctor.append(fac.createInvoke(CLASS_NAME_SERIALIZABLE_IMPL, "<init>", Type.VOID, Type.NO_ARGS, cg.addMethod(ctorMethod.getMethod()); InstructionHandle getter_ih_1 = getter.append(fac.createGetField(cg.getClassName(), cg.addMethod(getterMethod.getMethod()); InstructionHandle setter_ih_2 = setter.append(fac.createPutField(cg.getClassName(), cg.addMethod(setterMethod.getMethod()); JavaClass jClazz = cg.getJavaClass(); byte[] bytes = jClazz.getBytes(); return defineClass(jClazz.getClassName(), bytes, 0, bytes.length);
@Override public ConstantPoolGen analyze(IAnalysisCache analysisCache, ClassDescriptor descriptor) throws CheckedAnalysisException { ClassGen classGen = new ClassGen(analysisCache.getClassAnalysis(JavaClass.class, descriptor)); return classGen.getConstantPool(); }
@Override protected Class<?> findClass(String name) throws ClassNotFoundException { ClassGen cg = new ClassGen(name, "java.lang.Object", "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); cg.addEmptyConstructor(Constants.ACC_PUBLIC); JavaClass jClazz = cg.getJavaClass(); byte[] bytes = jClazz.getBytes(); return defineClass(jClazz.getClassName(), bytes, 0, bytes.length); }
public AttributeSetMethodGenerator(String methodName, ClassGen classGen) { super(org.apache.bcel.Constants.ACC_PRIVATE, org.apache.bcel.generic.Type.VOID, argTypes, argNames, methodName, classGen.getClassName(), new InstructionList(), classGen.getConstantPool()); _aloadDom = new ALOAD(DOM_INDEX); _astoreDom = new ASTORE(DOM_INDEX); _astoreIterator = new ASTORE(ITERATOR_INDEX); _aloadIterator = new ALOAD(ITERATOR_INDEX); _astoreHandler = new ASTORE(HANDLER_INDEX); _aloadHandler = new ALOAD(HANDLER_INDEX); }
private void createSetMethod(String method_name, String field_name, Class field_type) { InstructionList tmpIl = new InstructionList(); MethodGen method = new MethodGen(ACC_PUBLIC | ACC_FINAL, Type.VOID, buildArgs(new Class[] {field_type}), new String[] {"arg"}, method_name, classname, tmpIl, cp); tmpIl.append(InstructionConstants.ALOAD_0); tmpIl.append(InstructionConstants.ALOAD_1); tmpIl.append(factory.createFieldAccess(classname, field_name, getObjectType(Object.class), Constants.PUTFIELD)); tmpIl.append(InstructionFactory.createReturn(Type.VOID)); method.setMaxStack(); method.setMaxLocals(); cg.addMethod(method.getMethod()); tmpIl.dispose(); }
final ClassGen cg = new ClassGen(readerClassName, "java.lang.Object", "<generated>", ACC_PUBLIC | ACC_SUPER, new String[] { DataReader.class.getCanonicalName() }); final ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool cg.setMajor(MAJOR); cg.setMinor(MINOR); cg.addEmptyConstructor(ACC_PUBLIC); LOG.debug("<init> method:\n{}", cg.getMethods()[0].getCode().toString(true)); final InstructionList il = new InstructionList(); final InstructionFactory factory = new InstructionFactory(cg); final MethodGen read = new MethodGen( ACC_PUBLIC, org.apache.bcel.generic.Type.OBJECT, clazz.setEnd(il.append(InstructionFactory.ARETURN)); read.setMaxStack(); cg.addMethod(read.getMethod()); null, "getReturnType", readerClassName, il, cp); il.append(new LDC_W(cp.addClass(enhanceInnerClassName(rtc.getMethodReturnType(), rtc.getClazz().isMemberClass())))); il.append(InstructionFactory.ARETURN); returnType.setMaxStack(); cg.addMethod(returnType.getMethod());
/** * Initialize with existing class. * @param clazz JavaClass object (e.g. read from file) */ public ClassGen(JavaClass clazz) { class_name_index = clazz.getClassNameIndex(); superclass_name_index = clazz.getSuperclassNameIndex(); class_name = clazz.getClassName(); super_class_name = clazz.getSuperclassName(); file_name = clazz.getSourceFileName(); access_flags = clazz.getAccessFlags(); cp = new ConstantPoolGen(clazz.getConstantPool()); major = clazz.getMajor(); minor = clazz.getMinor(); Attribute[] attributes = clazz.getAttributes(); Method[] methods = clazz.getMethods(); Field[] fields = clazz.getFields(); String[] interfaces = clazz.getInterfaceNames(); for(int i=0; i < interfaces.length; i++) addInterface(interfaces[i]); for(int i=0; i < attributes.length; i++) addAttribute(attributes[i]); for(int i=0; i < methods.length; i++) addMethod(methods[i]); for(int i=0; i < fields.length; i++) addField(fields[i]); }
/** * Expects a real on the stack and pushes a truncated integer value * * @see org.apache.xalan.xsltc.compiler.util.Type#translateTo */ public void translateTo(ClassGenerator classGen, MethodGenerator methodGen, IntType type) { final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = methodGen.getInstructionList(); il.append(new INVOKESTATIC(cpg.addMethodref(BASIS_LIBRARY_CLASS, "realToInt","(D)I"))); }
new ClassGen(template, "java.lang.Object", "<generated>", Constants.ACC_PUBLIC | Constants.ACC_SUPER, null); ConstantPoolGen cp = cg.getConstantPool(); // cg creates constant pool InstructionList il = new InstructionList(); MethodGen mg = new MethodGen(Constants.ACC_STATIC | il.append(new NEW(br_index)); il.append(DUP); il.append(new NEW(ir_index)); il.append(DUP); LocalVariableGen lg = mg.addLocalVariable("in", new ObjectType("java.io.BufferedReader"), null, null); int in = lg.getIndex(); lg = mg.addLocalVariable("name", Type.STRING, null, null); int name = lg.getIndex(); il.append(ACONST_NULL); mg.setMaxStack(5); // Needed stack size cg.addMethod(mg.getMethod()); cg.addEmptyConstructor(Constants.ACC_PUBLIC); cg.getJavaClass().dump(template + ".class");
classGen.getStylesheet()); final ConstantPoolGen cpg = filterGen.getConstantPool(); final int length = (_closureVars == null) ? 0 : _closureVars.size(); VariableBase var = ((VariableRefBase) _closureVars.get(i)).getVariable(); filterGen.addField(new Field(ACC_PUBLIC, cpg.addUtf8(var.getEscapedName()), cpg.addUtf8(var.getType().toSignature()), null, cpg.getConstantPool())); final InstructionList il = new InstructionList(); testGen = new TestGenerator(ACC_PUBLIC | ACC_FINAL, org.apache.bcel.generic.Type.BOOLEAN, il.append(IRETURN); filterGen.addEmptyConstructor(ACC_PUBLIC); filterGen.addMethod(testGen); getXSLTC().dumpClass(filterGen.getJavaClass());
mg.addAnnotationEntry(createSimpleVisibleAnnotation(mg .getConstantPool())); il.append(factory.createNew("java.io.BufferedReader")); il.append(InstructionConst.DUP); // Use predefined constant il.append(factory.createNew("java.io.InputStreamReader")); il.append(InstructionConst.DUP); il.append(factory.createFieldAccess("java.lang.System", "in", i_stream, Type.VOID, new Type[] { new ObjectType("java.io.Reader") }, Const.INVOKESPECIAL)); LocalVariableGen lg = mg.addLocalVariable("in", new ObjectType( "java.io.BufferedReader"), null, null); final int in = lg.getIndex(); mg.setMaxStack(); mg.setMaxLocals(); cg.addMethod(mg.getMethod()); il.dispose(); // Allow instruction handles to be reused cg.addEmptyConstructor(Const.ACC_PUBLIC);
JavaClass target; try { target = Repository.lookupClass("Target"); } catch (final ClassNotFoundException ex) { throw new RuntimeException("unable to resolve Target", ex); } final ClassGen targetGen = new ClassGen(target); final ConstantPoolGen pool = targetGen.getConstantPool(); final ConstantMethodref ref = (ConstantMethodref) pool.getConstant( pool.lookupMethodref("Name", "getName", "()Ljava/lang/String;")); ref.setClassIndex(pool.lookupClass("Target")); ref.setNameAndTypeIndex(pool.addNameAndType("$Name$getName", "()Ljava/lang/String;")); final InstructionList code = new InstructionList(); final InstructionFactory factory = new InstructionFactory(targetGen, pool); code.append(factory.createConstant("overriden-name")); code.append(factory.createReturn(Type.STRING)); code.setPositions(); final MethodGen methodGen = new MethodGen( Constants.ACC_PRIVATE | Constants.ACC_SYNTHETIC | Constants.ACC_STATIC, Type.STRING, new Type[0], new String[0], "$Name$getName", "Target", code, pool); methodGen.setMaxLocals(0); methodGen.setMaxStack(1); targetGen.addMethod(methodGen.getMethod()); try { targetGen.getJavaClass().dump("Target.class"); } catch (final IOException ex) { throw new RuntimeException("unable to save Target", ex); }
public void replaceCheckMethod() { ClassGen cg = cgs.get(AuthClass); Method method = cg.getMethods()[0]; MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool()); InstructionList list = new InstructionList(); list.append(new ICONST(1)); list.append(new IRETURN()); mg.removeExceptionHandlers(); mg.removeLineNumbers(); mg.removeLocalVariables(); mg.removeExceptions(); mg.setInstructionList(list); mg.setMaxLocals(); mg.setMaxStack(); cg.replaceMethod(method, mg.getMethod()); }
import java.io.FileOutputStream; import org.apache.bcel.Const; import org.apache.bcel.generic.*; public class CreateB { public static void main(String[] args) throws Exception { ClassGen cg = new ClassGen("B", "A", "B.java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[] {}); ConstantPoolGen cp = cg.getConstantPool(); InstructionList il = new InstructionList(); MethodGen method = new MethodGen(Const.ACC_PUBLIC, Type.VOID, Type.NO_ARGS, new String[] {}, "<init>", "B", il, cp); il.append(InstructionFactory.createReturn(Type.VOID)); method.setMaxStack(); method.setMaxLocals(); cg.addMethod(method.getMethod()); il.dispose(); cg.getJavaClass().dump(new FileOutputStream("B.class")); } }
public void translate(ClassGenerator classGen, MethodGenerator methodGen) { ConstantPoolGen cpg = classGen.getConstantPool(); InstructionList il = methodGen.getInstructionList(); il.append(new PUSH(cpg, _value)); }
ClassGen classGen = new ClassGen(jclass); Method[] methodList = jclass.getMethods(); for (Method method : methodList) { if (method.isAbstract() || method.isNative()) { MethodGen methodGen = new MethodGen(method, jclass.getClassName(), classGen.getConstantPool());
/** * Convenience method. * * Add an empty constructor to this class that does nothing but calling super(). * @param access_flags rights for constructor */ public void addEmptyConstructor( final int access_flags ) { final InstructionList il = new InstructionList(); il.append(InstructionConst.THIS); // Push `this' il.append(new INVOKESPECIAL(cp.addMethodref(super_class_name, "<init>", "()V"))); il.append(InstructionConst.RETURN); final MethodGen mg = new MethodGen(access_flags, Type.VOID, Type.NO_ARGS, null, "<init>", class_name, il, cp); mg.setMaxStack(1); addMethod(mg.getMethod()); }
public static boolean disassemble(String name) { JavaClass mod = null; try { mod = Repository.lookupClass(name); } catch (Exception e) { System.err.println("Could not get class " + name); return false; } ClassGen modClass = new ClassGen(mod); ConstantPoolGen cp = modClass.getConstantPool(); Method[] methods = mod.getMethods(); for (int i = 0; i < methods.length; i++) { System.out.println("* "+methods[i].getName()); MethodGen mg = new MethodGen(methods[i], mod.getClassName(), cp); InstructionList il = mg.getInstructionList(); System.out.println(il.toString()); } return true; } }
public void fixStack() { for (ClassGen cg : cgs.values()) { for (Method method : cg.getMethods()) { MethodGen mg = new MethodGen(method, cg.getClassName(), cg.getConstantPool()); mg.removeNOPs(); mg.setMaxLocals(); mg.setMaxStack(); cg.replaceMethod(method, mg.getMethod()); logger.debug(String.format("Reset MaxStack and MaxLocals in %s.%s", cg.getClassName(), mg.getName())); } } }
/** * Some type conversions require gettting the first node from the node-set. * This function is defined to avoid code repetition. */ private void getFirstNode(ClassGenerator classGen, MethodGenerator methodGen) { final ConstantPoolGen cpg = classGen.getConstantPool(); final InstructionList il = methodGen.getInstructionList(); il.append(new INVOKEINTERFACE(cpg.addInterfaceMethodref(NODE_ITERATOR, NEXT, NEXT_SIG), 1)); }