Search in sources :

Example 46 with CtField

use of javassist.CtField in project dubbo by alibaba.

the class JValidator method getMethodParameterBean.

private static Object getMethodParameterBean(Class<?> clazz, Method method, Object[] args) {
    if (!hasConstraintParameter(method)) {
        return null;
    }
    try {
        String parameterClassName = generateMethodParameterClassName(clazz, method);
        Class<?> parameterClass;
        try {
            parameterClass = (Class<?>) Class.forName(parameterClassName, true, clazz.getClassLoader());
        } catch (ClassNotFoundException e) {
            ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader());
            CtClass ctClass = pool.makeClass(parameterClassName);
            ClassFile classFile = ctClass.getClassFile();
            classFile.setVersionToJava5();
            ctClass.addConstructor(CtNewConstructor.defaultConstructor(pool.getCtClass(parameterClassName)));
            // parameter fields
            Class<?>[] parameterTypes = method.getParameterTypes();
            Annotation[][] parameterAnnotations = method.getParameterAnnotations();
            for (int i = 0; i < parameterTypes.length; i++) {
                Class<?> type = parameterTypes[i];
                Annotation[] annotations = parameterAnnotations[i];
                AnnotationsAttribute attribute = new AnnotationsAttribute(classFile.getConstPool(), AnnotationsAttribute.visibleTag);
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType().isAnnotationPresent(Constraint.class)) {
                        javassist.bytecode.annotation.Annotation ja = new javassist.bytecode.annotation.Annotation(classFile.getConstPool(), pool.getCtClass(annotation.annotationType().getName()));
                        Method[] members = annotation.annotationType().getMethods();
                        for (Method member : members) {
                            if (Modifier.isPublic(member.getModifiers()) && member.getParameterTypes().length == 0 && member.getDeclaringClass() == annotation.annotationType()) {
                                Object value = member.invoke(annotation, new Object[0]);
                                if (null != value) {
                                    MemberValue memberValue = createMemberValue(classFile.getConstPool(), pool.get(member.getReturnType().getName()), value);
                                    ja.addMemberValue(member.getName(), memberValue);
                                }
                            }
                        }
                        attribute.addAnnotation(ja);
                    }
                }
                String fieldName = method.getName() + "Argument" + i;
                CtField ctField = CtField.make("public " + type.getCanonicalName() + " " + fieldName + ";", pool.getCtClass(parameterClassName));
                ctField.getFieldInfo().addAttribute(attribute);
                ctClass.addField(ctField);
            }
            parameterClass = ctClass.toClass(clazz.getClassLoader(), null);
        }
        Object parameterBean = parameterClass.newInstance();
        for (int i = 0; i < args.length; i++) {
            Field field = parameterClass.getField(method.getName() + "Argument" + i);
            field.set(parameterBean, args[i]);
        }
        return parameterBean;
    } catch (Throwable e) {
        logger.warn(e.getMessage(), e);
        return null;
    }
}
Also used : ClassFile(javassist.bytecode.ClassFile) Constraint(javax.validation.Constraint) AnnotationsAttribute(javassist.bytecode.AnnotationsAttribute) ClassPool(javassist.ClassPool) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Constraint(javax.validation.Constraint) CtClass(javassist.CtClass) Field(java.lang.reflect.Field) CtField(javassist.CtField) MemberValue(javassist.bytecode.annotation.MemberValue) ArrayMemberValue(javassist.bytecode.annotation.ArrayMemberValue) CharMemberValue(javassist.bytecode.annotation.CharMemberValue) BooleanMemberValue(javassist.bytecode.annotation.BooleanMemberValue) FloatMemberValue(javassist.bytecode.annotation.FloatMemberValue) DoubleMemberValue(javassist.bytecode.annotation.DoubleMemberValue) ShortMemberValue(javassist.bytecode.annotation.ShortMemberValue) StringMemberValue(javassist.bytecode.annotation.StringMemberValue) IntegerMemberValue(javassist.bytecode.annotation.IntegerMemberValue) LongMemberValue(javassist.bytecode.annotation.LongMemberValue) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) EnumMemberValue(javassist.bytecode.annotation.EnumMemberValue) ByteMemberValue(javassist.bytecode.annotation.ByteMemberValue) CtField(javassist.CtField) CtClass(javassist.CtClass)

Example 47 with CtField

use of javassist.CtField in project incubator-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();
    }
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) ClassPool(javassist.ClassPool) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) CtField(javassist.CtField) CtMethod(javassist.CtMethod)

Example 48 with CtField

use of javassist.CtField 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();
    }
}
Also used : DataIdentifier(org.apache.sysml.parser.DataIdentifier) ClassPool(javassist.ClassPool) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) CtField(javassist.CtField) CtMethod(javassist.CtMethod)

Example 49 with CtField

use of javassist.CtField 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();
}
Also used : CannotCompileException(javassist.CannotCompileException) Modifier(javassist.Modifier) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Hashing(com.google.common.hash.Hashing) CtClass(javassist.CtClass) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) ConcurrentMap(java.util.concurrent.ConcurrentMap) Objects(java.util.Objects) CtField(javassist.CtField) CtNewMethod(javassist.CtNewMethod) Stream(java.util.stream.Stream) Parameter(java.lang.reflect.Parameter) CtConstructor(javassist.CtConstructor) NotFoundException(javassist.NotFoundException) ThreadLocalStringBuilder(rpc.turbo.util.concurrent.ThreadLocalStringBuilder) Method(java.lang.reflect.Method) ClassPool(javassist.ClassPool) ThreadLocalStringBuilder(rpc.turbo.util.concurrent.ThreadLocalStringBuilder) ClassPool(javassist.ClassPool) CtConstructor(javassist.CtConstructor) CtClass(javassist.CtClass) CtField(javassist.CtField) Parameter(java.lang.reflect.Parameter) CtClass(javassist.CtClass)

Example 50 with CtField

use of javassist.CtField 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);
    }
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) ClassPool(javassist.ClassPool) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Aggregations

CtField (javassist.CtField)76 CtClass (javassist.CtClass)47 CtMethod (javassist.CtMethod)27 CannotCompileException (javassist.CannotCompileException)24 NotFoundException (javassist.NotFoundException)22 ClassPool (javassist.ClassPool)20 CtConstructor (javassist.CtConstructor)15 Test (org.junit.Test)12 ClassFile (javassist.bytecode.ClassFile)9 IOException (java.io.IOException)7 Method (java.lang.reflect.Method)7 ArrayList (java.util.ArrayList)6 AnnotationsAttribute (javassist.bytecode.AnnotationsAttribute)5 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)4 ConstPool (javassist.bytecode.ConstPool)4 SMethod (org.bimserver.shared.meta.SMethod)4 SParameter (org.bimserver.shared.meta.SParameter)4 InsertableMethod (com.github.stephanenicolas.afterburner.inserts.InsertableMethod)3 SimpleInsertableMethod (com.github.stephanenicolas.afterburner.inserts.SimpleInsertableMethod)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3