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