Search in sources :

Example 61 with CtMethod

use of javassist.CtMethod in project play-cookbook by spinscale.

the class SolrEnhancer method enhanceThisClass.

@Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
    CtClass ctClass = makeClass(applicationClass);
    if (!ctClass.subtypeOf(classPool.get("play.modules.solr.SearchModel"))) {
        return;
    }
    String method = "public static play.modules.solr.Query search(String query, String[] values) { return search(" + applicationClass.name + ".class, query, values); }";
    CtMethod count = CtMethod.make(method, ctClass);
    ctClass.addMethod(count);
    // Done.
    applicationClass.enhancedByteCode = ctClass.toBytecode();
    ctClass.defrost();
    Logger.info("Enhanced search of %s", applicationClass.name);
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Example 62 with CtMethod

use of javassist.CtMethod in project play-cookbook by spinscale.

the class CsvEnhancer method enhanceThisClass.

@Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
    CtClass ctClass = makeClass(applicationClass);
    if (!ctClass.subtypeOf(classPool.get("play.modules.csv.CsvModel"))) {
        return;
    }
    CtMethod findById = CtMethod.make("public static play.modules.csv.CsvModel findById(Long id) { return findById(" + applicationClass.name + ".class, id); }", ctClass);
    ctClass.addMethod(findById);
    CtMethod find = CtMethod.make("public static play.modules.csv.CsvQuery find(String query, Object[] fields) { return find(" + applicationClass.name + ".class, query, fields); }", ctClass);
    ctClass.addMethod(find);
    applicationClass.enhancedByteCode = ctClass.toBytecode();
    ctClass.defrost();
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod)

Example 63 with CtMethod

use of javassist.CtMethod in project play-cookbook by spinscale.

the class SearchHelperEnhancer method enhanceThisClass.

@Override
public void enhanceThisClass(ApplicationClass applicationClass) throws Exception {
    CtClass ctClass = makeClass(applicationClass);
    if (!ctClass.subtypeOf(classPool.get("play.modules.searchhelp.IndexedModel")) || !hasAnnotation(ctClass, "play.modules.search.Indexed")) {
        return;
    }
    CtMethod isIndexed = CtMethod.make("public static Boolean isIndexed() { return Boolean.TRUE; }", ctClass);
    ctClass.addMethod(isIndexed);
    List<String> fields = new ArrayList();
    for (CtField ctField : ctClass.getFields()) {
        if (hasAnnotation(ctField, "play.modules.search.Field")) {
            fields.add("\"" + ctField.getName() + "\"");
        }
    }
    String method;
    if (fields.size() > 0) {
        String fieldStr = fields.toString().replace("[", "").replace("]", "");
        method = "public static java.util.List getIndexedFields() { return java.util.Arrays.asList(new String[]{" + fieldStr + "}); }";
        CtMethod count = CtMethod.make(method, ctClass);
        ctClass.addMethod(count);
    }
    applicationClass.enhancedByteCode = ctClass.toBytecode();
    ctClass.defrost();
}
Also used : CtClass(javassist.CtClass) CtField(javassist.CtField) ArrayList(java.util.ArrayList) CtMethod(javassist.CtMethod)

Example 64 with CtMethod

use of javassist.CtMethod in project pinpoint by naver.

the class JavaAssistTest method testAssist.

@Test
public void testAssist() throws NotFoundException, NoSuchMethodException {
    CtClass ctClass = pool.get(String.class.getName());
    logger.debug(ctClass.toString());
    String s = "";
    //        ctClass.getMethod("valueOf", "(D)");
    CtMethod[] methods = ctClass.getMethods();
    //        for (CtMethod method :  methods) {
    //            logger.debug("{} {}", method.getMethodInfo(), method.getSignature());
    //        }
    CtMethod endsWith = ctClass.getMethod("endsWith", "(Ljava/lang/String;)Z");
    logger.debug(endsWith.getMethodInfo().toString());
    logger.debug(endsWith.getSignature());
    logger.debug(endsWith.getLongName());
    logger.debug(endsWith.toString());
    logger.debug(endsWith.getName());
    logger.debug(endsWith.getMethodInfo().getName());
    logger.debug(endsWith.getMethodInfo().getDescriptor());
    Method endsWith1 = String.class.getMethod("endsWith", String.class);
    logger.debug(endsWith1.toString());
}
Also used : CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) Method(java.lang.reflect.Method) CtMethod(javassist.CtMethod) Test(org.junit.Test)

Example 65 with CtMethod

use of javassist.CtMethod in project pinpoint by naver.

the class JavassistClass method addField0.

private void addField0(String accessorTypeName, String initValExp) throws InstrumentException {
    try {
        Class<?> accessorType = pluginContext.injectClass(classLoader, accessorTypeName);
        final AccessorAnalyzer accessorAnalyzer = new AccessorAnalyzer();
        final AccessorDetails accessorDetails = accessorAnalyzer.analyze(accessorType);
        Class<?> fieldType = accessorDetails.getFieldType();
        String fieldTypeName = JavaAssistUtils.javaClassNameToObjectName(fieldType.getName());
        final CtField newField = CtField.make("private " + fieldTypeName + " " + FIELD_PREFIX + JavaAssistUtils.javaClassNameToVariableName(accessorTypeName) + ";", ctClass);
        if (initValExp == null) {
            ctClass.addField(newField);
        } else {
            ctClass.addField(newField, initValExp);
        }
        final CtClass accessorInterface = getCtClass(accessorTypeName);
        ctClass.addInterface(accessorInterface);
        CtMethod getterMethod = CtNewMethod.getter(accessorDetails.getGetter().getName(), newField);
        ctClass.addMethod(getterMethod);
        CtMethod setterMethod = CtNewMethod.setter(accessorDetails.getSetter().getName(), newField);
        ctClass.addMethod(setterMethod);
    } catch (Exception e) {
        throw new InstrumentException("Failed to add field with accessor [" + accessorTypeName + "]. Cause:" + e.getMessage(), e);
    }
}
Also used : AccessorDetails(com.navercorp.pinpoint.profiler.instrument.AccessorAnalyzer.AccessorDetails) CtClass(javassist.CtClass) CtField(javassist.CtField) CtMethod(javassist.CtMethod) PinpointException(com.navercorp.pinpoint.exception.PinpointException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException)

Aggregations

CtMethod (javassist.CtMethod)76 CtClass (javassist.CtClass)46 NotFoundException (javassist.NotFoundException)23 CannotCompileException (javassist.CannotCompileException)20 ClassPool (javassist.ClassPool)18 CtField (javassist.CtField)14 Test (org.junit.Test)12 IOException (java.io.IOException)10 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)6 Method (java.lang.reflect.Method)5 CtConstructor (javassist.CtConstructor)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)4 GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)3 PinpointException (com.navercorp.pinpoint.exception.PinpointException)3 File (java.io.File)3 FileNotFoundException (java.io.FileNotFoundException)3 ArrayList (java.util.ArrayList)3 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)2 FileFilter (java.io.FileFilter)2 IllegalClassFormatException (java.lang.instrument.IllegalClassFormatException)2