public void throw_exception(Type type, String msg) { new_instance(type); dup(); push(msg); invoke_constructor(type, CSTRUCT_STRING); athrow(); }
public static void push_array(CodeEmitter e, Object[] array) { e.push(array.length); e.newarray(Type.getType(remapComponentType(array.getClass().getComponentType()))); for (int i = 0; i < array.length; i++) { e.dup(); e.push(i); push_object(e, array[i]); e.aastore(); } }
public static void push_object(CodeEmitter e, Object obj) { if (obj == null) { e.aconst_null(); } else { Class type = obj.getClass(); if (type.isArray()) { push_array(e, (Object[])obj); } else if (obj instanceof String) { e.push((String)obj); } else if (obj instanceof Type) { load_class(e, (Type)obj); } else if (obj instanceof Class) { load_class(e, Type.getType((Class)obj)); } else if (obj instanceof BigInteger) { e.new_instance(Constants.TYPE_BIG_INTEGER); e.dup(); e.push(obj.toString()); e.invoke_constructor(Constants.TYPE_BIG_INTEGER); } else if (obj instanceof BigDecimal) { e.new_instance(Constants.TYPE_BIG_DECIMAL); e.dup(); e.push(obj.toString()); e.invoke_constructor(Constants.TYPE_BIG_DECIMAL); } else { throw new IllegalArgumentException("unknown type: " + obj.getClass()); } } }
private void initFieldProvider(String[] names) { CodeEmitter e = getStaticHook(); EmitUtils.push_object(e, names); e.putstatic(getClassType(), FIELD_NAMES, Constants.TYPE_STRING_ARRAY); e.push(names.length); e.newarray(Constants.TYPE_CLASS); e.dup(); for(int i = 0; i < names.length; i++ ){ e.dup(); e.push(i); Type type = (Type)fields.get(names[i]); EmitUtils.load_class(e, type); e.aastore(); } e.putstatic(getClassType(), FIELD_TYPES, Constants.TYPE_CLASS_ARRAY); }
private static void member_helper_size(final CodeEmitter e, List members, final ObjectSwitchCallback callback, final ParameterTyper typer, final Label def, final Label end) throws Exception { final Map buckets = CollectionUtils.bucket(members, new Transformer() { public Object transform(Object value) { return new Integer(typer.getParameterTypes((MethodInfo)value).length); } }); e.dup(); e.arraylength(); e.process_switch(EmitUtils.getSwitchKeys(buckets), new ProcessSwitchCallback() { public void processCase(int key, Label dontUseEnd) throws Exception { List bucket = (List)buckets.get(new Integer(key)); member_helper_type(e, bucket, callback, typer, def, end, new BitSet()); } public void processDefault() throws Exception { e.goTo(def); } }); }
private void emitConstructors(ClassEmitter ce, List constructors) { boolean seenNull = false; for (Iterator it = constructors.iterator(); it.hasNext();) { MethodInfo constructor = (MethodInfo)it.next(); CodeEmitter e = EmitUtils.begin_method(ce, constructor, Constants.ACC_PUBLIC); e.load_this(); e.dup(); e.load_args(); Signature sig = constructor.getSignature(); seenNull = seenNull || sig.getDescriptor().equals("()V"); e.super_invoke_constructor(sig); e.invoke_static_this(BIND_CALLBACKS); if (!interceptDuringConstruction) { e.load_this(); e.push(1); e.putfield(CONSTRUCTED_FIELD); } e.return_value(); e.end_method(); } if (!classOnly && !seenNull && arguments == null) throw new IllegalArgumentException("Superclass has no null constructors but no arguments were given"); }
private void emitCommonNewInstance(CodeEmitter e) { e.new_instance_this(); e.dup(); e.invoke_constructor_this(); e.aconst_null(); e.invoke_static_this(SET_THREAD_CALLBACKS); e.return_value(); e.end_method(); }
public void processCase(Object key, Label end) { PropertyDescriptor pd = (PropertyDescriptor)setters.get(key); if (pd.getReadMethod() == null) { e.aconst_null(); } else { MethodInfo read = ReflectUtils.getMethodInfo(pd.getReadMethod()); e.dup(); e.invoke(read); e.box(read.getSignature().getReturnType()); } e.swap(); // move old value behind bean e.load_arg(2); // new value MethodInfo write = ReflectUtils.getMethodInfo(pd.getWriteMethod()); e.unbox(write.getSignature().getArgumentTypes()[0]); e.invoke(write); e.return_value(); } public void processDefault() {
/** * Allocates and fills an Object[] array with the arguments to the * current method. Primitive values are inserted as their boxed * (Object) equivalents. */ public void create_arg_array() { /* generates: Object[] args = new Object[]{ arg1, new Integer(arg2) }; */ push(state.argumentTypes.length); newarray(); for (int i = 0; i < state.argumentTypes.length; i++) { dup(); push(i); load_arg(i); box(state.argumentTypes[i]); aastore(); } }
public static void factory_method(ClassEmitter ce, Signature sig) { CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, sig, null); e.new_instance_this(); e.dup(); e.load_args(); e.invoke_constructor_this(TypeUtils.parseConstructor(sig.getArgumentTypes())); e.return_value(); e.end_method(); }
private static void hash_array(final CodeEmitter e, Type type, final int multiplier, final Customizer customizer) { Label skip = e.make_label(); Label end = e.make_label(); e.dup(); e.ifnull(skip); EmitUtils.process_array(e, type, new ProcessArrayCallback() { public void processElement(Type type) { hash_code(e, type, multiplier, customizer); } }); e.goTo(end); e.mark(skip); e.pop(); e.mark(end); }
private static void string_switch_trie(final CodeEmitter e, String[] strings, final ObjectSwitchCallback callback) throws Exception { final Label def = e.make_label(); final Label end = e.make_label(); final Map buckets = CollectionUtils.bucket(Arrays.asList(strings), new Transformer() { public Object transform(Object value) { return new Integer(((String)value).length()); } }); e.dup(); e.invoke_virtual(Constants.TYPE_STRING, STRING_LENGTH); e.process_switch(getSwitchKeys(buckets), new ProcessSwitchCallback() { public void processCase(int key, Label ignore_end) throws Exception { List bucket = (List)buckets.get(new Integer(key)); stringSwitchHelper(e, bucket, callback, def, end, 0); } public void processDefault() { e.goTo(def); } }); e.mark(def); e.pop(); callback.processDefault(); e.mark(end); }
private void emitNewInstanceCallback(ClassEmitter ce) { CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, SINGLE_NEW_INSTANCE, null); switch (callbackTypes.length) { case 0: // TODO: make sure Callback is null break; case 1: // for now just make a new array; TODO: optimize e.push(1); e.newarray(CALLBACK); e.dup(); e.push(0); e.load_arg(0); e.aastore(); e.invoke_static_this(SET_THREAD_CALLBACKS); break; default: e.throw_exception(ILLEGAL_STATE_EXCEPTION, "More than one callback object required"); } emitCommonNewInstance(e); }
private static void hash_object(CodeEmitter e, Type type, Customizer customizer) { // (f == null) ? 0 : f.hashCode(); Label skip = e.make_label(); Label end = e.make_label(); e.dup(); e.ifnull(skip); if (customizer != null) { customizer.customize(e, type); } e.invoke_virtual(Constants.TYPE_OBJECT, HASH_CODE); e.goTo(end); e.mark(skip); e.pop(); e.push(0); e.mark(end); }
private void emitCurrentCallback(CodeEmitter e, int index) { e.load_this(); e.getfield(getCallbackField(index)); e.dup(); Label end = e.make_label(); e.ifnonnull(end); e.pop(); // stack height e.load_this(); e.invoke_static_this(BIND_CALLBACKS); e.load_this(); e.getfield(getCallbackField(index)); e.mark(end); }
/** * Unboxes the object on the top of the stack. If the object is null, the * unboxed primitive value becomes zero. */ public void unbox_or_zero(Type type) { if (TypeUtils.isPrimitive(type)) { if (type != Type.VOID_TYPE) { Label nonNull = make_label(); Label end = make_label(); dup(); ifnonnull(nonNull); pop(); zero_or_null(type); goTo(end); mark(nonNull); unbox(type); mark(end); } } else { checkcast(type); } }
private void generateKeySet(String[] allNames) { // static initializer declare_field(Constants.ACC_STATIC | Constants.ACC_PRIVATE, "keys", FIXED_KEY_SET, null); CodeEmitter e = begin_static(); e.new_instance(FIXED_KEY_SET); e.dup(); EmitUtils.push_array(e, allNames); e.invoke_constructor(FIXED_KEY_SET, CSTRUCT_STRING_ARRAY); e.putfield("keys"); e.return_value(); e.end_method(); // keySet e = begin_method(Constants.ACC_PUBLIC, KEY_SET, null); e.load_this(); e.getfield("keys"); e.return_value(); e.end_method(); }
private void emitGetCallbacks(ClassEmitter ce) { CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, GET_CALLBACKS, null); e.load_this(); e.invoke_static_this(BIND_CALLBACKS); e.load_this(); e.push(callbackTypes.length); e.newarray(CALLBACK); for (int i = 0; i < callbackTypes.length; i++) { e.dup(); e.push(i); e.load_this(); e.getfield(getCallbackField(i)); e.aastore(); } e.return_value(); e.end_method(); }
public void generateClass(ClassVisitor cv) { final MethodInfo method = ReflectUtils.getMethodInfo(ReflectUtils.findInterfaceMethod(iface)); ClassEmitter ce = new ClassEmitter(cv); ce.begin_class(Constants.V1_2, Constants.ACC_PUBLIC, getClassName(), MULTICAST_DELEGATE, new Type[]{ Type.getType(iface) }, Constants.SOURCE_FILE); EmitUtils.null_constructor(ce); // generate proxied method emitProxy(ce, method); // newInstance CodeEmitter e = ce.begin_method(Constants.ACC_PUBLIC, NEW_INSTANCE, null); e.new_instance_this(); e.dup(); e.invoke_constructor_this(); e.return_value(); e.end_method(); // add e = ce.begin_method(Constants.ACC_PUBLIC, ADD_DELEGATE, null); e.load_this(); e.load_arg(0); e.checkcast(Type.getType(iface)); e.invoke_virtual_this(ADD_HELPER); e.return_value(); e.end_method(); ce.end_class(); }