Search in sources :

Example 11 with NotFoundException

use of javassist.NotFoundException in project hibernate-orm by hibernate.

the class PersistentAttributesHelper method getTargetEntityClass.

public static CtClass getTargetEntityClass(CtClass managedCtClass, CtField persistentField) throws NotFoundException {
    // get targetEntity defined in the annotation
    try {
        OneToOne oto = PersistentAttributesHelper.getAnnotation(persistentField, OneToOne.class);
        OneToMany otm = PersistentAttributesHelper.getAnnotation(persistentField, OneToMany.class);
        ManyToOne mto = PersistentAttributesHelper.getAnnotation(persistentField, ManyToOne.class);
        ManyToMany mtm = PersistentAttributesHelper.getAnnotation(persistentField, ManyToMany.class);
        Class<?> targetClass = null;
        if (oto != null) {
            targetClass = oto.targetEntity();
        }
        if (otm != null) {
            targetClass = otm.targetEntity();
        }
        if (mto != null) {
            targetClass = mto.targetEntity();
        }
        if (mtm != null) {
            targetClass = mtm.targetEntity();
        }
        if (targetClass != null && targetClass != void.class) {
            return managedCtClass.getClassPool().get(targetClass.getName());
        }
    } catch (NotFoundException ignore) {
    }
    // infer targetEntity from generic type signature
    String inferredTypeName = inferTypeName(managedCtClass, persistentField.getName());
    return inferredTypeName == null ? null : managedCtClass.getClassPool().get(inferredTypeName);
}
Also used : OneToOne(javax.persistence.OneToOne) ManyToMany(javax.persistence.ManyToMany) NotFoundException(javassist.NotFoundException) OneToMany(javax.persistence.OneToMany) ManyToOne(javax.persistence.ManyToOne)

Example 12 with NotFoundException

use of javassist.NotFoundException 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)

Example 13 with NotFoundException

use of javassist.NotFoundException in project incubator-systemml by apache.

the class GenerateClassesForMLContext method addConvenienceMethodsToMLContext.

/**
	 * Add methods to MLContext to allow tab-completion to folders/packages
	 * (such as {@code ml.scripts()} and {@code ml.nn()}).
	 * 
	 * @param source
	 *            path to source directory (typically, the scripts directory)
	 * @param fullDirClassName
	 *            the full name of the class representing the source (scripts)
	 *            directory
	 */
public static void addConvenienceMethodsToMLContext(String source, String fullDirClassName) {
    try {
        ClassPool pool = ClassPool.getDefault();
        CtClass ctMLContext = pool.get(MLContext.class.getName());
        CtClass dirClass = pool.get(fullDirClassName);
        String methodName = convertFullClassNameToConvenienceMethodName(fullDirClassName);
        System.out.println("Adding " + methodName + "() to " + ctMLContext.getName());
        String methodBody = "{ " + fullDirClassName + " z = new " + fullDirClassName + "(); return z; }";
        CtMethod ctMethod = CtNewMethod.make(Modifier.PUBLIC, dirClass, methodName, null, null, methodBody, ctMLContext);
        ctMLContext.addMethod(ctMethod);
        addPackageConvenienceMethodsToMLContext(source, ctMLContext);
        ctMLContext.writeFile(destination);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (RuntimeException e) {
        e.printStackTrace();
    } catch (NotFoundException e) {
        e.printStackTrace();
    } catch (CannotCompileException e) {
        e.printStackTrace();
    }
}
Also used : CtClass(javassist.CtClass) ClassPool(javassist.ClassPool) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(javassist.NotFoundException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) MLContext(org.apache.sysml.api.mlcontext.MLContext) CtMethod(javassist.CtMethod)

Example 14 with NotFoundException

use of javassist.NotFoundException in project gwt-test-utils by gwt-test-utils.

the class AutomaticPatcher method getInitMethod.

private CtMethod getInitMethod(Set<CtClass> patchClasses) {
    List<CtMethod> initMethods = new ArrayList<CtMethod>();
    for (CtClass patchClass : patchClasses) {
        for (CtMethod ctMethod : patchClass.getDeclaredMethods()) {
            if (ctMethod.hasAnnotation(InitMethod.class)) {
                if (!Modifier.isStatic(ctMethod.getModifiers())) {
                    throw new GwtTestPatchException("@" + InitMethod.class.getSimpleName() + " has to be static : '" + ctMethod.getLongName() + "'");
                }
                try {
                    if (ctMethod.getParameterTypes().length != 1 || ctMethod.getParameterTypes()[0] != GwtClassPool.getCtClass(CtClass.class)) {
                        throw new GwtTestPatchException("@" + InitMethod.class.getName() + " method must have one and only one parameter of type '" + CtClass.class.getName() + "'");
                    }
                } catch (NotFoundException e) {
                    // should never happen
                    throw new GwtTestPatchException(e);
                }
                initMethods.add(ctMethod);
            }
        }
    }
    CtMethod initMethod = getMethodToUse(initMethods, InitMethod.class);
    if (initMethod != null) {
        initMethod.setModifiers(Modifier.PUBLIC + Modifier.STATIC);
    }
    return initMethod;
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) InitMethod(com.googlecode.gwt.test.patchers.InitMethod) CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException) CtMethod(javassist.CtMethod)

Example 15 with NotFoundException

use of javassist.NotFoundException in project gwt-test-utils by gwt-test-utils.

the class ClassesScanner method visitClass.

private void visitClass(String classFileName, ClassVisitor classVisitor) {
    try {
        CtClass current = GwtClassPool.getClass(classFileName.substring(0, classFileName.length() - ".class".length()));
        classVisitor.visit(current);
        for (CtClass innerClass : current.getNestedClasses()) {
            classVisitor.visit(innerClass);
        }
    } catch (NotFoundException e) {
    // do nothing
    }
}
Also used : CtClass(javassist.CtClass) NotFoundException(javassist.NotFoundException)

Aggregations

NotFoundException (javassist.NotFoundException)35 CtClass (javassist.CtClass)26 CannotCompileException (javassist.CannotCompileException)19 CtMethod (javassist.CtMethod)15 ClassPool (javassist.ClassPool)12 IOException (java.io.IOException)5 CtField (javassist.CtField)5 EnhancementException (org.hibernate.bytecode.enhance.spi.EnhancementException)5 FileNotFoundException (java.io.FileNotFoundException)4 BadBytecode (javassist.bytecode.BadBytecode)4 CodeIterator (javassist.bytecode.CodeIterator)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 Bytecode (javassist.bytecode.Bytecode)3 CodeAttribute (javassist.bytecode.CodeAttribute)3 CompileError (javassist.compiler.CompileError)3 Javac (javassist.compiler.Javac)3 FileFilter (java.io.FileFilter)2 CtBehavior (javassist.CtBehavior)2 CtConstructor (javassist.CtConstructor)2