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");
}
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);
}
}
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");
}
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");
}
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();
}
Aggregations