Search in sources :

Example 1 with WarningMessage

use of org.codehaus.groovy.control.messages.WarningMessage in project groovy-core by groovy.

the class ASTTransformationVisitor method doAddGlobalTransforms.

private static void doAddGlobalTransforms(ASTTransformationsContext context, boolean isFirstScan) {
    final CompilationUnit compilationUnit = context.getCompilationUnit();
    GroovyClassLoader transformLoader = compilationUnit.getTransformLoader();
    Map<String, URL> transformNames = new LinkedHashMap<String, URL>();
    try {
        Enumeration<URL> globalServices = transformLoader.getResources("META-INF/services/org.codehaus.groovy.transform.ASTTransformation");
        while (globalServices.hasMoreElements()) {
            URL service = globalServices.nextElement();
            String className;
            BufferedReader svcIn = null;
            try {
                svcIn = new BufferedReader(new InputStreamReader(service.openStream(), "UTF-8"));
                try {
                    className = svcIn.readLine();
                } catch (IOException ioe) {
                    compilationUnit.getErrorCollector().addError(new SimpleMessage("IOException reading the service definition at " + service.toExternalForm() + " because of exception " + ioe.toString(), null));
                    continue;
                }
                Set<String> disabledGlobalTransforms = compilationUnit.getConfiguration().getDisabledGlobalASTTransformations();
                if (disabledGlobalTransforms == null)
                    disabledGlobalTransforms = Collections.emptySet();
                while (className != null) {
                    if (!className.startsWith("#") && className.length() > 0) {
                        if (!disabledGlobalTransforms.contains(className)) {
                            if (transformNames.containsKey(className)) {
                                if (!service.equals(transformNames.get(className))) {
                                    compilationUnit.getErrorCollector().addWarning(WarningMessage.POSSIBLE_ERRORS, "The global transform for class " + className + " is defined in both " + transformNames.get(className).toExternalForm() + " and " + service.toExternalForm() + " - the former definition will be used and the latter ignored.", null, null);
                                }
                            } else {
                                transformNames.put(className, service);
                            }
                        }
                    }
                    try {
                        className = svcIn.readLine();
                    } catch (IOException ioe) {
                        compilationUnit.getErrorCollector().addError(new SimpleMessage("IOException reading the service definition at " + service.toExternalForm() + " because of exception " + ioe.toString(), null));
                        // noinspection UnnecessaryContinue
                        continue;
                    }
                }
            } finally {
                if (svcIn != null)
                    svcIn.close();
            }
        }
    } catch (IOException e) {
        // FIXME the warning message will NPE with what I have :(
        compilationUnit.getErrorCollector().addError(new SimpleMessage("IO Exception attempting to load global transforms:" + e.getMessage(), null));
    }
    try {
        // test for 1.5 JVM
        Class.forName("java.lang.annotation.Annotation");
    } catch (Exception e) {
        // we failed, notify the user
        StringBuilder sb = new StringBuilder();
        sb.append("Global ASTTransformations are not enabled in retro builds of groovy.\n");
        sb.append("The following transformations will be ignored:");
        for (Map.Entry<String, URL> entry : transformNames.entrySet()) {
            sb.append('\t');
            sb.append(entry.getKey());
            sb.append('\n');
        }
        compilationUnit.getErrorCollector().addWarning(new WarningMessage(WarningMessage.POSSIBLE_ERRORS, sb.toString(), null, null));
        return;
    }
    // can be added for only for new transforms that have come in
    if (isFirstScan) {
        for (Map.Entry<String, URL> entry : transformNames.entrySet()) {
            context.getGlobalTransformNames().add(entry.getKey());
        }
        addPhaseOperationsForGlobalTransforms(context.getCompilationUnit(), transformNames, isFirstScan);
    } else {
        Iterator<Map.Entry<String, URL>> it = transformNames.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String, URL> entry = it.next();
            if (!context.getGlobalTransformNames().add(entry.getKey())) {
                // phase operations for this transform class have already been added before, so remove from current scan cycle
                it.remove();
            }
        }
        addPhaseOperationsForGlobalTransforms(context.getCompilationUnit(), transformNames, isFirstScan);
    }
}
Also used : WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) InputStreamReader(java.io.InputStreamReader) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) IOException(java.io.IOException) URL(java.net.URL) IOException(java.io.IOException) GroovyClassLoader(groovy.lang.GroovyClassLoader) BufferedReader(java.io.BufferedReader)

Example 2 with WarningMessage

use of org.codehaus.groovy.control.messages.WarningMessage in project groovy by apache.

the class ASTTransformationVisitor method addPhaseOperationsForGlobalTransforms.

private static void addPhaseOperationsForGlobalTransforms(CompilationUnit compilationUnit, Map<String, URL> transformNames, boolean isFirstScan) {
    GroovyClassLoader transformLoader = compilationUnit.getTransformLoader();
    for (Map.Entry<String, URL> entry : transformNames.entrySet()) {
        try {
            Class<?> gTransClass = transformLoader.loadClass(entry.getKey(), false, true, false);
            GroovyASTTransformation transformAnnotation = gTransClass.getAnnotation(GroovyASTTransformation.class);
            if (transformAnnotation == null) {
                compilationUnit.getErrorCollector().addWarning(new WarningMessage(WarningMessage.POSSIBLE_ERRORS, "Transform Class " + entry.getKey() + " is specified as a global transform in " + entry.getValue().toExternalForm() + " but it is not annotated by " + GroovyASTTransformation.class.getName() + " the global transform associated with it may fail and cause the compilation to fail.", null, null));
                continue;
            }
            if (ASTTransformation.class.isAssignableFrom(gTransClass)) {
                ASTTransformation instance = (ASTTransformation) gTransClass.getDeclaredConstructor().newInstance();
                if (instance instanceof CompilationUnitAware) {
                    ((CompilationUnitAware) instance).setCompilationUnit(compilationUnit);
                }
                CompilationUnit.ISourceUnitOperation suOp = source -> instance.visit(new ASTNode[] { source.getAST() }, source);
                if (isFirstScan) {
                    compilationUnit.addPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber());
                } else {
                    compilationUnit.addNewPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber());
                }
            } else {
                compilationUnit.getErrorCollector().addError(new SimpleMessage("Transform Class " + entry.getKey() + " specified at " + entry.getValue().toExternalForm() + " is not an ASTTransformation.", null));
            }
        } catch (Exception e) {
            Throwable effectiveException = e instanceof InvocationTargetException ? e.getCause() : e;
            compilationUnit.getErrorCollector().addError(new SimpleMessage("Could not instantiate global transform class " + entry.getKey() + " specified at " + entry.getValue().toExternalForm() + "  because of exception " + effectiveException.toString(), null));
        }
    }
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) CompilePhase(org.codehaus.groovy.control.CompilePhase) Enumeration(java.util.Enumeration) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) ASTTransformationsContext(org.codehaus.groovy.control.ASTTransformationsContext) URLStreams(org.codehaus.groovy.util.URLStreams) HashMap(java.util.HashMap) AnnotatedNode(org.codehaus.groovy.ast.AnnotatedNode) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Tuple2(groovy.lang.Tuple2) Tuple3(groovy.lang.Tuple3) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) Map(java.util.Map) ClassCodeVisitorSupport(org.codehaus.groovy.ast.ClassCodeVisitorSupport) Expression(org.codehaus.groovy.ast.expr.Expression) LinkedList(java.util.LinkedList) PropertyNode(org.codehaus.groovy.ast.PropertyNode) GeneratorContext(org.codehaus.groovy.classgen.GeneratorContext) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) CompilationUnitAware(groovy.transform.CompilationUnitAware) SourceUnit(org.codehaus.groovy.control.SourceUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) Set(java.util.Set) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) StandardCharsets(java.nio.charset.StandardCharsets) InvocationTargetException(java.lang.reflect.InvocationTargetException) ASTNode(org.codehaus.groovy.ast.ASTNode) Phases(org.codehaus.groovy.control.Phases) List(java.util.List) GroovyClassVisitor(org.codehaus.groovy.ast.GroovyClassVisitor) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) Tuple(groovy.lang.Tuple) BufferedReader(java.io.BufferedReader) Comparator(java.util.Comparator) GroovyClassLoader(groovy.lang.GroovyClassLoader) Collections(java.util.Collections) WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) CompilationUnitAware(groovy.transform.CompilationUnitAware) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) URL(java.net.URL) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GroovyClassLoader(groovy.lang.GroovyClassLoader) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with WarningMessage

use of org.codehaus.groovy.control.messages.WarningMessage in project groovy-core by groovy.

the class ASTTransformationVisitor method addPhaseOperationsForGlobalTransforms.

private static void addPhaseOperationsForGlobalTransforms(CompilationUnit compilationUnit, Map<String, URL> transformNames, boolean isFirstScan) {
    GroovyClassLoader transformLoader = compilationUnit.getTransformLoader();
    for (Map.Entry<String, URL> entry : transformNames.entrySet()) {
        try {
            Class gTransClass = transformLoader.loadClass(entry.getKey(), false, true, false);
            // no inspection unchecked
            GroovyASTTransformation transformAnnotation = (GroovyASTTransformation) gTransClass.getAnnotation(GroovyASTTransformation.class);
            if (transformAnnotation == null) {
                compilationUnit.getErrorCollector().addWarning(new WarningMessage(WarningMessage.POSSIBLE_ERRORS, "Transform Class " + entry.getKey() + " is specified as a global transform in " + entry.getValue().toExternalForm() + " but it is not annotated by " + GroovyASTTransformation.class.getName() + " the global transform associated with it may fail and cause the compilation to fail.", null, null));
                continue;
            }
            if (ASTTransformation.class.isAssignableFrom(gTransClass)) {
                final ASTTransformation instance = (ASTTransformation) gTransClass.newInstance();
                if (instance instanceof CompilationUnitAware) {
                    ((CompilationUnitAware) instance).setCompilationUnit(compilationUnit);
                }
                CompilationUnit.SourceUnitOperation suOp = new CompilationUnit.SourceUnitOperation() {

                    public void call(SourceUnit source) throws CompilationFailedException {
                        instance.visit(new ASTNode[] { source.getAST() }, source);
                    }
                };
                if (isFirstScan) {
                    compilationUnit.addPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber());
                } else {
                    compilationUnit.addNewPhaseOperation(suOp, transformAnnotation.phase().getPhaseNumber());
                }
            } else {
                compilationUnit.getErrorCollector().addError(new SimpleMessage("Transform Class " + entry.getKey() + " specified at " + entry.getValue().toExternalForm() + " is not an ASTTransformation.", null));
            }
        } catch (Exception e) {
            compilationUnit.getErrorCollector().addError(new SimpleMessage("Could not instantiate global transform class " + entry.getKey() + " specified at " + entry.getValue().toExternalForm() + "  because of exception " + e.toString(), null));
        }
    }
}
Also used : WarningMessage(org.codehaus.groovy.control.messages.WarningMessage) CompilationUnitAware(groovy.transform.CompilationUnitAware) SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) URL(java.net.URL) IOException(java.io.IOException) GroovyClassLoader(groovy.lang.GroovyClassLoader)

Aggregations

GroovyClassLoader (groovy.lang.GroovyClassLoader)3 IOException (java.io.IOException)3 URL (java.net.URL)3 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)3 WarningMessage (org.codehaus.groovy.control.messages.WarningMessage)3 CompilationUnitAware (groovy.transform.CompilationUnitAware)2 BufferedReader (java.io.BufferedReader)2 InputStreamReader (java.io.InputStreamReader)2 Tuple (groovy.lang.Tuple)1 Tuple2 (groovy.lang.Tuple2)1 Tuple3 (groovy.lang.Tuple3)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URISyntaxException (java.net.URISyntaxException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 Enumeration (java.util.Enumeration)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1