Search in sources :

Example 1 with CtConstructor

use of javassist.CtConstructor in project java-chassis by ServiceComb.

the class JavassistUtils method addEnumConstructor.

private static void addEnumConstructor(CtClass ctClass) throws Exception {
    String src = "super($1, $2);";
    CtConstructor ctConstructor = new CtConstructor(POOL.get(new String[] { String.class.getName(), int.class.getName() }), ctClass);
    ctConstructor.setBody(src);
    ctClass.addConstructor(ctConstructor);
}
Also used : CtConstructor(javassist.CtConstructor)

Example 2 with CtConstructor

use of javassist.CtConstructor in project afterburner by stephanenicolas.

the class AfterBurner method extractExistingConstructors.

private List<CtConstructor> extractExistingConstructors(final InsertableConstructor insertableConstructor) throws NotFoundException, AfterBurnerImpossibleException {
    List<CtConstructor> constructors = new ArrayList<CtConstructor>();
    CtConstructor[] declaredConstructors = insertableConstructor.getClassToInsertInto().getDeclaredConstructors();
    for (CtConstructor constructor : declaredConstructors) {
        CtClass[] paramClasses = constructor.getParameterTypes();
        if (insertableConstructor.acceptParameters(paramClasses)) {
            constructors.add(constructor);
        }
    }
    return constructors;
}
Also used : CtClass(javassist.CtClass) ArrayList(java.util.ArrayList) CtConstructor(javassist.CtConstructor)

Example 3 with CtConstructor

use of javassist.CtConstructor in project powermock by powermock.

the class TestClassTransformer method addConstructorNotification.

private void addConstructorNotification(final CtClass clazz) throws CannotCompileException {
    final String notificationCode = GlobalNotificationBuildSupport.class.getName() + ".testInstanceCreated(this);";
    final boolean asFinally = !hasSuperClass(clazz);
    for (final CtConstructor constr : clazz.getDeclaredConstructors()) {
        constr.insertAfter(notificationCode, asFinally);
    }
}
Also used : GlobalNotificationBuildSupport(org.powermock.core.testlisteners.GlobalNotificationBuildSupport) CtConstructor(javassist.CtConstructor)

Example 4 with CtConstructor

use of javassist.CtConstructor in project BIMserver by opensourceBIM.

the class RealtimeReflectorFactoryBuilder method build1.

private void build1(Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
    try {
        CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Impl" + implementationCounter);
        reflectorImplClass.addInterface(pool.get(interfaceClass.getName()));
        CtClass reflectorClass = pool.get(Reflector.class.getName());
        CtField reflectorField = new CtField(reflectorClass, "reflector", reflectorImplClass);
        reflectorImplClass.addField(reflectorField);
        CtConstructor constructor = new CtConstructor(new CtClass[] { reflectorClass }, reflectorImplClass);
        StringBuilder sb = new StringBuilder();
        reflectorImplClass.addConstructor(constructor);
        sb.append("{");
        sb.append("this.reflector = $1;");
        sb.append("}");
        constructor.setBody(sb.toString());
        for (SMethod sMethod : sService.getMethods()) {
            CtClass[] parameters = new CtClass[sMethod.getParameters().size()];
            int i = 0;
            for (org.bimserver.shared.meta.SParameter sParameter : sMethod.getParameters()) {
                parameters[i] = pool.get(sParameter.getType().toJavaCode());
                i++;
            }
            CtMethod method = new CtMethod(pool.get(sMethod.getReturnType().toJavaCode()), sMethod.getName(), parameters, reflectorImplClass);
            StringBuilder methodBuilder = new StringBuilder();
            methodBuilder.append("{");
            if (sMethod.getReturnType().isVoid()) {
            } else {
                methodBuilder.append("return (" + sMethod.getReturnType().toJavaCode() + ")");
            }
            methodBuilder.append("reflector.callMethod(\"" + interfaceClass.getSimpleName() + "\", \"" + sMethod.getName() + "\", " + sMethod.getReturnType().toJavaCode() + ".class");
            if (sMethod.getParameters().isEmpty()) {
                methodBuilder.append(", new " + KeyValuePair.class.getName() + "[0]");
            } else {
                methodBuilder.append(", new " + KeyValuePair.class.getName() + "[]{");
                int x = 1;
                for (SParameter sParameter : sMethod.getParameters()) {
                    methodBuilder.append("new " + KeyValuePair.class.getName() + "(\"" + sParameter.getName() + "\", $" + x + ")");
                    if (sMethod.getParameter(sMethod.getParameters().size() - 1) != sParameter) {
                        methodBuilder.append(", ");
                    }
                    x++;
                }
                methodBuilder.append("}");
            }
            methodBuilder.append(");");
            methodBuilder.append("}");
            method.setBody(methodBuilder.toString());
            reflectorImplClass.addMethod(method);
        }
        pool.toClass(reflectorImplClass, getClass().getClassLoader(), getClass().getProtectionDomain());
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) CtField(javassist.CtField) SParameter(org.bimserver.shared.meta.SParameter) SMethod(org.bimserver.shared.meta.SMethod) SParameter(org.bimserver.shared.meta.SParameter) CtMethod(javassist.CtMethod)

Example 5 with CtConstructor

use of javassist.CtConstructor in project BIMserver by opensourceBIM.

the class RealtimeReflectorFactoryBuilder method build2.

private void build2(Class<? extends PublicInterface> interfaceClass, org.bimserver.shared.meta.SService sService) {
    try {
        CtClass reflectorImplClass = pool.makeClass(GENERATED_CLASSES_PACKAGE + "." + interfaceClass.getSimpleName() + "Reflector" + implementationCounter);
        CtClass reflectorClass = pool.get(Reflector.class.getName());
        CtClass interfaceCtClass = pool.get(interfaceClass.getName());
        reflectorImplClass.addInterface(reflectorClass);
        CtField reflectorField = new CtField(interfaceCtClass, "publicInterface", reflectorImplClass);
        reflectorImplClass.addField(reflectorField);
        CtConstructor constructor = new CtConstructor(new CtClass[] { interfaceCtClass }, reflectorImplClass);
        StringBuilder sb = new StringBuilder();
        reflectorImplClass.addConstructor(constructor);
        sb.append("{");
        sb.append("this.publicInterface = $1;");
        sb.append("}");
        constructor.setBody(sb.toString());
        CtClass[] parameters = new CtClass[4];
        parameters[0] = pool.get(String.class.getName());
        parameters[1] = pool.get(String.class.getName());
        parameters[2] = pool.get(Class.class.getName());
        parameters[3] = pool.get(KeyValuePair.class.getName() + "[]");
        CtMethod method = new CtMethod(pool.get(Object.class.getName()), "callMethod", parameters, reflectorImplClass);
        StringBuilder methodBuilder = new StringBuilder();
        methodBuilder.append("{");
        methodBuilder.append("if  (1==0) {} ");
        for (SMethod sMethod : sService.getMethods()) {
            methodBuilder.append(" else if ($2.equals(\"" + sMethod.getName() + "\")) {");
            if (!sMethod.getReturnType().isVoid()) {
                methodBuilder.append("return ");
            }
            methodBuilder.append("publicInterface." + sMethod.getName() + "(");
            int i = 0;
            for (SParameter sParameter : sMethod.getParameters()) {
                methodBuilder.append("(" + sParameter.getType().toJavaCode() + ")$4[" + i + "].getValue()");
                if (i < sMethod.getParameters().size() - 1) {
                    methodBuilder.append(", ");
                }
                i++;
            }
            methodBuilder.append(");");
            methodBuilder.append("}");
        }
        methodBuilder.append("return null;");
        methodBuilder.append("}");
        method.setBody(methodBuilder.toString());
        reflectorImplClass.addMethod(method);
        pool.toClass(reflectorImplClass, getClass().getClassLoader(), getClass().getProtectionDomain());
    } catch (Exception e) {
        LOGGER.error("", e);
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) SParameter(org.bimserver.shared.meta.SParameter) SMethod(org.bimserver.shared.meta.SMethod) CtMethod(javassist.CtMethod) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) CtConstructor(javassist.CtConstructor)

Aggregations

CtConstructor (javassist.CtConstructor)37 CtClass (javassist.CtClass)23 ClassPool (javassist.ClassPool)16 CtMethod (javassist.CtMethod)16 CannotCompileException (javassist.CannotCompileException)14 NotFoundException (javassist.NotFoundException)13 CtField (javassist.CtField)12 IOException (java.io.IOException)8 Method (java.lang.reflect.Method)6 CtNewMethod (javassist.CtNewMethod)5 UUID (java.util.UUID)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)2 FileNotFoundException (java.io.FileNotFoundException)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2 Modifier (java.lang.reflect.Modifier)2 Parameter (java.lang.reflect.Parameter)2 ArrayList (java.util.ArrayList)2