Search in sources :

Example 41 with CtConstructor

use of javassist.CtConstructor in project incubator-skywalking by apache.

the class OALRuntime method generateMetricsBuilderClass.

/**
 * Generate metrics class builder and inject it to classloader
 */
private void generateMetricsBuilderClass(AnalysisResult metricsStmt) throws OALCompileException {
    String className = metricsBuilderClassName(metricsStmt, false);
    CtClass metricsBuilderClass = classPool.makeClass(metricsBuilderClassName(metricsStmt, true));
    try {
        metricsBuilderClass.addInterface(classPool.get(storageBuilderFactory.builderTemplate().getSuperClass()));
    } catch (NotFoundException e) {
        log.error("Can't find StorageBuilder interface for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Create empty construct
     */
    try {
        CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsBuilderClass);
        metricsBuilderClass.addConstructor(defaultConstructor);
    } catch (CannotCompileException e) {
        log.error("Can't add empty constructor in " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Generate methods
     */
    for (String method : METRICS_BUILDER_CLASS_METHODS) {
        StringWriter methodEntity = new StringWriter();
        try {
            configuration.getTemplate(storageBuilderFactory.builderTemplate().getTemplatePath() + "/" + method + ".ftl").process(metricsStmt, methodEntity);
            metricsBuilderClass.addMethod(CtNewMethod.make(methodEntity.toString(), metricsBuilderClass));
        } catch (Exception e) {
            log.error("Can't generate method " + method + " for " + className + ".", e);
            throw new OALCompileException(e.getMessage(), e);
        }
    }
    try {
        if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
            metricsBuilderClass.toClass(currentClassLoader, null);
        } else {
            metricsBuilderClass.toClass(MetricBuilderClassPackageHolder.class);
        }
    } catch (CannotCompileException e) {
        log.error("Can't compile/load " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    writeGeneratedFile(metricsBuilderClass, className, "metrics/builder");
}
Also used : CtClass(javassist.CtClass) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) StringWriter(java.io.StringWriter) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) ModuleStartException(org.apache.skywalking.oap.server.library.module.ModuleStartException) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor)

Example 42 with CtConstructor

use of javassist.CtConstructor in project javaparser by javaparser.

the class JavassistConstructorDeclarationTest method createValue.

@Override
public ResolvedConstructorDeclaration createValue() {
    try {
        TypeSolver typeSolver = new ReflectionTypeSolver();
        CtClass clazz = ClassPool.getDefault().getCtClass("java.lang.StringBuilder");
        CtConstructor constructor = clazz.getConstructors()[0];
        return new JavassistConstructorDeclaration(constructor, typeSolver);
    } catch (NotFoundException e) {
        throw new RuntimeException("Unexpected error.", e);
    }
}
Also used : CtClass(javassist.CtClass) TypeSolver(com.github.javaparser.symbolsolver.model.resolution.TypeSolver) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) NotFoundException(javassist.NotFoundException) ReflectionTypeSolver(com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeSolver) CtConstructor(javassist.CtConstructor)

Example 43 with CtConstructor

use of javassist.CtConstructor in project skywalking by apache.

the class OALRuntime method generateMetricsBuilderClass.

/**
 * Generate metrics class builder and inject it to classloader
 */
private void generateMetricsBuilderClass(AnalysisResult metricsStmt) throws OALCompileException {
    String className = metricsBuilderClassName(metricsStmt, false);
    CtClass metricsBuilderClass = classPool.makeClass(metricsBuilderClassName(metricsStmt, true));
    try {
        metricsBuilderClass.addInterface(classPool.get(storageBuilderFactory.builderTemplate().getSuperClass()));
    } catch (NotFoundException e) {
        log.error("Can't find StorageBuilder interface for " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Create empty construct
     */
    try {
        CtConstructor defaultConstructor = CtNewConstructor.make("public " + className + "() {}", metricsBuilderClass);
        metricsBuilderClass.addConstructor(defaultConstructor);
    } catch (CannotCompileException e) {
        log.error("Can't add empty constructor in " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    /**
     * Generate methods
     */
    for (String method : METRICS_BUILDER_CLASS_METHODS) {
        StringWriter methodEntity = new StringWriter();
        try {
            configuration.getTemplate(storageBuilderFactory.builderTemplate().getTemplatePath() + "/" + method + ".ftl").process(metricsStmt, methodEntity);
            metricsBuilderClass.addMethod(CtNewMethod.make(methodEntity.toString(), metricsBuilderClass));
        } catch (Exception e) {
            log.error("Can't generate method " + method + " for " + className + ".", e);
            throw new OALCompileException(e.getMessage(), e);
        }
    }
    try {
        if (SystemUtils.isJavaVersionAtMost(JavaVersion.JAVA_1_8)) {
            metricsBuilderClass.toClass(currentClassLoader, null);
        } else {
            metricsBuilderClass.toClass(MetricBuilderClassPackageHolder.class);
        }
    } catch (CannotCompileException e) {
        log.error("Can't compile/load " + className + ".", e);
        throw new OALCompileException(e.getMessage(), e);
    }
    writeGeneratedFile(metricsBuilderClass, className, "metrics/builder");
}
Also used : CtClass(javassist.CtClass) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) StringWriter(java.io.StringWriter) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) CannotCompileException(javassist.CannotCompileException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) OALCompileException(org.apache.skywalking.oap.server.core.oal.rt.OALCompileException) ModuleStartException(org.apache.skywalking.oap.server.library.module.ModuleStartException) StorageException(org.apache.skywalking.oap.server.core.storage.StorageException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor)

Example 44 with CtConstructor

use of javassist.CtConstructor in project demo-parent by yindanqing925.

the class CreatePerson method createPerson.

public static void createPerson() throws NotFoundException, CannotCompileException, IOException {
    ClassPool pool = ClassPool.getDefault();
    // 创建一个类
    CtClass personClazz = pool.makeClass("org.nh.gome.demo.javassist.Person");
    // 创建成员变量
    CtField ageField = new CtField(pool.get("java.lang.Integer"), "age", personClazz);
    ageField.setModifiers(Modifier.PRIVATE);
    personClazz.addField(ageField, CtField.Initializer.constant(0));
    // 创建get/set
    personClazz.addMethod(CtNewMethod.setter("setAge", ageField));
    personClazz.addMethod(CtNewMethod.getter("getAge", ageField));
    // 创建无参构造
    CtConstructor personConstructor = new CtConstructor(new CtClass[] {}, personClazz);
    personConstructor.setBody("{System.out.println(\"person no args constructor\");}");
    personClazz.addConstructor(personConstructor);
    // 创建带参构造
    personConstructor = new CtConstructor(new CtClass[] { pool.get("java.lang.Integer") }, personClazz);
    personConstructor.setBody("{$0.age = $1; System.out.println(\"person all args constructor\");}");
    personClazz.addConstructor(personConstructor);
    // 创建方法
    CtMethod logAgeMethod = new CtMethod(CtClass.voidType, "logAgeMethod", new CtClass[] {}, personClazz);
    logAgeMethod.setModifiers(Modifier.PUBLIC);
    logAgeMethod.setBody("{System.out.println(age);}");
    personClazz.addMethod(logAgeMethod);
    // 写入文件
    personClazz.writeFile("D:/tmp/0428");
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod) CtConstructor(javassist.CtConstructor)

Example 45 with CtConstructor

use of javassist.CtConstructor in project demo-parent by yindanqing925.

the class CreatePerson method createPerson2.

public static void createPerson2() throws NotFoundException, CannotCompileException, IOException {
    ClassPool pool = ClassPool.getDefault();
    // 创建一个类
    CtClass personClazz = pool.makeClass("org.nh.gome.demo.javassist.Person");
    // 创建成员变量
    CtField ageField = new CtField(pool.get("java.lang.Integer"), "age", personClazz);
    ageField.setModifiers(Modifier.PRIVATE);
    personClazz.addField(ageField);
    // 创建get/set
    personClazz.addMethod(CtNewMethod.setter("setAge", ageField));
    personClazz.addMethod(CtNewMethod.getter("getAge", ageField));
    // 创建无参构造
    CtConstructor personNoArgsConstructor = CtNewConstructor.make("public Person(){System.out.println(\"person no args constructor\");}", personClazz);
    personClazz.addConstructor(personNoArgsConstructor);
    // 创建带参构造
    CtConstructor personAllArgsConstructor = CtNewConstructor.make("public Person(int age){this.age = age; System.out.println(\"person no args constructor\");}", personClazz);
    personClazz.addConstructor(personAllArgsConstructor);
    // 创建方法
    CtMethod logAgeMethod = new CtMethod(CtClass.voidType, "logAgeMethod", new CtClass[] {}, personClazz);
    logAgeMethod.setModifiers(Modifier.PUBLIC);
    logAgeMethod.setBody("{System.out.println(age);}");
    personClazz.addMethod(logAgeMethod);
    // 写入文件
    personClazz.writeFile();
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) ClassPool(javassist.ClassPool) CtMethod(javassist.CtMethod) CtConstructor(javassist.CtConstructor)

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