List of usage examples for org.objectweb.asm Opcodes ICONST_0
int ICONST_0
To view the source code for org.objectweb.asm Opcodes ICONST_0.
Click Source Link
From source file:net.yrom.tools.RSymbols.java
License:Apache License
private void drainSymbols(Path file) { final String filename = file.getFileName().toString(); String typeName = filename.substring(0, filename.length() - ".class".length()); byte[] bytes; try {/*from w ww. j ava2 s . co m*/ bytes = Files.readAllBytes(file); } catch (IOException e) { throw new UncheckedIOException(e); } ClassVisitor visitor = new ClassVisitor(Opcodes.ASM5) { @Override public FieldVisitor visitField(int access, String name, String desc, String signature, Object value) { // read constant value if (value instanceof Integer) { String key = typeName + '.' + name; Integer old = symbols.get(key); if (old != null && !old.equals(value)) { throw new IllegalStateException("Value of " + key + " mismatched! " + "Excepted 0x" + Integer.toHexString(old) + " but was 0x" + Integer.toHexString((Integer) value)); } else { symbols.put(key, (Integer) value); } } return null; } @Override public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) { if (access == Opcodes.ACC_STATIC && "<clinit>".equals(name)) { return new MethodVisitor(Opcodes.ASM5) { int[] current = null; LinkedList<Integer> intStack = new LinkedList<>(); @Override public void visitIntInsn(int opcode, int operand) { if (opcode == Opcodes.NEWARRAY && operand == Opcodes.T_INT) { current = new int[intStack.pop()]; } else if (opcode == Opcodes.BIPUSH) { intStack.push(operand); } } @Override public void visitLdcInsn(Object cst) { if (cst instanceof Integer) { intStack.push((Integer) cst); } } @Override public void visitInsn(int opcode) { if (opcode >= Opcodes.ICONST_0 && opcode <= Opcodes.ICONST_5) { intStack.push(opcode - Opcodes.ICONST_0); } else if (opcode == Opcodes.IASTORE) { int value = intStack.pop(); int index = intStack.pop(); current[index] = value; } } @Override public void visitFieldInsn(int opcode, String owner, String name, String desc) { if (opcode == Opcodes.PUTSTATIC) { int[] old = styleables.get(name); if (old != null && old.length != current.length && !Arrays.equals(old, current)) { throw new IllegalStateException("Value of styleable." + name + " mismatched! " + "Excepted " + Arrays.toString(old) + " but was " + Arrays.toString(current)); } else { styleables.put(name, current); } current = null; intStack.clear(); } } }; } return null; } }; new ClassReader(bytes).accept(visitor, SKIP_DEBUG | SKIP_FRAMES); }
From source file:net.yrom.tools.ShrinkRClassVisitor.java
License:Apache License
private static void pushInt(MethodVisitor mv, int i) { if (0 <= i && i <= 5) { mv.visitInsn(Opcodes.ICONST_0 + i); // ICONST_0 ~ ICONST_5 } else if (i <= Byte.MAX_VALUE) { mv.visitIntInsn(Opcodes.BIPUSH, i); } else if (i <= Short.MAX_VALUE) { mv.visitIntInsn(Opcodes.SIPUSH, i); } else {/* w w w . j ava2s .co m*/ mv.visitLdcInsn(i); } }
From source file:org.actorsguildframework.internal.codegenerator.BeanCreator.java
License:Apache License
/** * Writes the bean constructor to the given ClassWriter. * @param beanClass the original bean class to extend * @param bcd the descriptor of the bean * @param classNameInternal the internal name of the new class * @param cw the ClassWriter to write to * @param snippetWriter if not null, this will be invoked to add a snippet * after the invocation of the super constructor *//*from w ww .j a v a2 s.c o m*/ public static void writeConstructor(Class<?> beanClass, BeanClassDescriptor bcd, String classNameInternal, ClassWriter cw, SnippetWriter snippetWriter) { String classNameDescriptor = "L" + classNameInternal + ";"; int localPropertySize = 0; ArrayList<PropertyDescriptor> localVarProperties = new ArrayList<PropertyDescriptor>(); for (int i = 0; i < bcd.getPropertyCount(); i++) { PropertyDescriptor pd = bcd.getProperty(i); if (pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null)) { localVarProperties.add(pd); localPropertySize += Type.getType(pd.getPropertyClass()).getSize(); } } final int locVarThis = 0; final int locVarController = 1; final int locVarProps = 2; final int locVarPropertiesOffset = 3; final int locVarP = 3 + localPropertySize; final int locVarK = 4 + localPropertySize; final int locVarV = 5 + localPropertySize; MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, "<init>", "(Lorg/actorsguildframework/internal/Controller;Lorg/actorsguildframework/Props;)V", null, null); mv.visitCode(); Label lTry = new Label(); Label lCatch = new Label(); mv.visitTryCatchBlock(lTry, lCatch, lCatch, "java/lang/ClassCastException"); Label lBegin = new Label(); mv.visitLabel(lBegin); mv.visitVarInsn(Opcodes.ALOAD, locVarThis); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(beanClass), "<init>", "()V"); if (snippetWriter != null) snippetWriter.write(mv); Label lPropertyInit = new Label(); mv.visitLabel(lPropertyInit); // load default values into the local variables for each property that must be set int varCount = 0; for (PropertyDescriptor pd : localVarProperties) { Type pt = Type.getType(pd.getPropertyClass()); if (pd.getDefaultValue() != null) mv.visitFieldInsn(Opcodes.GETSTATIC, Type.getInternalName(pd.getDefaultValue().getDeclaringClass()), pd.getDefaultValue().getName(), Type.getDescriptor(pd.getDefaultValue().getType())); else GenerationUtils.generateLoadDefault(mv, pd.getPropertyClass()); mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), locVarPropertiesOffset + varCount); varCount += pt.getSize(); } // loop through the props argument's list mv.visitVarInsn(Opcodes.ALOAD, locVarProps); mv.visitVarInsn(Opcodes.ASTORE, locVarP); Label lWhile = new Label(); Label lEndWhile = new Label(); Label lWhileBody = new Label(); mv.visitLabel(lWhile); mv.visitJumpInsn(Opcodes.GOTO, lEndWhile); mv.visitLabel(lWhileBody); mv.visitVarInsn(Opcodes.ALOAD, locVarP); mv.visitInsn(Opcodes.DUP); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getKey", "()Ljava/lang/String;"); mv.visitVarInsn(Opcodes.ASTORE, locVarK); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "getValue", "()Ljava/lang/Object;"); mv.visitVarInsn(Opcodes.ASTORE, locVarV); mv.visitLabel(lTry); // write an if for each property Label lEndIf = new Label(); varCount = 0; int ifCount = 0; for (int i = 0; i < bcd.getPropertyCount(); i++) { PropertyDescriptor pd = bcd.getProperty(i); boolean usesLocal = pd.getPropertySource().isGenerating() || (pd.getDefaultValue() != null); Class<?> propClass = pd.getPropertyClass(); Type pt = Type.getType(propClass); mv.visitVarInsn(Opcodes.ALOAD, locVarK); mv.visitLdcInsn(pd.getName()); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/String", "equals", "(Ljava/lang/Object;)Z"); Label lElse = new Label(); mv.visitJumpInsn(Opcodes.IFEQ, lElse); if (!usesLocal) mv.visitVarInsn(Opcodes.ALOAD, locVarThis); // for setter invocation, load 'this' if (propClass.isPrimitive()) { mv.visitLdcInsn(pd.getName()); mv.visitVarInsn(Opcodes.ALOAD, locVarV); mv.visitMethodInsn(Opcodes.INVOKESTATIC, Type.getInternalName(BeanHelper.class), String.format("get%s%sFromPropValue", propClass.getName().substring(0, 1).toUpperCase(Locale.US), propClass.getName().substring(1)), "(Ljava/lang/String;Ljava/lang/Object;)" + pt.getDescriptor()); } else if (!propClass.equals(Object.class)) { mv.visitVarInsn(Opcodes.ALOAD, locVarV); mv.visitTypeInsn(Opcodes.CHECKCAST, pt.getInternalName()); } else mv.visitVarInsn(Opcodes.ALOAD, locVarV); if (!usesLocal) mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(), Type.getMethodDescriptor(pd.getSetter())); else mv.visitVarInsn(pt.getOpcode(Opcodes.ISTORE), varCount + locVarPropertiesOffset); mv.visitJumpInsn(Opcodes.GOTO, lEndIf); mv.visitLabel(lElse); ifCount++; if (usesLocal) varCount += pt.getSize(); } // else (==> if not prop matched) throw IllegalArgumentException mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class)); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn("Unknown property \"%s\"."); mv.visitInsn(Opcodes.ICONST_1); mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object"); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_0); mv.visitVarInsn(Opcodes.ALOAD, locVarK); mv.visitInsn(Opcodes.AASTORE); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;"); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(lCatch); mv.visitInsn(Opcodes.POP); // pop the exception object (not needed) mv.visitTypeInsn(Opcodes.NEW, Type.getInternalName(IllegalArgumentException.class)); mv.visitInsn(Opcodes.DUP); mv.visitLdcInsn("Incompatible type for property \"%s\". Got %s."); mv.visitInsn(Opcodes.ICONST_2); mv.visitTypeInsn(Opcodes.ANEWARRAY, "java/lang/Object"); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_0); mv.visitVarInsn(Opcodes.ALOAD, locVarK); mv.visitInsn(Opcodes.AASTORE); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.ICONST_1); mv.visitVarInsn(Opcodes.ALOAD, locVarV); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;"); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "java/lang/Class", "getName", "()Ljava/lang/String;"); mv.visitInsn(Opcodes.AASTORE); mv.visitMethodInsn(Opcodes.INVOKESTATIC, "java/lang/String", "format", "(Ljava/lang/String;[Ljava/lang/Object;)Ljava/lang/String;"); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getInternalName(IllegalArgumentException.class), "<init>", "(Ljava/lang/String;)V"); mv.visitInsn(Opcodes.ATHROW); mv.visitLabel(lEndIf); mv.visitVarInsn(Opcodes.ALOAD, locVarP); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, "org/actorsguildframework/Props", "tail", "()Lorg/actorsguildframework/Props;"); mv.visitVarInsn(Opcodes.ASTORE, locVarP); mv.visitLabel(lEndWhile); mv.visitVarInsn(Opcodes.ALOAD, locVarP); mv.visitJumpInsn(Opcodes.IFNONNULL, lWhileBody); // write local variables back into properties varCount = 0; for (PropertyDescriptor pd : localVarProperties) { Type pt = Type.getType(pd.getPropertyClass()); mv.visitVarInsn(Opcodes.ALOAD, locVarThis); if (pd.getPropertySource() == PropertySource.ABSTRACT_METHOD) { mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount); mv.visitFieldInsn(Opcodes.PUTFIELD, classNameInternal, String.format(PROP_FIELD_NAME_TEMPLATE, pd.getName()), pt.getDescriptor()); } else if (pd.getPropertySource() == PropertySource.USER_WRITTEN) { mv.visitVarInsn(pt.getOpcode(Opcodes.ILOAD), locVarPropertiesOffset + varCount); mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classNameInternal, pd.getSetter().getName(), Type.getMethodDescriptor(pd.getSetter())); } else throw new RuntimeException("Internal error"); varCount += pt.getSize(); } // if bean is thread-safe, publish all writes now if (bcd.isThreadSafe()) { mv.visitVarInsn(Opcodes.ALOAD, locVarThis); mv.visitInsn(Opcodes.DUP); mv.visitInsn(Opcodes.MONITORENTER); mv.visitInsn(Opcodes.MONITOREXIT); } mv.visitInsn(Opcodes.RETURN); Label lEnd = new Label(); mv.visitLabel(lEnd); mv.visitLocalVariable("this", classNameDescriptor, null, lBegin, lEnd, locVarThis); mv.visitLocalVariable("controller", "Lorg/actorsguildframework/internal/Controller;", null, lBegin, lEnd, locVarController); mv.visitLocalVariable("props", "Lorg/actorsguildframework/Props;", null, lBegin, lEnd, locVarProps); varCount = 0; for (PropertyDescriptor pd : localVarProperties) { Type pt = Type.getType(pd.getPropertyClass()); mv.visitLocalVariable("__" + pd.getName(), pt.getDescriptor(), GenericTypeHelper.getSignature(pd.getPropertyType()), lPropertyInit, lEnd, locVarPropertiesOffset + varCount); varCount += pt.getSize(); } mv.visitLocalVariable("p", "Lorg/actorsguildframework/Props;", null, lPropertyInit, lEnd, locVarP); mv.visitLocalVariable("k", "Ljava/lang/String;", null, lWhile, lEndWhile, locVarK); mv.visitLocalVariable("v", "Ljava/lang/Object;", null, lWhile, lEndWhile, locVarV); mv.visitMaxs(0, 0); mv.visitEnd(); }
From source file:org.actorsguildframework.internal.codegenerator.GenerationUtils.java
License:Apache License
/** * Writes an instructions to the given MethodVisitor which will load the default * value for the given class. For primitives, it will load 0 or false in the * correct type onto the stack. For references, the generated code will put a null. * @param mv the MethodVisitor to write to * @param clazz the class to write the default for. */// w w w . java2s . com public static void generateLoadDefault(MethodVisitor mv, Class<?> clazz) { if (!clazz.isPrimitive()) mv.visitInsn(Opcodes.ACONST_NULL); else if (clazz.equals(Integer.TYPE) || clazz.equals(Character.TYPE) || clazz.equals(Short.TYPE) || clazz.equals(Byte.TYPE) || clazz.equals(Boolean.TYPE)) mv.visitInsn(Opcodes.ICONST_0); else if (clazz.equals(Long.TYPE)) mv.visitInsn(Opcodes.LCONST_0); else if (clazz.equals(Double.TYPE)) mv.visitInsn(Opcodes.DCONST_0); else if (clazz.equals(Float.TYPE)) mv.visitInsn(Opcodes.FCONST_0); else throw new RuntimeException("Oops, forgot the primitive " + clazz + "?"); }
From source file:org.adjective.stout.operation.ConstantIntegerExpression.java
License:Apache License
public static Instruction getInstruction(int value) { if (value >= -1 && value <= 5) { return new GenericInstruction(Opcodes.ICONST_0 + value); }//from w w w . ja v a 2s.c o m if (value >= -128 && value < 127) { return new IntInstruction(Opcodes.BIPUSH, value); } if (value >= -32768 && value < 32767) { return new IntInstruction(Opcodes.SIPUSH, value); } return new LoadConstantInstruction(new Integer(value)); }
From source file:org.adjective.stout.tools.StackVisualiserMethodVisitor.java
License:Apache License
public void visitInsn(int opcode) { switch (opcode) { case Opcodes.NOP: break;//from w w w . ja va 2s .c o m case Opcodes.ACONST_NULL: push("null", Object.class); break; case Opcodes.ICONST_M1: case Opcodes.ICONST_0: case Opcodes.ICONST_1: case Opcodes.ICONST_2: case Opcodes.ICONST_3: case Opcodes.ICONST_4: case Opcodes.ICONST_5: push(Integer.toString(opcode - Opcodes.ICONST_0), Type.INT_TYPE); break; case Opcodes.LCONST_0: case Opcodes.LCONST_1: push(Integer.toString(opcode - Opcodes.LCONST_0), Type.LONG_TYPE); break; case Opcodes.FCONST_0: case Opcodes.FCONST_1: case Opcodes.FCONST_2: push(Integer.toString(opcode - Opcodes.FCONST_0), Type.FLOAT_TYPE); break; case Opcodes.DCONST_0: case Opcodes.DCONST_1: push(Integer.toString(opcode - Opcodes.DCONST_0), Type.DOUBLE_TYPE); break; case Opcodes.IALOAD: case Opcodes.LALOAD: case Opcodes.FALOAD: case Opcodes.DALOAD: case Opcodes.AALOAD: case Opcodes.BALOAD: case Opcodes.CALOAD: case Opcodes.SALOAD: { Type opType = getType(Opcodes.IALOAD, opcode); StackValue idx = pop(Type.INT_TYPE); StackValue arr = popArray(opType); push(arr.description + "[" + idx.description + "]", opType); } break; case Opcodes.IASTORE: case Opcodes.LASTORE: case Opcodes.FASTORE: case Opcodes.DASTORE: case Opcodes.AASTORE: case Opcodes.BASTORE: case Opcodes.CASTORE: case Opcodes.SASTORE: { Type opType = getType(Opcodes.IASTORE, opcode); pop(opType); pop(Type.INT_TYPE); popArray(opType); } break; case Opcodes.POP: pop(); break; case Opcodes.POP2: pop(2); break; case Opcodes.DUP: push(peek()); break; case Opcodes.DUP2: push(peek(2)); push(peek(1)); break; case Opcodes.DUP_X1: { StackValue a = pop(); StackValue b = pop(); push(a); push(b); push(a); } break; case Opcodes.DUP_X2: { StackValue a = pop(); StackValue b = pop(); StackValue c = pop(); push(a); push(c); push(b); push(a); } break; case Opcodes.DUP2_X1: { StackValue a = popValue(false); StackValue b = pop(); StackValue c = pop(); push(b); push(a); push(c); push(b); push(a); } case Opcodes.DUP2_X2: { StackValue a = popValue(false); StackValue b = pop(); StackValue c = popValue(false); StackValue d = pop(); push(b); push(a); push(d); push(c); push(b); push(a); } break; case Opcodes.SWAP: { StackValue a = pop(); StackValue b = pop(); push(a); push(b); } break; case Opcodes.IADD: case Opcodes.LADD: case Opcodes.FADD: case Opcodes.DADD: math(Opcodes.IADD, opcode, "+"); break; case Opcodes.ISUB: case Opcodes.LSUB: case Opcodes.FSUB: case Opcodes.DSUB: math(Opcodes.ISUB, opcode, "-"); break; case Opcodes.IMUL: case Opcodes.LMUL: case Opcodes.FMUL: case Opcodes.DMUL: math(Opcodes.IMUL, opcode, "*"); break; case Opcodes.IDIV: case Opcodes.LDIV: case Opcodes.FDIV: case Opcodes.DDIV: math(Opcodes.IDIV, opcode, "/"); break; case Opcodes.IREM: case Opcodes.LREM: case Opcodes.FREM: case Opcodes.DREM: math(Opcodes.IREM, opcode, "%"); break; case Opcodes.IAND: case Opcodes.LAND: math(Opcodes.IAND, opcode, "&"); break; case Opcodes.IOR: case Opcodes.LOR: math(Opcodes.IOR, opcode, "|"); break; case Opcodes.IXOR: case Opcodes.LXOR: math(Opcodes.IXOR, opcode, "^"); break; case Opcodes.INEG: case Opcodes.LNEG: case Opcodes.FNEG: case Opcodes.DNEG: { Type type = getType(Opcodes.INEG, opcode); StackValue a = pop(type); push("-" + a.description, type); } break; case Opcodes.ISHL: case Opcodes.LSHL: { Type type = getType(Opcodes.ISHL, opcode); StackValue n = pop(Type.INT_TYPE); StackValue a = pop(type); push(a.description + "<<" + n.description, type); } break; case Opcodes.ISHR: case Opcodes.LSHR: { Type type = getType(Opcodes.ISHR, opcode); StackValue n = pop(Type.INT_TYPE); StackValue a = pop(type); push(a.description + ">>" + n.description, type); } break; case Opcodes.IUSHR: case Opcodes.LUSHR: { Type type = getType(Opcodes.IUSHR, opcode); StackValue n = pop(Type.INT_TYPE); StackValue a = pop(type); push(a.description + ">>>" + n.description, type); } case Opcodes.LCMP: { StackValue a = pop(Type.LONG_TYPE); StackValue b = pop(Type.LONG_TYPE); push(a.description + " cmp " + b.description + " {-1|0|1}", Type.LONG_TYPE); } break; case Opcodes.I2L: case Opcodes.I2F: case Opcodes.I2D: case Opcodes.L2I: case Opcodes.L2F: case Opcodes.L2D: case Opcodes.F2I: case Opcodes.F2L: case Opcodes.F2D: case Opcodes.D2I: case Opcodes.D2L: case Opcodes.D2F: case Opcodes.I2B: case Opcodes.I2C: case Opcodes.I2S: cast(opcode); break; case Opcodes.ARETURN: case Opcodes.ATHROW: popObject(); break; case Opcodes.RETURN: break; default: throw new IllegalArgumentException("Unsupported opcode " + opcode + " - " + OPCODES[opcode]); } print(opcode, ""); /* * FCMPL, FCMPG, DCMPL, DCMPG, IRETURN, LRETURN, * FRETURN, DRETURN, ARETURN, RETURN, ARRAYLENGTH, ATHROW, * MONITORENTER, or MONITOREXIT */ }
From source file:org.apache.aries.proxy.impl.gen.ProxySubclassAdapter.java
License:Apache License
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {//from w ww.jav a 2s .co m LOGGER.debug(Constants.LOG_ENTRY, "visit", new Object[] { version, access, name, signature, superName, interfaces }); // store the superclass binary name this.superclassBinaryName = name.replaceAll("/", "\\."); try { this.superclassClass = Class.forName(superclassBinaryName, false, loader); } catch (ClassNotFoundException cnfe) { throw new TypeNotPresentException(superclassBinaryName, cnfe); } // keep the same access and signature as the superclass (unless it's abstract) // remove all the superclass interfaces because they will be inherited // from the superclass anyway if ((access & ACC_ABSTRACT) != 0) { //If the super was abstract the subclass should not be! access &= ~ACC_ABSTRACT; } cv.visit(ProxyUtils.getWeavingJavaVersion(), access, newClassName, signature, name, null); // add a private field for the invocation handler // this isn't static in case we have multiple instances of the same // proxy cv.visitField(ACC_PRIVATE, IH_FIELD, Type.getDescriptor(InvocationHandler.class), null, null); // create a static adapter for generating a static initialiser method in // the generated subclass staticAdapter = new GeneratorAdapter(ACC_STATIC, new Method("<clinit>", Type.VOID_TYPE, NO_ARGS), null, null, cv); // add a zero args constructor method Method m = new Method("<init>", Type.VOID_TYPE, NO_ARGS); GeneratorAdapter methodAdapter = new GeneratorAdapter(ACC_PUBLIC, m, null, null, cv); // loadthis methodAdapter.loadThis(); // List the constructors in the superclass. Constructor<?>[] constructors = superclassClass.getDeclaredConstructors(); // Check that we've got at least one constructor, and get the 1st one in the list. if (constructors.length > 0) { // We now need to construct the proxy class as though it is going to invoke the superclasses constructor. // We do this because we can no longer call the java.lang.Object() zero arg constructor as the JVM now throws a VerifyError. // So what we do is build up the calling of the superclasses constructor using nulls and default values. This means that the // class bytes can be verified by the JVM, and then in the ProxySubclassGenerator, we load the class without invoking the // constructor. Method constructor = Method.getMethod(constructors[0]); Type[] argTypes = constructor.getArgumentTypes(); if (argTypes.length == 0) { methodAdapter.invokeConstructor(Type.getType(superclassClass), new Method("<init>", Type.VOID_TYPE, NO_ARGS)); } else { for (Type type : argTypes) { switch (type.getSort()) { case Type.ARRAY: // We need to process any array or multidimentional arrays. String elementDesc = type.getElementType().getDescriptor(); String typeDesc = type.getDescriptor(); // Iterate over the number of arrays and load 0 for each one. Keep a count of the number of // arrays as we will need to run different code fo multi dimentional arrays. int index = 0; while (!elementDesc.equals(typeDesc)) { typeDesc = typeDesc.substring(1); methodAdapter.visitInsn(Opcodes.ICONST_0); index++; } // If we're just a single array, then call the newArray method, otherwise use the MultiANewArray instruction. if (index == 1) { methodAdapter.newArray(type.getElementType()); } else { methodAdapter.visitMultiANewArrayInsn(type.getDescriptor(), index); } break; case Type.BOOLEAN: methodAdapter.push(true); break; case Type.BYTE: methodAdapter.push(Type.VOID_TYPE); break; case Type.CHAR: methodAdapter.push(Type.VOID_TYPE); break; case Type.DOUBLE: methodAdapter.push(0.0); break; case Type.FLOAT: methodAdapter.push(0.0f); break; case Type.INT: methodAdapter.push(0); break; case Type.LONG: methodAdapter.push(0l); break; case Type.SHORT: methodAdapter.push(0); break; default: case Type.OBJECT: methodAdapter.visitInsn(Opcodes.ACONST_NULL); break; } } methodAdapter.invokeConstructor(Type.getType(superclassClass), new Method("<init>", Type.VOID_TYPE, argTypes)); } } methodAdapter.returnValue(); methodAdapter.endMethod(); // add a method for getting the invocation handler Method setter = new Method("setInvocationHandler", Type.VOID_TYPE, new Type[] { IH_TYPE }); m = new Method("getInvocationHandler", IH_TYPE, NO_ARGS); methodAdapter = new GeneratorAdapter(ACC_PUBLIC | ACC_FINAL, m, null, null, cv); // load this to get the field methodAdapter.loadThis(); // get the ih field and return methodAdapter.getField(newClassType, IH_FIELD, IH_TYPE); methodAdapter.returnValue(); methodAdapter.endMethod(); // add a method for setting the invocation handler methodAdapter = new GeneratorAdapter(ACC_PUBLIC | ACC_FINAL, setter, null, null, cv); // load this to put the field methodAdapter.loadThis(); // load the method arguments (i.e. the invocation handler) to the stack methodAdapter.loadArgs(); // set the ih field using the method argument methodAdapter.putField(newClassType, IH_FIELD, IH_TYPE); methodAdapter.returnValue(); methodAdapter.endMethod(); // loop through the class hierarchy to get any needed methods off the // supertypes // start by finding the methods declared on the class of interest (the // superclass of our dynamic subclass) java.lang.reflect.Method[] observedMethods = superclassClass.getDeclaredMethods(); // add the methods to a set of observedMethods ProxySubclassMethodHashSet<String> setOfObservedMethods = new ProxySubclassMethodHashSet<String>( observedMethods.length); setOfObservedMethods.addMethodArray(observedMethods); // get the next superclass in the hierarchy Class<?> nextSuperClass = superclassClass.getSuperclass(); while (nextSuperClass != null) { // set the fields for the current class setCurrentAnalysisClassFields(nextSuperClass); // add a static field and static initializer code to the generated // subclass // for each of the superclasses in the hierarchy addClassStaticField(currentlyAnalysedClassName); LOGGER.debug("Class currently being analysed: {} {}", currentlyAnalysedClassName, currentlyAnalysedClass); // now find the methods declared on the current class and add them // to a set of foundMethods java.lang.reflect.Method[] foundMethods = currentlyAnalysedClass.getDeclaredMethods(); ProxySubclassMethodHashSet<String> setOfFoundMethods = new ProxySubclassMethodHashSet<String>( foundMethods.length); setOfFoundMethods.addMethodArray(foundMethods); // remove from the set of foundMethods any methods we saw on a // subclass // because we want to use the lowest level declaration of a method setOfFoundMethods.removeAll(setOfObservedMethods); try { // read the current class and use a // ProxySubclassHierarchyAdapter // to process only methods on that class that are in the list ClassLoader loader = currentlyAnalysedClass.getClassLoader(); if (loader == null) { loader = this.loader; } ClassReader cr = new ClassReader(loader .getResourceAsStream(currentlyAnalysedClass.getName().replaceAll("\\.", "/") + ".class")); ClassVisitor hierarchyAdapter = new ProxySubclassHierarchyAdapter(this, setOfFoundMethods); cr.accept(hierarchyAdapter, ClassReader.SKIP_DEBUG); } catch (IOException e) { throw new TypeNotPresentException(currentlyAnalysedClassName, e); } // now add the foundMethods to the overall list of observed methods setOfObservedMethods.addAll(setOfFoundMethods); // get the next class up in the hierarchy and go again nextSuperClass = currentlyAnalysedClass.getSuperclass(); } // we've finished looking at the superclass hierarchy // set the fields for the immediate superclass of our dynamic subclass setCurrentAnalysisClassFields(superclassClass); // add the class static field addClassStaticField(currentlyAnalysedClassName); // we do the lowest class last because we are already visiting the class // when in this adapter code // now we are ready to visit all the methods on the lowest class // which will happen by the ASM ClassVisitor implemented in this adapter LOGGER.debug(Constants.LOG_EXIT, "visit"); }
From source file:org.apache.drill.exec.compile.AsmUtil.java
License:Apache License
/** * Determine if the given opcode is a load of a constant (xCONST_y). * * @param opcode the opcode/*from w w w. j a v a2 s . co m*/ * @return true if the opcode is one of the constant loading ones, false otherwise */ public static boolean isXconst(final int opcode) { switch (opcode) { case Opcodes.ICONST_0: case Opcodes.ICONST_1: case Opcodes.ICONST_2: case Opcodes.ICONST_3: case Opcodes.ICONST_4: case Opcodes.ICONST_5: case Opcodes.ICONST_M1: case Opcodes.DCONST_0: case Opcodes.DCONST_1: case Opcodes.FCONST_0: case Opcodes.FCONST_1: case Opcodes.LCONST_0: case Opcodes.LCONST_1: return true; } return false; }
From source file:org.apache.drill.exec.compile.bytecode.ValueHolderIden.java
License:Apache License
private static void initType(int index, Type t, DirectSorter v) { switch (t.getSort()) { case Type.BOOLEAN: case Type.BYTE: case Type.CHAR: case Type.SHORT: case Type.INT: v.visitInsn(Opcodes.ICONST_0); v.directVarInsn(Opcodes.ISTORE, index); break;/*from w ww . java2s . c om*/ case Type.LONG: v.visitInsn(Opcodes.LCONST_0); v.directVarInsn(Opcodes.LSTORE, index); break; case Type.FLOAT: v.visitInsn(Opcodes.FCONST_0); v.directVarInsn(Opcodes.FSTORE, index); break; case Type.DOUBLE: v.visitInsn(Opcodes.DCONST_0); v.directVarInsn(Opcodes.DSTORE, index); break; case Type.OBJECT: v.visitInsn(Opcodes.ACONST_NULL); v.directVarInsn(Opcodes.ASTORE, index); break; default: throw new UnsupportedOperationException(); } }
From source file:org.boretti.drools.integration.drools5.DroolsAddConstructorMethodVisitor.java
License:Open Source License
@Override protected void onMethodEnter() { this.visitVarInsn(Opcodes.ALOAD, 0); this.visitInsn(Opcodes.DUP); this.visitInsn(Opcodes.DUP); this.visitInsn(Opcodes.ICONST_0); this.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(parent).getInternalName(), droolsName, Type.BOOLEAN_TYPE.getDescriptor()); this.visitTypeInsn(Opcodes.NEW, Type.getType(DroolsProvider.class).getInternalName()); this.visitInsn(Opcodes.DUP); this.visitMethodInsn(Opcodes.INVOKESPECIAL, Type.getType(DroolsProvider.class).getInternalName(), "<init>", "()V"); this.visitInsn(Opcodes.SWAP); this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getObjectType(parent).getInternalName(), "getClass", "()Ljava/lang/Class;"); if (overrideNameJVMIndex >= 0) { this.visitVarInsn(Opcodes.ALOAD, overrideNameJVMIndex); }/*from w w w . java 2 s . c om*/ if (overrideTypeJVMIndex >= 0) { this.visitVarInsn(Opcodes.ALOAD, overrideTypeJVMIndex); } if (overrideNameJVMIndex < 0 && overrideTypeJVMIndex < 0) { this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(), "getRuleBase", "(Ljava/lang/Class;)Lorg/drools/RuleBase;"); } else if (overrideNameJVMIndex < 0 && overrideTypeJVMIndex >= 0) { this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(), "getRuleBaseOverride", "(Ljava/lang/Class;Lorg/boretti/drools/integration/drools5/DroolsServiceType;)Lorg/drools/RuleBase;"); } else if (overrideNameJVMIndex >= 0 && overrideTypeJVMIndex < 0) { this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(), "getRuleBaseOverride", "(Ljava/lang/Class;Ljava/lang/String;)Lorg/drools/RuleBase;"); } else { this.visitMethodInsn(Opcodes.INVOKEVIRTUAL, Type.getType(DroolsProvider.class).getInternalName(), "getRuleBaseOverride", "(Ljava/lang/Class;Ljava/lang/String;Lorg/boretti/drools/integration/drools5/DroolsServiceType;)Lorg/drools/RuleBase;"); } this.visitFieldInsn(Opcodes.PUTFIELD, Type.getObjectType(parent).getInternalName(), droolsRule, Type.getType(RuleBase.class).getDescriptor()); }