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();
}
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;
}
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;
}
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);
}
}
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);
}
Aggregations