Search in sources :

Example 6 with CannotCompileException

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();
    }
}
Also used : PersistentAttributeInterceptable(org.hibernate.engine.spi.PersistentAttributeInterceptable) CtField(javassist.CtField) CannotCompileException(javassist.CannotCompileException)

Example 7 with CannotCompileException

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();
    }
}
Also used : CtField(javassist.CtField) CannotCompileException(javassist.CannotCompileException)

Example 8 with CannotCompileException

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);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) CannotCompileException(javassist.CannotCompileException)

Example 9 with CannotCompileException

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);
    }
}
Also used : CtField(javassist.CtField) EnhancementException(org.hibernate.bytecode.enhance.spi.EnhancementException) NotFoundException(javassist.NotFoundException) CannotCompileException(javassist.CannotCompileException) CtMethod(javassist.CtMethod)

Example 10 with CannotCompileException

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();
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) CannotCompileException(javassist.CannotCompileException) FileFilter(java.io.FileFilter) File(java.io.File) CtMethod(javassist.CtMethod)

Aggregations

CannotCompileException (javassist.CannotCompileException)28 NotFoundException (javassist.NotFoundException)19 CtClass (javassist.CtClass)17 CtMethod (javassist.CtMethod)16 ClassPool (javassist.ClassPool)9 CtField (javassist.CtField)8 IOException (java.io.IOException)7 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 FileNotFoundException (java.io.FileNotFoundException)4 File (java.io.File)3 CtConstructor (javassist.CtConstructor)3 BadBytecode (javassist.bytecode.BadBytecode)3 Bytecode (javassist.bytecode.Bytecode)3 CodeAttribute (javassist.bytecode.CodeAttribute)3 CodeIterator (javassist.bytecode.CodeIterator)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3 FileFilter (java.io.FileFilter)2 AsyncProvider (com.google.gwt.inject.client.AsyncProvider)1 AsyncCallback (com.google.gwt.user.client.rpc.AsyncCallback)1