use of javassist.ClassPool in project systemml by apache.
the class GenerateClassesForMLContext method createFunctionOutputClass.
/**
* Create a class that encapsulates the outputs of a function.
*
* @param scriptFilePath
* the path to a script file
* @param fs
* a SystemML function statement
*/
public static void createFunctionOutputClass(String scriptFilePath, FunctionStatement fs) {
try {
ArrayList<DataIdentifier> oparams = fs.getOutputParams();
// than encapsulating it in a function output class
if ((oparams.size() == 0) || (oparams.size() == 1)) {
return;
}
String fullFunctionOutputClassName = getFullFunctionOutputClassName(scriptFilePath, fs);
System.out.println("Generating Class: " + fullFunctionOutputClassName);
ClassPool pool = ClassPool.getDefault();
CtClass ctFuncOut = pool.makeClass(fullFunctionOutputClassName);
// add fields
for (int i = 0; i < oparams.size(); i++) {
DataIdentifier oparam = oparams.get(i);
String type = getParamTypeAsString(oparam);
String name = oparam.getName();
String fstring = "public " + type + " " + name + ";";
CtField field = CtField.make(fstring, ctFuncOut);
ctFuncOut.addField(field);
}
// add constructor
String simpleFuncOutClassName = fullFunctionOutputClassName.substring(fullFunctionOutputClassName.lastIndexOf(".") + 1);
StringBuilder con = new StringBuilder();
con.append("public " + simpleFuncOutClassName + "(");
for (int i = 0; i < oparams.size(); i++) {
if (i > 0) {
con.append(", ");
}
DataIdentifier oparam = oparams.get(i);
String type = getParamTypeAsString(oparam);
String name = oparam.getName();
con.append(type + " " + name);
}
con.append(") {\n");
for (int i = 0; i < oparams.size(); i++) {
DataIdentifier oparam = oparams.get(i);
String name = oparam.getName();
con.append("this." + name + "=" + name + ";\n");
}
con.append("}\n");
String cstring = con.toString();
CtConstructor ctCon = CtNewConstructor.make(cstring, ctFuncOut);
ctFuncOut.addConstructor(ctCon);
// add toString
StringBuilder s = new StringBuilder();
s.append("public String toString(){\n");
s.append("StringBuilder sb = new StringBuilder();\n");
for (int i = 0; i < oparams.size(); i++) {
DataIdentifier oparam = oparams.get(i);
String name = oparam.getName();
s.append("sb.append(\"" + name + " (" + getSimpleParamTypeAsString(oparam) + "): \" + " + name + " + \"\\n\");\n");
}
s.append("String str = sb.toString();\nreturn str;\n");
s.append("}\n");
String toStr = s.toString();
CtMethod toStrMethod = CtNewMethod.make(toStr, ctFuncOut);
ctFuncOut.addMethod(toStrMethod);
ctFuncOut.writeFile(destination);
} catch (RuntimeException e) {
e.printStackTrace();
} catch (CannotCompileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
use of javassist.ClassPool in project turbo-rpc by hank-whu.
the class FailoverInvokerFactory method generateDefaultImplObject.
private Object generateDefaultImplObject(Class<?> clazz) throws Exception {
if (clazz == null) {
throw new RuntimeException("clazz must not be null");
}
Method[] allMethods = clazz.getMethods();
for (Method method : allMethods) {
if (!CompletableFuture.class.equals(method.getReturnType())) {
throw new RuntimeException("method return-type must be CompletableFuture, " + method);
}
}
if (!Stream.of(allMethods).anyMatch(m -> m.isDefault())) {
return null;
}
final String remoteClassName = //
clazz.getName() + "_DefaultImpl_" + UUID.randomUUID().toString().replace("-", "");
// 创建类
ClassPool pool = ClassPool.getDefault();
CtClass defaultImplCtClass = pool.makeClass(remoteClassName);
CtClass[] interfaces = { pool.getCtClass(clazz.getName()) };
defaultImplCtClass.setInterfaces(interfaces);
// 添加无参的构造函数
CtConstructor constructor = new CtConstructor(null, defaultImplCtClass);
constructor.setModifiers(Modifier.PUBLIC);
constructor.setBody("{}");
defaultImplCtClass.addConstructor(constructor);
for (Method method : allMethods) {
if (method.isDefault()) {
continue;
}
StringBuilder methodBuilder = new StringBuilder();
methodBuilder.append("public ");
methodBuilder.append(method.getReturnType().getName());
methodBuilder.append(" ");
methodBuilder.append(method.getName());
methodBuilder.append("(");
Class<?>[] parameterTypes = method.getParameterTypes();
for (int i = 0; i < parameterTypes.length; i++) {
Class<?> parameterType = parameterTypes[i];
methodBuilder.append(parameterType.getName());
methodBuilder.append(" param");
methodBuilder.append(i);
if (i != parameterTypes.length - 1) {
methodBuilder.append(", ");
}
}
methodBuilder.append("){\r\n throw new UnsupportedOperationException();\r\n}");
CtMethod m = CtNewMethod.make(methodBuilder.toString(), defaultImplCtClass);
defaultImplCtClass.addMethod(m);
}
Class<?> invokerClass = defaultImplCtClass.toClass();
return invokerClass.getConstructor().newInstance();
}
use of javassist.ClassPool in project turbo-rpc by hank-whu.
the class MethodParamClassFactory method doCreateClass.
@SuppressWarnings("unchecked")
private static Class<? extends MethodParam> doCreateClass(Method method) throws CannotCompileException, NotFoundException {
Class<?>[] parameterTypes = method.getParameterTypes();
Parameter[] parameters = method.getParameters();
if (!parameters[0].isNamePresent()) {
throw new RuntimeException(NOT_SUPPORT_PARAMETER_NAME_MSG);
}
String paramTypes = //
Stream.of(parameterTypes).map(//
clazz -> clazz.getName()).collect(Collectors.joining(",", "(", ")"));
String hash = Hashing.murmur3_128().hashString(paramTypes, StandardCharsets.UTF_8).toString();
final String methodParamClassName = //
method.getDeclaringClass().getName() + //
"$MethodParam" + "$" + //
method.getName() + "$" + //
parameterTypes.length + "$" + // 防止同名方法冲突
hash;
try {
Class<?> clazz = MethodParamClassFactory.class.getClassLoader().loadClass(methodParamClassName);
if (clazz != null) {
return (Class<? extends MethodParam>) clazz;
}
} catch (ClassNotFoundException e) {
}
// 创建类
ClassPool pool = ClassPool.getDefault();
CtClass methodParamCtClass = pool.makeClass(methodParamClassName);
CtClass[] interfaces = { pool.getCtClass(MethodParam.class.getName()) };
methodParamCtClass.setInterfaces(interfaces);
for (int i = 0; i < parameterTypes.length; i++) {
Parameter parameter = parameters[i];
String paramName = parameter.getName();
Class<?> paramType = parameterTypes[i];
String capitalize = Character.toUpperCase(paramName.charAt(0)) + paramName.substring(1);
String getter = "get" + capitalize;
String setter = "set" + capitalize;
CtField ctField = new CtField(pool.get(paramType.getName()), paramName, methodParamCtClass);
ctField.setModifiers(Modifier.PRIVATE);
methodParamCtClass.addField(ctField);
methodParamCtClass.addMethod(CtNewMethod.getter("$param" + i, ctField));
methodParamCtClass.addMethod(CtNewMethod.getter(getter, ctField));
methodParamCtClass.addMethod(CtNewMethod.setter(setter, ctField));
}
// 添加无参的构造函数
CtConstructor constructor0 = new CtConstructor(null, methodParamCtClass);
constructor0.setModifiers(Modifier.PUBLIC);
constructor0.setBody("{}");
methodParamCtClass.addConstructor(constructor0);
// 添加有参的构造函数
CtClass[] paramCtClassArray = new CtClass[method.getParameterCount()];
for (int i = 0; i < method.getParameterCount(); i++) {
Class<?> paramType = parameterTypes[i];
CtClass paramCtClass = pool.get(paramType.getName());
paramCtClassArray[i] = paramCtClass;
}
StringBuilder bodyBuilder = ThreadLocalStringBuilder.current();
bodyBuilder.append("{\r\n");
for (int i = 0; i < method.getParameterCount(); i++) {
String paramName = parameters[i].getName();
bodyBuilder.append("$0.");
bodyBuilder.append(paramName);
bodyBuilder.append(" = $");
bodyBuilder.append(i + 1);
bodyBuilder.append(";\r\n");
}
bodyBuilder.append("}");
CtConstructor constructor1 = new CtConstructor(paramCtClassArray, methodParamCtClass);
constructor1.setBody(bodyBuilder.toString());
methodParamCtClass.addConstructor(constructor1);
return (Class<? extends MethodParam>) methodParamCtClass.toClass();
}
use of javassist.ClassPool in project incubator-servicecomb-java-chassis by apache.
the class JavassistUtils method detach.
// for test
public static void detach(String clsName) {
try {
ClassPool classPool = getOrCreateClassPool(Thread.currentThread().getContextClassLoader());
classPool.getCtClass(clsName).detach();
} catch (NotFoundException e) {
// do nothing.
}
}
use of javassist.ClassPool in project incubator-servicecomb-java-chassis by apache.
the class JavassistUtils method createClass.
public static Class<?> createClass(ClassLoader classLoader, ClassConfig config) {
if (classLoader == null) {
classLoader = Thread.currentThread().getContextClassLoader();
}
ClassPool classPool = getOrCreateClassPool(classLoader);
CtClass ctClass = classPool.getOrNull(config.getClassName());
if (ctClass == null) {
if (config.isIntf()) {
ctClass = classPool.makeInterface(config.getClassName());
} else {
ctClass = classPool.makeClass(config.getClassName());
}
}
try {
for (String intfName : config.getIntfList()) {
ctClass.addInterface(classPool.get(intfName));
}
for (FieldConfig fieldConfig : config.getFieldList()) {
CtField field = createCtField(classPool, ctClass, fieldConfig);
ctClass.addField(field);
if (fieldConfig.isGenGetter()) {
addFieldGetter(config, fieldConfig);
}
if (fieldConfig.isGenSetter()) {
addFieldSetter(config, fieldConfig);
}
}
for (MethodConfig methodConfig : config.getMethodList()) {
try {
CtMethod ctMethod = CtMethod.make(methodConfig.getSource(), ctClass);
if (methodConfig.getGenericSignature() != null) {
ctMethod.setGenericSignature(methodConfig.getGenericSignature());
}
ctClass.addMethod(ctMethod);
} catch (CannotCompileException e) {
LOGGER.error("Failed to create method, source:\n{}.", methodConfig.getSource());
throw e;
}
}
LOGGER.info("generate {} in classLoader {}.", config.getClassName(), classLoader);
return ctClass.toClass(classLoader, null);
} catch (Throwable e) {
throw new Error(String.format("Failed to create %s in classLoader %s.", config.getClassName(), classLoader), e);
}
}
Aggregations