use of javassist.CannotCompileException 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)) {
if (!enhancementContext.isMappedCollection(ctField)) {
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.CannotCompileException in project hibernate-orm by hibernate.
the class EntityEnhancer method createCollectionDirtyCheckGetFieldsMethod.
private void createCollectionDirtyCheckGetFieldsMethod(CtClass managedCtClass) {
try {
final StringBuilder body = new StringBuilder();
body.append(String.format("private void %1$s(%3$s tracker) {%n" + " if (%2$s == null) { return; }%n%n", EnhancerConstants.TRACKER_COLLECTION_CHANGED_FIELD_NAME, EnhancerConstants.TRACKER_COLLECTION_NAME, DirtyTracker.class.getName()));
for (CtField ctField : collectCollectionFields(managedCtClass)) {
if (!enhancementContext.isMappedCollection(ctField)) {
body.append(String.format(" // Collection field [%1$s]%n" + " if (%1$s == null && %2$s.getSize(\"%1$s\") != -1) { tracker.add(\"%1$s\"); }%n" + " if (%1$s != null && %2$s.getSize(\"%1$s\") != %1$s.size()) { tracker.add(\"%1$s\"); }%n%n", ctField.getName(), EnhancerConstants.TRACKER_COLLECTION_NAME));
}
}
body.append("}");
MethodWriter.write(managedCtClass, body.toString());
} catch (CannotCompileException cce) {
cce.printStackTrace();
}
}
use of javassist.CannotCompileException in project hibernate-orm by hibernate.
the class FieldWriter method addWithModifiers.
private static void addWithModifiers(CtClass target, CtClass type, String name, int modifiers, Class<?>... annotations) {
try {
final CtField f = new CtField(type, name, target);
f.setModifiers(f.getModifiers() | modifiers);
addAnnotations(f.getFieldInfo(), annotations);
target.addField(f);
} catch (CannotCompileException cce) {
final String msg = String.format("Could not enhance class [%s] to add field [%s]", target.getName(), name);
throw new EnhancementException(msg, cce);
}
}
use of javassist.CannotCompileException 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.CannotCompileException in project incubator-systemml by apache.
the class GenerateClassesForMLContext method addPackageConvenienceMethodsToMLContext.
/**
* Add methods to MLContext to allow tab-completion to packages contained
* within the source directory (such as {@code ml.nn()}).
*
* @param dirPath
* path to source directory (typically, the scripts directory)
* @param ctMLContext
* javassist compile-time class representation of MLContext
*/
public static void addPackageConvenienceMethodsToMLContext(String dirPath, CtClass ctMLContext) {
try {
if (!SOURCE.equalsIgnoreCase(dirPath)) {
return;
}
File dir = new File(dirPath);
File[] subdirs = dir.listFiles(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory();
}
});
for (File subdir : subdirs) {
String subDirPath = dirPath + File.separator + subdir.getName();
if (skipDir(subdir, false)) {
continue;
}
String fullSubDirClassName = dirPathToFullDirClassName(subDirPath);
ClassPool pool = ClassPool.getDefault();
CtClass subDirClass = pool.get(fullSubDirClassName);
String subDirName = subdir.getName();
subDirName = subDirName.replaceAll("-", "_");
subDirName = subDirName.toLowerCase();
System.out.println("Adding " + subDirName + "() to " + ctMLContext.getName());
String methodBody = "{ " + fullSubDirClassName + " z = new " + fullSubDirClassName + "(); return z; }";
CtMethod ctMethod = CtNewMethod.make(Modifier.PUBLIC, subDirClass, subDirName, null, null, methodBody, ctMLContext);
ctMLContext.addMethod(ctMethod);
}
} catch (NotFoundException e) {
e.printStackTrace();
} catch (CannotCompileException e) {
e.printStackTrace();
}
}
Aggregations