use of javassist.CtField in project motech by motech.
the class EntityBuilderImpl method makeClass.
private CtClass makeClass(EntityDto entity, List<FieldDto> fields, EntityType type, Bundle bundle) throws NotFoundException, CannotCompileException, ReflectiveOperationException {
// try to get declaring class
CtClass declaring = getDeclaringClass(entity, type, bundle);
// check and add default constructor if necessary
injectDefaultConstructor(declaring);
// create properties (add fields, getters and setters)
for (FieldDto field : fields) {
String fieldName = field.getBasic().getName();
try {
// We skip version fields for trash and history
if (field.isVersionField() && type != EntityType.STANDARD) {
continue;
}
CtField ctField;
if (!shouldLeaveExistingField(field, declaring)) {
JavassistUtil.removeFieldIfExists(declaring, fieldName);
ctField = createField(declaring, entity, field, type);
if (isObjectNullOrBlankString(field.getBasic().getDefaultValue())) {
declaring.addField(ctField);
} else {
declaring.addField(ctField, createInitializer(entity, field));
}
} else {
ctField = JavassistUtil.findField(declaring, fieldName);
}
String getter = MemberUtil.getGetterName(fieldName, declaring);
String setter = MemberUtil.getSetterName(fieldName);
if (!shouldLeaveExistingMethod(field, getter, declaring)) {
createGetter(declaring, fieldName, ctField);
}
if (!shouldLeaveExistingMethod(field, setter, declaring)) {
createSetter(declaring, fieldName, ctField);
}
} catch (RuntimeException e) {
throw new EntityCreationException("Error while processing field " + fieldName, e);
}
}
return declaring;
}
use of javassist.CtField in project dubbo by alibaba.
the class JValidator method generateMethodParameterClass.
/**
* try to generate methodParameterClass.
*
* @param clazz interface class
* @param method invoke method
* @param parameterClassName generated parameterClassName
* @return Class<?> generated methodParameterClass
* @throws Exception
*/
private static Class<?> generateMethodParameterClass(Class<?> clazz, Method method, String parameterClassName) throws Exception {
ClassPool pool = ClassGenerator.getClassPool(clazz.getClassLoader());
synchronized (parameterClassName.intern()) {
CtClass ctClass = null;
try {
ctClass = pool.getCtClass(parameterClassName);
} catch (NotFoundException ignore) {
}
if (null == 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);
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);
}
return ctClass.toClass(clazz.getClassLoader(), null);
} else {
return Class.forName(parameterClassName, true, clazz.getClassLoader());
}
}
}
use of javassist.CtField in project restfulie-java by caelum.
the class DefaultEnhancer method enhanceLinks.
private void enhanceLinks(CtClass newType) throws CannotCompileException {
CtField field = CtField.make("public java.util.List link = new java.util.ArrayList();", newType);
newType.addField(field);
newType.addMethod(CtNewMethod.make("public java.util.List getLinks() { return link; }", newType));
newType.addMethod(CtNewMethod.make("public java.util.List getLinks(String rel) { java.util.List links = new java.util.ArrayList(); for(int i=0;i<link.size();i++) {br.com.caelum.restfulie.Link t = link.get(i); if(t.getRel().equals(rel)) links.add(t); } return links; }", newType));
newType.addMethod(CtNewMethod.make("public br.com.caelum.restfulie.Link getLink(String rel) { for(int i=0;i<link.size();i++) {br.com.caelum.restfulie.Link t = link.get(i); if(t.getRel().equals(rel)) return t; } return null; }", newType));
newType.addMethod(CtNewMethod.make("public boolean hasLink(String link) { return getLink(link)!=null; }", newType));
}
use of javassist.CtField 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.CtField in project powermock by powermock.
the class PowerMockExpressionEditor method edit.
@Override
public void edit(FieldAccess f) throws CannotCompileException {
if (f.isReader()) {
CtClass returnTypeAsCtClass;
FieldInfo fieldInfo;
try {
CtField field = f.getField();
returnTypeAsCtClass = field.getType();
fieldInfo = field.getFieldInfo2();
} catch (NotFoundException e) {
/*
* If multiple java agents are active (in INST_REDEFINE mode), the types implicitly loaded by javassist from disk
* might differ from the types available in memory. Thus, this error might occur.
*
* It may also happen if PowerMock is modifying an SPI where the SPI require some classes to be available in the classpath
* at runtime but they are not! This is valid in some cases such as slf4j.
*/
return;
}
if (isNotSyntheticField(fieldInfo)) {
String code = "{Object value = " + MockGateway.class.getName() + ".fieldCall(" + "$0,$class,\"" + f.getFieldName() + "\",$type);" + "if(value == " + MockGateway.class.getName() + ".PROCEED) {" + " $_ = $proceed($$);" + "} else {" + " $_ = " + getCorrectReturnValueType(returnTypeAsCtClass) + ";" + "}}";
f.replace(code);
}
}
}
Aggregations