use of javassist.NotFoundException in project pinpoint by naver.
the class JavassistClass method getNestedClasses.
@Override
public List<InstrumentClass> getNestedClasses(ClassFilter filter) {
List<InstrumentClass> list = new ArrayList<InstrumentClass>();
CtClass[] nestedClasses;
try {
nestedClasses = ctClass.getNestedClasses();
} catch (NotFoundException ex) {
return list;
}
if (nestedClasses == null || nestedClasses.length == 0) {
return list;
}
for (CtClass nested : nestedClasses) {
final InstrumentClass clazz = new JavassistClass(objectBinderFactory, pluginContext, interceptorRegistryBinder, apiMetaDataService, classLoader, nested);
if (filter.accept(clazz)) {
list.add(clazz);
}
}
return list;
}
use of javassist.NotFoundException in project pinpoint by naver.
the class JavassistClass method weave.
@Override
public void weave(String adviceClassName) throws InstrumentException {
pluginContext.injectClass(classLoader, adviceClassName);
CtClass adviceClass;
try {
adviceClass = getCtClass(adviceClassName);
} catch (NotFoundException e) {
throw new NotFoundInstrumentException(adviceClassName + " not found. Caused:" + e.getMessage(), e);
}
try {
AspectWeaverClass weaverClass = new AspectWeaverClass();
weaverClass.weaving(ctClass, adviceClass);
} catch (CannotCompileException e) {
throw new InstrumentException("weaving fail. sourceClassName:" + ctClass.getName() + " adviceClassName:" + adviceClassName + " Caused:" + e.getMessage(), e);
} catch (NotFoundException e) {
throw new InstrumentException("weaving fail. sourceClassName:" + ctClass.getName() + " adviceClassName:" + adviceClassName + " Caused:" + e.getMessage(), e);
}
}
use of javassist.NotFoundException in project hibernate-orm by hibernate.
the class MethodWriter method addSetter.
public static CtMethod addSetter(CtClass target, String field, String name) {
CtField actualField = null;
try {
actualField = target.getField(field);
log.debugf("Writing setter method [%s] into [%s] for field [%s]", name, target.getName(), field);
CtMethod method = CtNewMethod.setter(name, actualField);
target.addMethod(method);
return method;
} catch (CannotCompileException cce) {
try {
// Fall back to create a getter from delegation.
CtMethod method = CtNewMethod.delegator(CtNewMethod.setter(name, actualField), target);
target.addMethod(method);
return method;
} catch (CannotCompileException ignored) {
String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
throw new EnhancementException(msg, cce);
}
} catch (NotFoundException nfe) {
String msg = String.format("Could not enhance class [%s] to add method [%s] for field [%s]", target.getName(), name, field);
throw new EnhancementException(msg, nfe);
}
}
use of javassist.NotFoundException in project hibernate-orm by hibernate.
the class PersistentAttributesEnhancer method handleBiDirectionalAssociation.
private void handleBiDirectionalAssociation(CtClass managedCtClass, CtField persistentField, CtMethod fieldWriter) throws NotFoundException, CannotCompileException {
if (!PersistentAttributesHelper.isPossibleBiDirectionalAssociation(persistentField)) {
return;
}
final CtClass targetEntity = PersistentAttributesHelper.getTargetEntityClass(managedCtClass, persistentField);
if (targetEntity == null) {
log.infof("Could not find type of bi-directional association for field [%s#%s]", managedCtClass.getName(), persistentField.getName());
return;
}
final String mappedBy = PersistentAttributesHelper.getMappedBy(persistentField, targetEntity, enhancementContext);
if (mappedBy == null || mappedBy.isEmpty()) {
log.infof("Could not find bi-directional association for field [%s#%s]", managedCtClass.getName(), persistentField.getName());
return;
}
// create a temporary getter and setter on the target entity to be able to compile our code
final String mappedByGetterName = EnhancerConstants.PERSISTENT_FIELD_READER_PREFIX + mappedBy;
final String mappedBySetterName = EnhancerConstants.PERSISTENT_FIELD_WRITER_PREFIX + mappedBy;
CtMethod getter;
CtMethod setter;
boolean tmpTargetMethods = false;
try {
getter = targetEntity.getDeclaredMethod(mappedByGetterName);
setter = targetEntity.getDeclaredMethod(mappedByGetterName);
} catch (NotFoundException nfe) {
getter = MethodWriter.addGetter(targetEntity, mappedBy, mappedByGetterName);
setter = MethodWriter.addSetter(targetEntity, mappedBy, mappedBySetterName);
tmpTargetMethods = true;
}
// code fragments to check loaded state. We don't want to trigger lazy loading in association management code
String currentAssociationLoaded = String.format("%s.isPropertyInitialized(this.%s, \"%s\")", Hibernate.class.getName(), persistentField.getName(), mappedBy);
String targetElementLoaded = String.format("%s.isPropertyInitialized(target, \"%s\")", Hibernate.class.getName(), mappedBy);
String newAssociationLoaded = String.format("%s.isPropertyInitialized($1, \"%s\")", Hibernate.class.getName(), mappedBy);
if (PersistentAttributesHelper.hasAnnotation(persistentField, OneToOne.class)) {
// only unset when $1 != null to avoid recursion
fieldWriter.insertBefore(String.format(" if (this.%1$s != null && %2$s && $1 != null) { this.%1$s.%3$s(null); }%n", persistentField.getName(), currentAssociationLoaded, mappedBySetterName));
fieldWriter.insertAfter(String.format(" if ($1 != null && %s && $1.%s() != this) { $1.%s(this); }%n", newAssociationLoaded, mappedByGetterName, mappedBySetterName));
}
if (PersistentAttributesHelper.hasAnnotation(persistentField, OneToMany.class)) {
boolean isMap = PersistentAttributesHelper.isAssignable(persistentField.getType(), Map.class.getName());
String toArrayMethod = isMap ? "values().toArray()" : "toArray()";
// only remove elements not in the new collection or else we would loose those elements
// don't use iterator to avoid ConcurrentModException
fieldWriter.insertBefore(String.format(" if (this.%3$s != null && %1$s) {%n" + " Object[] array = this.%3$s.%2$s;%n" + " for (int i = 0; i < array.length; i++) {%n" + " %4$s target = (%4$s) array[i];%n" + " if ($1 == null || !$1.contains(target)) { target.%5$s(null); }%n" + " }%n" + " }%n", currentAssociationLoaded, toArrayMethod, persistentField.getName(), targetEntity.getName(), mappedBySetterName));
fieldWriter.insertAfter(String.format(" if ($1 != null && %1$s) {%n" + " Object[] array = $1.%2$s;%n" + " for (int i = 0; i < array.length; i++) {%n" + " %4$s target = (%4$s) array[i];%n" + " if (%3$s && target.%5$s() != this) { target.%6$s(this); }%n" + " }%n" + " }%n", newAssociationLoaded, toArrayMethod, targetElementLoaded, targetEntity.getName(), mappedByGetterName, mappedBySetterName));
}
if (PersistentAttributesHelper.hasAnnotation(persistentField, ManyToOne.class)) {
fieldWriter.insertBefore(String.format(" if (this.%2$s != null && %1$s && this.%2$s.%3$s() != null) { this.%2$s.%3$s().remove(this); }%n", currentAssociationLoaded, persistentField.getName(), mappedByGetterName));
// check .contains(this) to avoid double inserts (but preventing duplicates)
fieldWriter.insertAfter(String.format(" if ($1 != null && %s) {%n" + " java.util.Collection c = $1.%s();%n" + " if (c != null && !c.contains(this)) { c.add(this); }%n" + " }%n", newAssociationLoaded, mappedByGetterName));
}
if (PersistentAttributesHelper.hasAnnotation(persistentField, ManyToMany.class)) {
if (PersistentAttributesHelper.isAssignable(persistentField.getType(), Map.class.getName()) || PersistentAttributesHelper.isAssignable(targetEntity.getField(mappedBy).getType(), Map.class.getName())) {
log.infof("Bi-directional association for field [%s#%s] not managed: @ManyToMany in java.util.Map attribute not supported ", managedCtClass.getName(), persistentField.getName());
return;
}
fieldWriter.insertBefore(String.format(" if (this.%2$s != null && %1$s) {%n" + " Object[] array = this.%2$s.toArray();%n" + " for (int i = 0; i < array.length; i++) {%n" + " %3$s target = (%3$s) array[i];%n" + " if ($1 == null || !$1.contains(target)) { target.%4$s().remove(this); }%n" + " }%n" + " }%n", currentAssociationLoaded, persistentField.getName(), targetEntity.getName(), mappedByGetterName));
fieldWriter.insertAfter(String.format(" if ($1 != null && %s) {%n" + " Object[] array = $1.toArray();%n" + " for (int i = 0; i < array.length; i++) {%n" + " %s target = (%s) array[i];%n" + " if (%s) {%n" + " java.util.Collection c = target.%s();%n" + " if (c != this && c != null) { c.add(this); }%n" + " }%n" + " }%n" + " }%n", newAssociationLoaded, targetEntity.getName(), targetEntity.getName(), targetElementLoaded, mappedByGetterName));
}
if (tmpTargetMethods) {
targetEntity.removeMethod(getter);
targetEntity.removeMethod(setter);
}
}
use of javassist.NotFoundException in project hibernate-orm by hibernate.
the class PersistentAttributesHelper method getterOrNull.
private static CtMethod getterOrNull(CtClass containerClass, String propertyName) {
for (CtMethod method : containerClass.getDeclaredMethods()) {
try {
// if the method has parameters, skip it
if (method.isEmpty() || method.getParameterTypes().length != 0) {
continue;
}
} catch (NotFoundException e) {
continue;
}
final String methodName = method.getName();
// try "get"
if (methodName.startsWith("get")) {
String testStdMethod = Introspector.decapitalize(methodName.substring(3));
String testOldMethod = methodName.substring(3);
if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {
return method;
}
}
// if not "get", then try "is"
if (methodName.startsWith("is")) {
String testStdMethod = Introspector.decapitalize(methodName.substring(2));
String testOldMethod = methodName.substring(2);
if (testStdMethod.equals(propertyName) || testOldMethod.equals(propertyName)) {
return method;
}
}
}
return null;
}
Aggregations