use of javassist.CtField in project hibernate-orm by hibernate.
the class EntityEnhancer method createClearDirtyCollectionMethod.
private void createClearDirtyCollectionMethod(CtClass managedCtClass) throws CannotCompileException {
try {
final StringBuilder body = new StringBuilder();
body.append(String.format("private void %1$s() {%n" + " if (%2$s == null) { %2$s = new %3$s(); }%n" + " %4$s lazyInterceptor = null;%n", EnhancerConstants.TRACKER_COLLECTION_CLEAR_NAME, EnhancerConstants.TRACKER_COLLECTION_NAME, COLLECTION_TRACKER_IMPL, LazyAttributeLoadingInterceptor.class.getName()));
if (PersistentAttributesHelper.isAssignable(managedCtClass, PersistentAttributeInterceptable.class.getName())) {
body.append(String.format(" if(%1$s != null && %1$s instanceof %2$s) lazyInterceptor = (%2$s) %1$s;%n%n", EnhancerConstants.INTERCEPTOR_FIELD_NAME, LazyAttributeLoadingInterceptor.class.getName()));
}
for (CtField ctField : collectCollectionFields(managedCtClass)) {
body.append(String.format(" // collection field [%1$s]%n" + " if (lazyInterceptor == null || lazyInterceptor.isAttributeLoaded(\"%1$s\")) {%n" + " if (%1$s == null) { %2$s.add(\"%1$s\", -1); }%n" + " else { %2$s.add(\"%1$s\", %1$s.size()); }%n" + " }%n%n", ctField.getName(), EnhancerConstants.TRACKER_COLLECTION_NAME));
}
body.append("}");
MethodWriter.write(managedCtClass, body.toString());
} catch (CannotCompileException cce) {
cce.printStackTrace();
}
}
use of javassist.CtField in project libSBOLj by SynBioDex.
the class OBOTermCreator method createTerm.
// /**
// * Dynamically generates a Class file, for an interface that represents an OBO term with the given fields.
// *
// * The OBO term is uniquely identified by the id, and if a term with this id has been converted into a Class
// * file already, then that Class file (cached within the OBOTermCreator object) will be returned immediately.
// *
// * @param id
// * @param name
// * @param def
// * @param comments
// * @param is_a
// * @param relTypes
// * @param relTypedefs
// * @return term
// * @throws CannotCompileException
// */
public Class createTerm(String id, String name, String def, String[] comments, Class[] is_a, Class[] relTypes, String[] relTypedefs) throws CannotCompileException {
String ccClassName = unmangleName(name);
if (created.containsKey(id)) {
return created.get(id);
}
if (created.containsKey(ccClassName)) {
return created.get(ccClassName);
}
OBOAnnotationParser obo = new OBOAnnotationParser();
ClassPool cp = ClassPool.getDefault();
CtClass stringClass = null, stringArrayClass = null;
try {
stringClass = cp.get("java.lang.String");
stringArrayClass = cp.get("java.lang.String[]");
} catch (NotFoundException e) {
throw new IllegalStateException(e);
}
CtClass cc = cp.makeInterface(ccClassName);
cc.setModifiers(javassist.Modifier.INTERFACE | javassist.Modifier.PUBLIC);
ClassFile ccFile = cc.getClassFile();
ConstPool constpool = ccFile.getConstPool();
Annotation termAnnotation = new Annotation("org.sc.obo.annotations.Term", constpool);
CtField idField = new CtField(stringClass, "id", cc);
idField.setModifiers(javassist.Modifier.PUBLIC | javassist.Modifier.STATIC | javassist.Modifier.FINAL);
CtField nameField = new CtField(stringClass, "name", cc);
nameField.setModifiers(javassist.Modifier.PUBLIC | javassist.Modifier.STATIC | javassist.Modifier.FINAL);
CtField defField = new CtField(stringClass, "def", cc);
defField.setModifiers(javassist.Modifier.PUBLIC | javassist.Modifier.STATIC | javassist.Modifier.FINAL);
cc.addField(idField, CtField.Initializer.constant(removeSlashes(id)));
cc.addField(nameField, CtField.Initializer.constant(removeSlashes(name)));
cc.addField(defField, CtField.Initializer.constant(removeSlashes(def)));
if (is_a != null) {
for (Class superClass : is_a) {
if (!obo.isTerm(superClass)) {
throw new IllegalArgumentException(superClass.getCanonicalName());
}
try {
CtClass superCtClass = cp.get(superClass.getCanonicalName());
cc.addInterface(superCtClass);
} catch (NotFoundException e) {
throw new IllegalArgumentException(e);
}
}
}
/*
* Finally, we convert all the relTypes/relTypedefs into methods.
*
* The main trick here is to do this in a way that the method names don't clash!
*
* We want to rename each method to a form that's unique to this class as well -- the reason is that
* Class A and Class B can have incompatible methods with the same name, which is fine since neither of
* them is a superclass (super-interface, whatevs) of the other.
*
* However, as soon as we define a Class C that extends both interfaces A & B, we have a problem -- suddenly
* we inherit both methods, but with incompatible types.
*
* So we need to mangle the names of A and B's classes, so that they are (a) descriptive, but (b) don't
* clash with other class's method names. This leads to ugly code generation, but ... it works.
*/
if (relTypes != null && relTypedefs != null) {
if (relTypes.length != relTypedefs.length) {
throw new IllegalArgumentException();
}
String[] nonDups = renameDuplicates(relTypedefs);
for (int i = 0; i < relTypes.length; i++) {
try {
if (relTypes[i] == null) {
throw new IllegalArgumentException(id + " " + Arrays.asList(relTypes));
}
Class arrayType = relTypes[i].isArray() ? relTypes[i] : getArrayType(relTypes[i]);
String typeName = arrayType.getCanonicalName();
String methodName = findNonConflictingName(ccClassName, arrayType, nonDups[i], null, is_a);
CtClass relTypeClass = cp.get(typeName);
CtMethod relMethod = new CtMethod(relTypeClass, methodName, new CtClass[] {}, cc);
relMethod.setModifiers(Modifier.PUBLIC | Modifier.ABSTRACT);
// We need to create this with the *original* typedef name,
// not the new (non-clashing) method name. That way, we can recover the original
// name of the property from the (mangled) method name.
Annotation relAnnotation = new Annotation("org.sc.obo.annotations.Relates", constpool);
relAnnotation.addMemberValue("value", new StringMemberValue(relTypedefs[i], ccFile.getConstPool()));
AnnotationsAttribute annotations = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
annotations.addAnnotation(relAnnotation);
relMethod.getMethodInfo().addAttribute(annotations);
cc.addMethod(relMethod);
} catch (NotFoundException e) {
throw new IllegalArgumentException(e);
}
}
}
AnnotationsAttribute attr = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
attr.addAnnotation(termAnnotation);
ccFile.addAttribute(attr);
Class c = cc.toClass();
created.put(id, c);
created.put(ccClassName, c);
return c;
}
use of javassist.CtField in project motech by motech.
the class JavassistBuilder method createField.
/**
* Creates class field with the given name for the given class declaration and type.
*
* @param declaring the class to which the field will be added
* @param type the field type
* @param name the field name
* @param genericSignature the generic signature
* @return An instance of {@link javassist.CtField} represents a field
* @throws CannotCompileException when bytecode transformation has failed
*/
public static CtField createField(CtClass declaring, CtClass type, String name, String genericSignature) throws CannotCompileException {
String fieldName = uncapitalize(name);
CtField field = new CtField(type, fieldName, declaring);
field.setModifiers(Modifier.PRIVATE);
if (isNotBlank(genericSignature)) {
field.setGenericSignature(genericSignature);
}
return field;
}
use of javassist.CtField in project motech by motech.
the class EntityBuilderImpl method addProperty.
private void addProperty(CtClass declaring, String typeClassName, String propertyName, String defaultValue) {
try {
String name = uncapitalize(propertyName);
JavassistUtil.removeFieldIfExists(declaring, propertyName);
CtClass type = classPool.getOrNull(typeClassName);
CtField field = JavassistBuilder.createField(declaring, type, propertyName, null);
if (isBlank(defaultValue)) {
declaring.addField(field);
} else {
CtField.Initializer initializer = JavassistBuilder.createInitializer(typeClassName, defaultValue);
declaring.addField(field, initializer);
}
createGetter(declaring, name, field);
createSetter(declaring, name, field);
} catch (CannotCompileException e) {
throw new PropertyCreationException("Error while creating property " + propertyName, e);
}
}
use of javassist.CtField in project BroadleafCommerce by BroadleafCommerce.
the class AnnotationsCopyClassTransformer method transform.
@Override
public byte[] transform(ClassLoader loader, String className, Class<?> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) throws IllegalClassFormatException {
// Lambdas and anonymous methods in Java 8 do not have a class name defined and so no transformation should be done
if (className == null) {
return null;
}
String convertedClassName = className.replace('/', '.');
if (xformTemplates.containsKey(convertedClassName)) {
String xformKey = convertedClassName;
String[] xformVals = xformTemplates.get(xformKey).split(",");
logger.lifecycle(LifeCycleEvent.START, String.format("Transform - Copying annotations into [%s] from [%s]", xformKey, StringUtils.join(xformVals, ",")));
CtClass clazz = null;
try {
// Load the destination class and defrost it so it is eligible for modifications
ClassPool classPool = ClassPool.getDefault();
clazz = classPool.makeClass(new ByteArrayInputStream(classfileBuffer), false);
clazz.defrost();
for (String xformVal : xformVals) {
// Load the source class
String trimmed = xformVal.trim();
classPool.appendClassPath(new LoaderClassPath(Class.forName(trimmed).getClassLoader()));
CtClass template = classPool.get(trimmed);
// Copy over all declared annotations from fields from the template class
// Note that we do not copy over fields with the @NonCopiedField annotation
CtField[] fieldsToCopy = template.getDeclaredFields();
for (CtField field : fieldsToCopy) {
if (field.hasAnnotation(NonCopied.class)) {
logger.debug(String.format("Not copying annotation from field [%s]", field.getName()));
} else {
logger.debug(String.format("Copying annotation from field [%s]", field.getName()));
ConstPool constPool = clazz.getClassFile().getConstPool();
CtField fieldFromMainClass = clazz.getField(field.getName());
for (Object o : field.getFieldInfo().getAttributes()) {
if (o instanceof AnnotationsAttribute) {
AnnotationsAttribute templateAnnotations = (AnnotationsAttribute) o;
// have to make a copy of the annotations from the target
AnnotationsAttribute copied = (AnnotationsAttribute) templateAnnotations.copy(constPool, null);
// add all the copied annotations into the target class's field.
for (Object attribute : fieldFromMainClass.getFieldInfo().getAttributes()) {
if (attribute instanceof AnnotationsAttribute) {
for (Annotation annotation : copied.getAnnotations()) {
((AnnotationsAttribute) attribute).addAnnotation(annotation);
}
}
}
}
}
}
}
}
logger.lifecycle(LifeCycleEvent.END, String.format("Transform - Copying annotations into [%s] from [%s]", xformKey, StringUtils.join(xformVals, ",")));
return clazz.toBytecode();
} catch (Exception e) {
throw new RuntimeException("Unable to transform class", e);
} finally {
if (clazz != null) {
clazz.detach();
}
}
}
return null;
}
Aggregations