Search in sources :

Example 11 with ErrorCollector

use of org.codehaus.groovy.control.ErrorCollector in project groovy by apache.

the class GroovyDocParser method parseGroovy.

private Map<String, GroovyClassDoc> parseGroovy(String packagePath, String file, String src) throws RuntimeException {
    CompilerConfiguration config = new CompilerConfiguration();
    config.getOptimizationOptions().put(CompilerConfiguration.GROOVYDOC, true);
    CompilationUnit compUnit = new CompilationUnit(config);
    SourceUnit unit = new SourceUnit(file, src, config, null, new ErrorCollector(config));
    compUnit.addSource(unit);
    compUnit.compile(Phases.CONVERSION);
    ModuleNode root = unit.getAST();
    GroovydocVisitor visitor = new GroovydocVisitor(unit, packagePath, links);
    visitor.visitClass(root.getClasses().get(0));
    return visitor.getGroovyClassDocs();
}
Also used : CompilationUnit(org.codehaus.groovy.control.CompilationUnit) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) SourceUnit(org.codehaus.groovy.control.SourceUnit) GroovydocVisitor(org.apache.groovy.antlr.GroovydocVisitor) ModuleNode(org.codehaus.groovy.ast.ModuleNode)

Example 12 with ErrorCollector

use of org.codehaus.groovy.control.ErrorCollector in project groovy by apache.

the class ProxyGeneratorAdapter method adjustSuperClass.

private Class<?> adjustSuperClass(final Class<?> superClass, final Class<?>[] interfaces) {
    boolean isSuperClassAnInterface = superClass.isInterface();
    if (!isSuperClassAnInterface) {
        return superClass;
    }
    Class<?> result = Object.class;
    Set<ClassNode> traits = new LinkedHashSet<>();
    // check if it's a trait
    collectTraits(superClass, traits);
    if (interfaces != null) {
        for (Class<?> anInterface : interfaces) {
            collectTraits(anInterface, traits);
        }
    }
    if (!traits.isEmpty()) {
        String name = superClass.getName() + "$TraitAdapter";
        ClassNode cn = new ClassNode(name, ACC_PUBLIC | ACC_ABSTRACT, ClassHelper.OBJECT_TYPE, traits.toArray(ClassNode.EMPTY_ARRAY), null);
        CompilationUnit cu = new CompilationUnit(loader);
        CompilerConfiguration config = new CompilerConfiguration();
        SourceUnit su = new SourceUnit(name + "wrapper", "", config, loader, new ErrorCollector(config));
        cu.addSource(su);
        cu.compile(Phases.CONVERSION);
        su.getAST().addClass(cn);
        cu.compile(Phases.CLASS_GENERATION);
        List<GroovyClass> classes = cu.getClasses();
        for (GroovyClass groovyClass : classes) {
            if (groovyClass.getName().equals(name)) {
                return loader.defineClass(name, groovyClass.getBytes());
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) CompilationUnit(org.codehaus.groovy.control.CompilationUnit) ClassNode(org.codehaus.groovy.ast.ClassNode) GroovyClass(org.codehaus.groovy.tools.GroovyClass) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) SourceUnit(org.codehaus.groovy.control.SourceUnit) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyObject(groovy.lang.GroovyObject)

Example 13 with ErrorCollector

use of org.codehaus.groovy.control.ErrorCollector in project groovy by apache.

the class TypeCheckingContext method pushErrorCollector.

public ErrorCollector pushErrorCollector() {
    CompilerConfiguration config = Optional.ofNullable(getErrorCollector()).map(ErrorCollector::getConfiguration).orElseGet(() -> getSource().getConfiguration());
    ErrorCollector collector = new ErrorCollector(config);
    pushErrorCollector(collector);
    return collector;
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ErrorCollector(org.codehaus.groovy.control.ErrorCollector)

Example 14 with ErrorCollector

use of org.codehaus.groovy.control.ErrorCollector in project groovy by apache.

the class ExtendedVerifier method visitConstructorOrMethod.

private void visitConstructorOrMethod(MethodNode node, int methodTarget) {
    visitAnnotations(node, methodTarget);
    for (Parameter parameter : node.getParameters()) {
        visitAnnotations(parameter, PARAMETER_TARGET);
        visitTypeAnnotations(parameter.getType());
        extractTypeUseAnnotations(parameter.getAnnotations(), parameter.getType(), PARAMETER_TARGET);
    }
    if (node.getExceptions() != null) {
        for (ClassNode t : node.getExceptions()) {
            visitTypeAnnotations(t);
        }
    }
    if (this.currentClass.isAnnotationDefinition() && !node.isStaticConstructor()) {
        ErrorCollector errorCollector = new ErrorCollector(this.source.getConfiguration());
        AnnotationVisitor visitor = new AnnotationVisitor(this.source, errorCollector);
        visitor.setReportClass(this.currentClass);
        visitor.checkReturnType(node.getReturnType(), node);
        if (node.getParameters().length > 0) {
            addError("Annotation members may not have parameters.", node.getParameters()[0]);
        }
        if (node.getExceptions().length > 0) {
            addError("Annotation members may not have a throws clause.", node.getExceptions()[0]);
        }
        ReturnStatement code = (ReturnStatement) node.getCode();
        if (code != null) {
            visitor.visitExpression(node.getName(), code.getExpression(), node.getReturnType());
            visitor.checkCircularReference(this.currentClass, node.getReturnType(), code.getExpression());
        }
        this.source.getErrorCollector().addCollectorContents(errorCollector);
    }
    Statement code = node.getCode();
    if (code != null) {
        code.visit(this);
    }
}
Also used : ClassNode(org.codehaus.groovy.ast.ClassNode) Statement(org.codehaus.groovy.ast.stmt.Statement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) ReturnStatement(org.codehaus.groovy.ast.stmt.ReturnStatement) Parameter(org.codehaus.groovy.ast.Parameter) ErrorCollector(org.codehaus.groovy.control.ErrorCollector)

Example 15 with ErrorCollector

use of org.codehaus.groovy.control.ErrorCollector in project groovy by apache.

the class ExtendedVerifier method visitAnnotations.

private void visitAnnotations(AnnotatedNode node, List<AnnotationNode> annotations, int target) {
    if (annotations.isEmpty()) {
        return;
    }
    this.currentClass.setAnnotated(true);
    Map<String, List<AnnotationNode>> nonSourceAnnotations = new LinkedHashMap<>();
    for (AnnotationNode unvisited : annotations) {
        AnnotationNode visited;
        {
            ErrorCollector errorCollector = new ErrorCollector(source.getConfiguration());
            AnnotationVisitor visitor = new AnnotationVisitor(source, errorCollector);
            visited = visitor.visit(unvisited);
            source.getErrorCollector().addCollectorContents(errorCollector);
        }
        String name = visited.getClassNode().getName();
        boolean skip = currentClass.isRecord() && skippableRecordAnnotation(node, visited);
        if (!visited.hasSourceRetention()) {
            List<AnnotationNode> seen = nonSourceAnnotations.get(name);
            if (seen == null) {
                seen = new ArrayList<>();
            } else if (!isRepeatable(visited)) {
                addError("Cannot specify duplicate annotation on the same member : " + name, visited);
            }
            seen.add(visited);
            if (!skip) {
                nonSourceAnnotations.put(name, seen);
            }
        }
        // Check if the annotation target is correct, unless it's the target annotating an annotation definition
        // defining on which target elements the annotation applies
        boolean isTargetAnnotation = name.equals("java.lang.annotation.Target");
        if (!isTargetAnnotation && !skip && !visited.isTargetAllowed(target) && !isTypeUseScenario(visited, target)) {
            addError("Annotation @" + name + " is not allowed on element " + AnnotationNode.targetToName(target), visited);
        }
        visitDeprecation(node, visited);
        visitOverride(node, visited);
    }
    processDuplicateAnnotationContainers(node, nonSourceAnnotations);
}
Also used : AnnotationNode(org.codehaus.groovy.ast.AnnotationNode) List(java.util.List) ArrayList(java.util.ArrayList) ErrorCollector(org.codehaus.groovy.control.ErrorCollector) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ErrorCollector (org.codehaus.groovy.control.ErrorCollector)19 ArrayList (java.util.ArrayList)6 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)6 CompilationUnit (org.codehaus.groovy.control.CompilationUnit)5 SourceUnit (org.codehaus.groovy.control.SourceUnit)5 List (java.util.List)4 ClassNode (org.codehaus.groovy.ast.ClassNode)4 GroovyClass (org.codehaus.groovy.tools.GroovyClass)4 GroovyObject (groovy.lang.GroovyObject)3 LinkedHashSet (java.util.LinkedHashSet)3 MultipleCompilationErrorsException (org.codehaus.groovy.control.MultipleCompilationErrorsException)3 SyntaxException (org.codehaus.groovy.syntax.SyntaxException)3 Script (groovy.lang.Script)2 IOException (java.io.IOException)2 LinkedHashMap (java.util.LinkedHashMap)2 ReturnStatement (org.codehaus.groovy.ast.stmt.ReturnStatement)2 Statement (org.codehaus.groovy.ast.stmt.Statement)2 GroovityClassLoader (com.disney.groovity.compile.GroovityClassLoader)1 GroovityCompilerEvent (com.disney.groovity.compile.GroovityCompilerEvent)1 TransformedSource (com.disney.groovity.compile.GroovitySourceTransformer.TransformedSource)1