Search in sources :

Example 1 with GwtTestPatchException

use of com.googlecode.gwt.test.exceptions.GwtTestPatchException in project gwt-test-utils by gwt-test-utils.

the class GwtTranslator method applyJavaClassModifiers.

private void applyJavaClassModifiers(CtClass ctClass) {
    try {
        // Apply internal modifiers
        serializableModifier.modify(ctClass);
        hasHTMLModifier.modify(ctClass);
        hasNameModifier.modify(ctClass);
        classVisibilityModifier.modify(ctClass);
    } catch (Exception e) {
        if (GwtTestException.class.isInstance(e)) {
            throw (GwtTestException) e;
        }
        throw new GwtTestPatchException(e);
    }
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) GwtTestException(com.googlecode.gwt.test.exceptions.GwtTestException) NotFoundException(javassist.NotFoundException) GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException)

Example 2 with GwtTestPatchException

use of com.googlecode.gwt.test.exceptions.GwtTestPatchException in project gwt-test-utils by gwt-test-utils.

the class AutomaticPatcher method getOverrideMethod.

private <T extends Annotation> CtMethod getOverrideMethod(List<CtMethod> methods, Class<T> annotationClass) {
    CtMethod overrideInitMethod = null;
    for (CtMethod method : methods) {
        T annotation = getMethodAnnotation(method, annotationClass);
        if (isOverride(annotation)) {
            if (overrideInitMethod != null) {
                throw new GwtTestPatchException("There are more than one @" + annotationClass.getSimpleName() + " with 'override=true' for the same target : '" + method.getLongName() + "' and '" + overrideInitMethod.getLongName() + "'");
            } else {
                overrideInitMethod = method;
            }
        }
    }
    if (overrideInitMethod == null) {
        StringBuilder sb = new StringBuilder();
        sb.append(methods.size()).append(" @");
        sb.append(annotationClass.getSimpleName());
        sb.append(" methods detected for the same target, but no one is set to override the other. You must use 'override=true' on the one which should be applied : ");
        for (CtMethod method : methods) {
            sb.append("'").append(method.getLongName()).append("'");
            sb.append(" or ");
        }
        throw new GwtTestPatchException(sb.substring(0, sb.length() - 4));
    }
    return overrideInitMethod;
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) CtMethod(javassist.CtMethod)

Example 3 with GwtTestPatchException

use of com.googlecode.gwt.test.exceptions.GwtTestPatchException in project gwt-test-utils by gwt-test-utils.

the class AutomaticPatcher method getPatchMethods.

private Set<CtMethod> getPatchMethods(Set<CtClass> patchClasses) {
    Set<CtMethod> result = new HashSet<CtMethod>();
    // add all @PatchMethod found in a temporary map
    Map<String, List<CtMethod>> temp = new HashMap<String, List<CtMethod>>();
    for (CtClass patchClass : patchClasses) {
        for (CtMethod ctMethod : patchClass.getDeclaredMethods()) {
            if (ctMethod.hasAnnotation(PatchMethod.class)) {
                if (!Modifier.isStatic(ctMethod.getModifiers())) {
                    throw new GwtTestPatchException("@" + PatchMethod.class.getName() + " has to be static : '" + ctMethod.getLongName() + "'");
                }
                String nameAndSignature = ctMethod.getName() + Descriptor.toString(ctMethod.getSignature());
                List<CtMethod> correspondingMethods = temp.get(nameAndSignature);
                if (correspondingMethods == null) {
                    correspondingMethods = new ArrayList<CtMethod>();
                    temp.put(nameAndSignature, correspondingMethods);
                }
                correspondingMethods.add(ctMethod);
            }
        }
    }
    // override=true
    for (Map.Entry<String, List<CtMethod>> entry : temp.entrySet()) {
        CtMethod methodToUse = getMethodToUse(entry.getValue(), PatchMethod.class);
        methodToUse.setModifiers(Modifier.PUBLIC + Modifier.STATIC);
        result.add(methodToUse);
    }
    return result;
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) CtClass(javassist.CtClass) PatchMethod(com.googlecode.gwt.test.patchers.PatchMethod) CtMethod(javassist.CtMethod)

Example 4 with GwtTestPatchException

use of com.googlecode.gwt.test.exceptions.GwtTestPatchException in project gwt-test-utils by gwt-test-utils.

the class ClassesScanner method scanPackages.

public void scanPackages(ClassVisitor classVisitor, Set<String> rootPackages) {
    for (String rootPackage : rootPackages) {
        String path = rootPackage.replaceAll("\\.", "/");
        logger.debug("Scan package " + rootPackage);
        if (rootPackage.endsWith(".")) {
            rootPackage = rootPackage.substring(0, rootPackage.length() - 1);
        }
        try {
            Enumeration<URL> l = Thread.currentThread().getContextClassLoader().getResources(path);
            while (l.hasMoreElements()) {
                URL url = l.nextElement();
                String u = url.toExternalForm();
                if (u.startsWith("file:")) {
                    String directoryName = u.substring("file:".length());
                    directoryName = URLDecoder.decode(directoryName, "UTF-8");
                    scanClassesFromDirectory(new File(directoryName), rootPackage, classVisitor);
                } else if (u.startsWith("jar:file:")) {
                    scanClassesFromJarFile(u.substring("jar:file:".length()), path, classVisitor);
                } else {
                    throw new IllegalArgumentException("Not managed class container " + u);
                }
            }
        } catch (Exception e) {
            throw new GwtTestPatchException("Error while scanning package '" + rootPackage + "'", e);
        }
    }
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) JarFile(java.util.jar.JarFile) File(java.io.File) URL(java.net.URL) NotFoundException(javassist.NotFoundException) GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException)

Example 5 with GwtTestPatchException

use of com.googlecode.gwt.test.exceptions.GwtTestPatchException in project gwt-test-utils by gwt-test-utils.

the class GwtClassLoader method findClass.

@Override
protected Class<?> findClass(String className) throws ClassNotFoundException {
    byte[] classfile = findClassBytes(className);
    int i = className.lastIndexOf('.');
    if (i != -1) {
        String pname = className.substring(0, i);
        if (getPackage(pname) == null) {
            try {
                definePackage(pname, null, null, null, null, null, null, null);
            } catch (IllegalArgumentException e) {
            // ignore. maybe the package object for the same
            // name has been created just right away.
            }
        }
    }
    try {
        if (domain == null) {
            return defineClass(className, classfile, 0, classfile.length);
        } else {
            return defineClass(className, classfile, 0, classfile.length, domain);
        }
    } catch (Throwable t) {
        throw new GwtTestPatchException("Error while defining " + className + " from modified bytecode", t);
    }
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException)

Aggregations

GwtTestPatchException (com.googlecode.gwt.test.exceptions.GwtTestPatchException)9 CtClass (javassist.CtClass)3 CtMethod (javassist.CtMethod)3 NotFoundException (javassist.NotFoundException)3 PatchMethod (com.googlecode.gwt.test.patchers.PatchMethod)2 SafeHtml (com.google.gwt.safehtml.shared.SafeHtml)1 GwtTestException (com.googlecode.gwt.test.exceptions.GwtTestException)1 ClassVisitor (com.googlecode.gwt.test.internal.ClassesScanner.ClassVisitor)1 InitMethod (com.googlecode.gwt.test.patchers.InitMethod)1 PatchClass (com.googlecode.gwt.test.patchers.PatchClass)1 File (java.io.File)1 StringReader (java.io.StringReader)1 URL (java.net.URL)1 JarFile (java.util.jar.JarFile)1 Annotation (javassist.bytecode.annotation.Annotation)1 ClassMemberValue (javassist.bytecode.annotation.ClassMemberValue)1 StringMemberValue (javassist.bytecode.annotation.StringMemberValue)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 XMLReader (org.xml.sax.XMLReader)1