Search in sources :

Example 26 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)

Example 27 with CtConstructor

use of javassist.CtConstructor in project incubator-servicecomb-java-chassis by apache.

the class JavassistUtils method addEnumConstructor.

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

Example 28 with CtConstructor

use of javassist.CtConstructor in project systemml by apache.

the class GenerateClassesForMLContext method createScriptClass.

/**
 * Convert a script file to a Java class that extends the MLContext API's
 * Script class.
 *
 * @param scriptFilePath
 *            the path to a script file
 */
public static void createScriptClass(String scriptFilePath) {
    try {
        String fullScriptClassName = BASE_DEST_PACKAGE + "." + scriptFilePathToFullClassNameNoBase(scriptFilePath);
        System.out.println("Generating Class: " + fullScriptClassName);
        ClassPool pool = ClassPool.getDefault();
        CtClass ctNewScript = pool.makeClass(fullScriptClassName);
        CtClass ctScript = pool.get(Script.class.getName());
        ctNewScript.setSuperclass(ctScript);
        CtConstructor ctCon = new CtConstructor(null, ctNewScript);
        ctCon.setBody(scriptConstructorBody(scriptFilePath));
        ctNewScript.addConstructor(ctCon);
        addFunctionMethods(scriptFilePath, ctNewScript);
        ctNewScript.writeFile(destination);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (CannotCompileException e) {
        e.printStackTrace();
    } catch (NotFoundException e) {
        e.printStackTrace();
    }
}
Also used : CtClass(javassist.CtClass) Script(org.apache.sysml.api.mlcontext.Script) DMLScript(org.apache.sysml.api.DMLScript) ClassPool(javassist.ClassPool) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) CtConstructor(javassist.CtConstructor)

Example 29 with CtConstructor

use of javassist.CtConstructor in project powermock by powermock.

the class JavaAssistTestClassTransformer 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 30 with CtConstructor

use of javassist.CtConstructor in project powermock by powermock.

the class ClassReplicaCreator method createInstanceReplica.

/**
 * Create a class that is a replica of type {@code T}. To allow for
 * partial mocking all calls to non-mocked methods will be delegated to the
 * {@code delegator}.
 *
 * @param <T>       The type of the replica class to be created.
 * @param delegator The delegator object that will be invoked to allow for partial
 *                  mocking.
 * @return A replica class that can be used to duck-type an instance.
 */
@SuppressWarnings("unchecked")
public <T> Class<T> createInstanceReplica(T delegator) {
    if (delegator == null) {
        throw new IllegalArgumentException("delegator cannot be null");
    }
    final Class<T> clazz = (Class<T>) delegator.getClass();
    ClassPool classpool = ClassPool.getDefault();
    final String originalClassName = clazz.getName();
    CtClass originalClassAsCtClass;
    final CtClass newClass = classpool.makeClass(generateReplicaClassName(clazz));
    try {
        originalClassAsCtClass = classpool.get(originalClassName);
        copyFields(originalClassAsCtClass, newClass);
        addDelegatorField(delegator, newClass);
        CtMethod[] declaredMethods = originalClassAsCtClass.getDeclaredMethods();
        for (CtMethod ctMethod : declaredMethods) {
            @SuppressWarnings("unused") final String code = getReplicaMethodDelegationCode(delegator.getClass(), ctMethod, POWERMOCK_INSTANCE_DELEGATOR_FIELD_NAME);
            CtMethod make2 = CtNewMethod.copy(ctMethod, newClass, null);
            newClass.addMethod(make2);
        }
        CtConstructor[] declaredConstructors = originalClassAsCtClass.getDeclaredConstructors();
        for (CtConstructor ctConstructor : declaredConstructors) {
            CtConstructor copy = CtNewConstructor.copy(ctConstructor, newClass, null);
            newClass.addConstructor(copy);
        }
        return (Class<T>) newClass.toClass(this.getClass().getClassLoader(), this.getClass().getProtectionDomain());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : ClassPool(javassist.ClassPool) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Aggregations

CtConstructor (javassist.CtConstructor)91 CtClass (javassist.CtClass)66 CtMethod (javassist.CtMethod)38 NotFoundException (javassist.NotFoundException)33 ClassPool (javassist.ClassPool)30 CtField (javassist.CtField)30 CannotCompileException (javassist.CannotCompileException)29 IOException (java.io.IOException)13 Method (java.lang.reflect.Method)9 ArrayList (java.util.ArrayList)7 FileNotFoundException (java.io.FileNotFoundException)6 CtNewMethod (javassist.CtNewMethod)6 MethodInfo (javassist.bytecode.MethodInfo)6 StorageException (org.apache.skywalking.oap.server.core.storage.StorageException)6 StringWriter (java.io.StringWriter)5 ConstPool (javassist.bytecode.ConstPool)5 HashMap (java.util.HashMap)4 Annotation (javassist.bytecode.annotation.Annotation)4 OALCompileException (org.apache.skywalking.oap.server.core.oal.rt.OALCompileException)4 ModuleStartException (org.apache.skywalking.oap.server.library.module.ModuleStartException)4