Search in sources :

Example 1 with Type

use of org.apache.xbean.asm6.Type in project tomee by apache.

the class CmrField method createSignature.

private static String createSignature(final Type type, final Type... genericTypes) {
    final StringBuilder builder = new StringBuilder();
    builder.append("L").append(type.getInternalName());
    if (genericTypes.length > 0) {
        builder.append("<");
        for (final Type genericType : genericTypes) {
            builder.append(genericType.getDescriptor());
        }
        builder.append(">");
    }
    builder.append(";");
    return builder.toString();
}
Also used : Type(org.apache.xbean.asm5.Type)

Example 2 with Type

use of org.apache.xbean.asm6.Type in project tomee by apache.

the class DynamicSubclass method visitConstructor.

private static MethodVisitor visitConstructor(final ClassWriter cw, final String proxyClassFileName, final String classFileName, final Constructor<?> constructor) {
    final String descriptor = Type.getConstructorDescriptor(constructor);
    final String[] exceptions = new String[constructor.getExceptionTypes().length];
    for (int i = 0; i < exceptions.length; i++) {
        exceptions[i] = Type.getInternalName(constructor.getExceptionTypes()[i]);
    }
    final MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", descriptor, null, exceptions);
    mv.visitCode();
    mv.visitVarInsn(ALOAD, 0);
    int index = 1;
    for (final Type type : Type.getArgumentTypes(descriptor)) {
        mv.visitVarInsn(type.getOpcode(ILOAD), index);
        index += size(type);
    }
    mv.visitMethodInsn(INVOKESPECIAL, classFileName, "<init>", descriptor, false);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitVarInsn(ALOAD, 0);
    mv.visitFieldInsn(PUTFIELD, proxyClassFileName, "this$handler", "Ljava/lang/reflect/InvocationHandler;");
    mv.visitInsn(RETURN);
    mv.visitMaxs(2, 1);
    return mv;
}
Also used : Type(org.apache.xbean.asm5.Type) MethodVisitor(org.apache.xbean.asm5.MethodVisitor)

Example 3 with Type

use of org.apache.xbean.asm6.Type in project component-runtime by Talend.

the class ProxyGenerator method delegateMethod.

private void delegateMethod(final ClassWriter cw, final Method method, final String proxyClassFileName, final int methodIndex) {
    final Class<?> returnType = method.getReturnType();
    final Class<?>[] parameterTypes = method.getParameterTypes();
    final Class<?>[] exceptionTypes = method.getExceptionTypes();
    final int modifiers = method.getModifiers();
    if (Modifier.isFinal(modifiers) || Modifier.isStatic(modifiers)) {
        throw new IllegalStateException("It's not possible to proxy a final or static method: " + method.getDeclaringClass().getName() + " " + method.getName());
    }
    // push the method definition
    int modifier = modifiers & (ACC_PUBLIC | ACC_PROTECTED | ACC_VARARGS);
    MethodVisitor mv = cw.visitMethod(modifier, method.getName(), Type.getMethodDescriptor(method), null, null);
    mv.visitCode();
    // push try/catch block, to catch declared exceptions, and to catch java.lang.Throwable
    final Label l0 = new Label();
    final Label l1 = new Label();
    final Label l2 = new Label();
    if (exceptionTypes.length > 0) {
        mv.visitTryCatchBlock(l0, l1, l2, "java/lang/reflect/InvocationTargetException");
    }
    // push try code
    mv.visitLabel(l0);
    final String classNameToOverride = method.getDeclaringClass().getName().replace('.', '/');
    mv.visitLdcInsn(Type.getType("L" + classNameToOverride + ";"));
    // the following code generates the bytecode for this line of Java:
    // Method method = <proxy>.class.getMethod("add", new Class[] { <array of function argument classes> });
    // get the method name to invoke, and push to stack
    mv.visitLdcInsn(method.getName());
    // create the Class[]
    createArrayDefinition(mv, parameterTypes.length, Class.class);
    int length = 1;
    // push parameters into array
    for (int i = 0; i < parameterTypes.length; i++) {
        // keep copy of array on stack
        mv.visitInsn(DUP);
        Class<?> parameterType = parameterTypes[i];
        // push number onto stack
        pushIntOntoStack(mv, i);
        if (parameterType.isPrimitive()) {
            String wrapperType = getWrapperType(parameterType);
            mv.visitFieldInsn(GETSTATIC, wrapperType, "TYPE", "Ljava/lang/Class;");
        } else {
            mv.visitLdcInsn(Type.getType(parameterType));
        }
        mv.visitInsn(AASTORE);
        if (Long.TYPE.equals(parameterType) || Double.TYPE.equals(parameterType)) {
            length += 2;
        } else {
            length++;
        }
    }
    // the following code generates bytecode equivalent to:
    // return ((<returntype>) invocationHandler.invoke(this, {methodIndex}, new Object[] { <function arguments
    // }))[.<primitive>Value()];
    final Label l4 = new Label();
    mv.visitLabel(l4);
    mv.visitVarInsn(ALOAD, 0);
    // get the invocationHandler field from this class
    mv.visitFieldInsn(GETFIELD, proxyClassFileName, FIELD_INTERCEPTOR_HANDLER, Type.getDescriptor(InterceptorHandler.class));
    // add the Method from the static array as first parameter
    mv.visitFieldInsn(GETSTATIC, proxyClassFileName, FIELD_INTERCEPTED_METHODS, Type.getDescriptor(Method[].class));
    // push the methodIndex of the current method
    if (methodIndex < 128) {
        mv.visitIntInsn(BIPUSH, methodIndex);
    } else if (methodIndex < 32267) {
        // for methods > 127 we need to push a short number as index
        mv.visitIntInsn(SIPUSH, methodIndex);
    } else {
        throw new IllegalStateException("Sorry, we only support Classes with 2^15 methods...");
    }
    // and now load the Method from the array
    mv.visitInsn(AALOAD);
    // prepare the parameter array as Object[] and store it on the stack
    pushMethodParameterArray(mv, parameterTypes);
    // invoke the invocationHandler
    mv.visitMethodInsn(INVOKEINTERFACE, Type.getInternalName(InterceptorHandler.class), "invoke", "(Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object;", true);
    // cast the result
    mv.visitTypeInsn(CHECKCAST, getCastType(returnType));
    if (returnType.isPrimitive() && (!Void.TYPE.equals(returnType))) {
        // get the primitive value
        mv.visitMethodInsn(INVOKEVIRTUAL, getWrapperType(returnType), getPrimitiveMethod(returnType), "()" + Type.getDescriptor(returnType), false);
    }
    // push return
    mv.visitLabel(l1);
    if (!Void.TYPE.equals(returnType)) {
        mv.visitInsn(getReturnInsn(returnType));
    } else {
        mv.visitInsn(POP);
        mv.visitInsn(RETURN);
    }
    // catch InvocationTargetException
    if (exceptionTypes.length > 0) {
        mv.visitLabel(l2);
        mv.visitVarInsn(ASTORE, length);
        Label l5 = new Label();
        mv.visitLabel(l5);
        for (int i = 0; i < exceptionTypes.length; i++) {
            Class<?> exceptionType = exceptionTypes[i];
            mv.visitLdcInsn(Type.getType("L" + exceptionType.getCanonicalName().replace('.', '/') + ";"));
            mv.visitVarInsn(ALOAD, length);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "getClass", "()Ljava/lang/Class;", false);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false);
            final Label l6 = new Label();
            mv.visitJumpInsn(IFEQ, l6);
            final Label l7 = new Label();
            mv.visitLabel(l7);
            mv.visitVarInsn(ALOAD, length);
            mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/reflect/InvocationTargetException", "getCause", "()Ljava/lang/Throwable;", false);
            mv.visitTypeInsn(CHECKCAST, getCastType(exceptionType));
            mv.visitInsn(ATHROW);
            mv.visitLabel(l6);
            if (i == (exceptionTypes.length - 1)) {
                mv.visitTypeInsn(NEW, "java/lang/reflect/UndeclaredThrowableException");
                mv.visitInsn(DUP);
                mv.visitVarInsn(ALOAD, length);
                mv.visitMethodInsn(INVOKESPECIAL, "java/lang/reflect/UndeclaredThrowableException", "<init>", "(Ljava/lang/Throwable;)V", false);
                mv.visitInsn(ATHROW);
            }
        }
    }
    // finish this method
    mv.visitMaxs(0, 0);
    mv.visitEnd();
}
Also used : InterceptorHandler(org.talend.sdk.component.api.service.interceptor.InterceptorHandler) Label(org.apache.xbean.asm6.Label) MethodVisitor(org.apache.xbean.asm6.MethodVisitor)

Example 4 with Type

use of org.apache.xbean.asm6.Type in project jodd by oblac.

the class BigClassTest method testAllFeatures.

@Test
public void testAllFeatures() throws IOException, IllegalAccessException, InstantiationException {
    StatCounter.counter = 0;
    final MutableBoolean firstTime = new MutableBoolean(true);
    ProxyAspect aspect = new ProxyAspect(StatCounterAdvice.class, new ProxyPointcutSupport() {

        public boolean apply(MethodInfo mi) {
            if (firstTime.value) {
                firstTime.value = false;
                ClassInfo ci = mi.getClassInfo();
                assertEquals("BigFatJoe", ci.getClassname());
                assertEquals(BigFatJoe.class.getPackage().getName(), ci.getPackage());
                assertEquals("jodd/proxetta/data/BigFatJoe", ci.getReference());
                assertEquals("jodd/proxetta/data/SmallSkinnyZoe", ci.getSuperName());
                AnnotationInfo[] anns = ci.getAnnotations();
                assertNotNull(anns);
                assertEquals(3, anns.length);
                AnnotationInfo ai = anns[0];
                assertSame(ai, getAnnotation(ci, MadvocAction.class));
                assertEquals(MadvocAction.class.getName(), ai.getAnnotationClassname());
                assertEquals("L" + MadvocAction.class.getName().replace('.', '/') + ";", ai.getAnnotationSignature());
                assertEquals("madvocAction", ai.getElement("value"));
                ai = anns[1];
                assertSame(ai, getAnnotation(ci, PetiteBean.class));
                assertEquals(PetiteBean.class.getName(), ai.getAnnotationClassname());
                assertEquals("L" + PetiteBean.class.getName().replace('.', '/') + ";", ai.getAnnotationSignature());
                ai = anns[2];
                assertSame(ai, getAnnotation(ci, InterceptedBy.class));
                assertEquals(InterceptedBy.class.getName(), ai.getAnnotationClassname());
                assertEquals("L" + InterceptedBy.class.getName().replace('.', '/') + ";", ai.getAnnotationSignature());
                assertTrue(ai.getElement("value") instanceof Object[]);
                assertFalse(ai.getElement("value") instanceof String[]);
                Object c1 = ((Object[]) ai.getElement("value"))[0];
                assertEquals("Ljodd/proxetta/data/Str;", ((Type) c1).getDescriptor());
            }
            if (mi.getMethodName().equals("publicMethod")) {
                AnnotationInfo[] anns = mi.getAnnotations();
                assertNotNull(anns);
                assertEquals(3, anns.length);
                AnnotationInfo ai = anns[0];
                assertSame(ai, getAnnotation(mi, Action.class));
                assertEquals(Action.class.getName(), ai.getAnnotationClassname());
                assertEquals("value", ai.getElement("value"));
                assertEquals("alias", ai.getElement("alias"));
                ai = anns[1];
                assertSame(ai, getAnnotation(mi, PetiteInject.class));
                assertEquals(PetiteInject.class.getName(), ai.getAnnotationClassname());
                assertEquals(0, ai.getElementNames().size());
                ai = anns[2];
                assertSame(ai, getAnnotation(mi, Transaction.class));
                assertEquals(Transaction.class.getName(), ai.getAnnotationClassname());
                assertEquals(2, ai.getElementNames().size());
                String s = (String) ai.getElement("propagation");
                assertEquals("PROPAGATION_REQUIRES_NEW", s);
            }
            if (mi.getMethodName().equals("superPublicMethod")) {
                AnnotationInfo[] anns = mi.getAnnotations();
                assertNotNull(anns);
                assertEquals(3, anns.length);
                AnnotationInfo ai = anns[0];
                assertSame(ai, getAnnotation(mi, Action.class));
                assertEquals(Action.class.getName(), ai.getAnnotationClassname());
                assertEquals(0, ai.getElementNames().size());
                ai = anns[1];
                assertSame(ai, getAnnotation(mi, PetiteInject.class));
                assertEquals(PetiteInject.class.getName(), ai.getAnnotationClassname());
                assertEquals(0, ai.getElementNames().size());
                ai = anns[2];
                assertSame(ai, getAnnotation(mi, Transaction.class));
                assertEquals(Transaction.class.getName(), ai.getAnnotationClassname());
                assertEquals(0, ai.getElementNames().size());
            }
            //System.out.println(!isRootMethod(mi) + " " + mi.getDeclaredClassName() + '#' + mi.getMethodName());
            return !isRootMethod(mi);
        }
    });
    byte[] classBytes = ProxyProxetta.withAspects(aspect).builder(BigFatJoe.class).create();
    //		URL resource = BigFatJoe.class.getResource("/" + BigFatJoe.class.getName().replace(".", "/") + ".class");
    //		jodd.io.FileUtil.copy(FileUtil.toFile(resource), new java.io.File(SystemUtil.getUserHome(), "jo.class"));
    //		jodd.io.FileUtil.writeBytes(new java.io.File(SystemUtil.getUserHome(), "joe.class"), classBytes);
    Class clazz = ClassLoaderUtil.defineClass(null, classBytes);
    BigFatJoe bigFatJoe = (BigFatJoe) clazz.newInstance();
    assertEquals(BigFatJoe.class.getName() + "$$Proxetta", bigFatJoe.getClass().getName());
    assertEquals(BigFatJoe.class, ProxettaUtil.getTargetClass(bigFatJoe.getClass()));
    // test invocation
    // 2 x static + 1 x instance
    assertEquals(3, StatCounter.counter);
    bigFatJoe.publicMethod();
    assertEquals(4, StatCounter.counter);
    bigFatJoe.callInnerMethods();
    // private method is not overridden
    assertEquals(7, StatCounter.counter);
    bigFatJoe.superPublicMethod();
    assertEquals(8, StatCounter.counter);
    bigFatJoe.callInnerMethods2();
    // only public super methods are overridden
    assertEquals(9, StatCounter.counter);
    // test class annotation
    MadvocAction ma = (MadvocAction) clazz.getAnnotation(MadvocAction.class);
    assertEquals("madvocAction", ma.value());
    InterceptedBy ib = (InterceptedBy) clazz.getAnnotation(InterceptedBy.class);
    assertNotNull(ib.value());
    assertEquals(2, ib.value().length);
    // test method annotation
    ClassDescriptor cd = ClassIntrospector.lookup(clazz);
    Method m = cd.getMethodDescriptor("publicMethod", false).getMethod();
    assertNotNull(m);
    Annotation[] aa = m.getAnnotations();
    assertEquals(3, aa.length);
    Action a = (Action) aa[0];
    assertEquals("alias", a.alias());
    assertEquals("extension", a.extension());
    assertEquals("method", a.method());
    assertEquals("value", a.value());
    PetiteInject pi = (PetiteInject) aa[1];
    assertEquals("", pi.value());
    Transaction tx = (Transaction) aa[2];
    assertTrue(tx.readOnly());
    assertEquals(1000, tx.timeout());
    assertEquals("PROPAGATION_REQUIRES_NEW", tx.propagation());
    bigFatJoe.runInnerClass();
    // proxy + call
    assertEquals(11, StatCounter.counter);
}
Also used : ClassDescriptor(jodd.introspector.ClassDescriptor) MutableBoolean(jodd.mutable.MutableBoolean) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Type(jodd.asm5.Type) ProxyPointcutSupport(jodd.proxetta.pointcuts.ProxyPointcutSupport) Test(org.junit.Test)

Example 5 with Type

use of org.apache.xbean.asm6.Type in project jodd by oblac.

the class MethodFinder method visitMethod.

@Override
public MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions) {
    if (paramExtractor != null) {
        // method already found, skip all further methods
        return null;
    }
    if (!name.equals(methodName)) {
        // different method
        return null;
    }
    Type[] argumentTypes = Type.getArgumentTypes(desc);
    int dwordsCount = 0;
    for (Type t : argumentTypes) {
        if (t.getClassName().equals(TYPE_LONG) || t.getClassName().equals(TYPE_DOUBLE)) {
            dwordsCount++;
        }
    }
    int paramCount = argumentTypes.length;
    if (paramCount != this.parameterTypes.length) {
        // different number of params
        return null;
    }
    for (int i = 0; i < argumentTypes.length; i++) {
        if (!isEqualTypeName(argumentTypes[i], this.parameterTypes[i])) {
            // wrong param types
            return null;
        }
    }
    this.paramExtractor = new ParamExtractor((Modifier.isStatic(access) ? 0 : 1), argumentTypes.length + dwordsCount);
    return paramExtractor;
}
Also used : Type(jodd.asm5.Type)

Aggregations

Type (org.apache.xbean.asm5.Type)4 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 MatchEntry (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntry)3 MatchEntryBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entries.grouping.MatchEntryBuilder)3 Icmpv4TypeCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv4TypeCaseBuilder)3 Icmpv6TypeCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.Icmpv6TypeCaseBuilder)3 TcpSrcCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.TcpSrcCaseBuilder)3 Icmpv4TypeBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv4.type._case.Icmpv4TypeBuilder)3 Icmpv6TypeBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.icmpv6.type._case.Icmpv6TypeBuilder)3 TcpSrcBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.tcp.src._case.TcpSrcBuilder)3 Type (jodd.asm5.Type)2 Dscp (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Dscp)2 Ipv4Address (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address)2 MacAddress (org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.MacAddress)2 EtherType (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.EtherType)2 PortNumber (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortNumber)2 IpDscp (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.IpDscp)2 EthDstCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthDstCaseBuilder)2 EthSrcCaseBuilder (org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.oxm.rev150225.match.entry.value.grouping.match.entry.value.EthSrcCaseBuilder)2