Search in sources :

Example 6 with GwtTestPatchException

use of com.googlecode.gwt.test.exceptions.GwtTestPatchException 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 7 with GwtTestPatchException

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

the class ConfigurationLoader method visitPatchClasses.

private void visitPatchClasses() {
    final Map<String, Set<CtClass>> patchClassMap = new HashMap<String, Set<CtClass>>();
    ClassVisitor patchClassVisitor = new ClassVisitor() {

        public void visit(CtClass ctClass) {
            try {
                if (ctClass.hasAnnotation(PatchClass.class)) {
                    Annotation annotation = JavassistUtils.getAnnotation(ctClass, PatchClass.class);
                    String classToPatchName = PatchClass.class.getName();
                    ClassMemberValue value = (ClassMemberValue) annotation.getMemberValue("value");
                    if (value != null) {
                        classToPatchName = value.getValue();
                    }
                    if (classToPatchName.equals(PatchClass.class.getName())) {
                        StringMemberValue target = (StringMemberValue) annotation.getMemberValue("target");
                        classToPatchName = (target != null) ? target.getValue() : "";
                    }
                    if (!"".equals(classToPatchName)) {
                        addPatchClass(classToPatchName, ctClass);
                    }
                }
            } catch (ClassNotFoundException e) {
                // should never happen
                throw new GwtTestPatchException(e);
            }
        }

        private void addPatchClass(String targetName, CtClass patchClass) {
            Set<CtClass> patchClasses = patchClassMap.get(targetName);
            if (patchClasses == null) {
                patchClasses = new HashSet<CtClass>();
                patchClassMap.put(targetName, patchClasses);
            }
            patchClasses.add(patchClass);
            LOGGER.debug("Add patch for class '" + targetName + "' : '" + patchClass.getName() + "'");
        }
    };
    ClassesScanner.getInstance().scanPackages(patchClassVisitor, scanPackages);
    // create all patchers
    patcherFactory = new PatcherFactory(patchClassMap);
}
Also used : StringMemberValue(javassist.bytecode.annotation.StringMemberValue) ClassVisitor(com.googlecode.gwt.test.internal.ClassesScanner.ClassVisitor) Annotation(javassist.bytecode.annotation.Annotation) ClassMemberValue(javassist.bytecode.annotation.ClassMemberValue) GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) CtClass(javassist.CtClass) PatchClass(com.googlecode.gwt.test.patchers.PatchClass)

Example 8 with GwtTestPatchException

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

the class GwtHtmlParser method parseInternal.

private NodeList<Node> parseInternal(String html) {
    if (html == null || html.trim().length() == 0) {
        return JsoUtils.newNodeList(Collections.<Node>emptyList());
    }
    try {
        XMLReader xmlReader = getXMLReader();
        GwtHtmlContentHandler contentHandler = new GwtHtmlContentHandler();
        xmlReader.setContentHandler(contentHandler);
        xmlReader.parse(new InputSource(new StringReader(html)));
        return contentHandler.getParsedNodes();
    } catch (Exception e) {
        throw new GwtTestPatchException("Error while parsing HTML '" + html + "'", e);
    }
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException) GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException)

Example 9 with GwtTestPatchException

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

the class SafeHtmlUtilsPatcher method fromSafeConstant.

@PatchMethod
public static SafeHtml fromSafeConstant(String s) {
    // PatchMethod to avoid gwt-dev dependency.. See SafeHtmlHostedModeUtils
    Class<?> clazz;
    try {
        clazz = Class.forName("com.google.gwt.safehtml.shared.SafeHtmlString");
        Constructor<?> cons = clazz.getDeclaredConstructor(String.class);
        return (SafeHtml) GwtReflectionUtils.instantiateClass(cons, s);
    } catch (Exception e) {
        throw new GwtTestPatchException("Error while instanciate a SafeHtmlString instance", e);
    }
}
Also used : GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) SafeHtml(com.google.gwt.safehtml.shared.SafeHtml) GwtTestPatchException(com.googlecode.gwt.test.exceptions.GwtTestPatchException) PatchMethod(com.googlecode.gwt.test.patchers.PatchMethod)

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