Java Code Examples for org.codehaus.groovy.control.SourceUnit#addError()
The following examples show how to use
org.codehaus.groovy.control.SourceUnit#addError() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MacroGroovyMethods.java From groovy with Apache License 2.0 | 6 votes |
protected static TupleExpression getMacroArguments(SourceUnit source, MethodCallExpression call) { Expression macroCallArguments = call.getArguments(); if (macroCallArguments == null) { source.addError(new SyntaxException("Call should have arguments" + '\n', call)); return null; } if (!(macroCallArguments instanceof TupleExpression)) { source.addError(new SyntaxException("Call should have TupleExpression as arguments" + '\n', macroCallArguments)); return null; } TupleExpression tupleArguments = (TupleExpression) macroCallArguments; if (tupleArguments.getExpressions() == null) { source.addError(new SyntaxException("Call arguments should have expressions" + '\n', tupleArguments)); return null; } return tupleArguments; }
Example 2
Source File: MacroGroovyMethods.java From groovy with Apache License 2.0 | 6 votes |
protected static ClosureExpression getClosureArgument(SourceUnit source, MethodCallExpression call) { TupleExpression tupleArguments = getMacroArguments(source, call); int size = tupleArguments == null ? -1 : tupleArguments.getExpressions().size(); if (size < 1) { source.addError(new SyntaxException("Call arguments should have at least one argument" + '\n', tupleArguments)); return null; } Expression result = tupleArguments.getExpression(size - 1); if (!(result instanceof ClosureExpression)) { source.addError(new SyntaxException("Last call argument should be a closure" + '\n', result)); return null; } return (ClosureExpression) result; }
Example 3
Source File: GenericsUtils.java From groovy with Apache License 2.0 | 6 votes |
public static ClassNode[] parseClassNodesFromString(final String option, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage) { try { ModuleNode moduleNode = ParserPlugin.buildAST("Dummy<" + option + "> dummy;", compilationUnit.getConfiguration(), compilationUnit.getClassLoader(), null); DeclarationExpression dummyDeclaration = (DeclarationExpression) ((ExpressionStatement) moduleNode.getStatementBlock().getStatements().get(0)).getExpression(); // the returned node is DummyNode<Param1, Param2, Param3, ...) ClassNode dummyNode = dummyDeclaration.getLeftExpression().getType(); GenericsType[] dummyNodeGenericsTypes = dummyNode.getGenericsTypes(); if (dummyNodeGenericsTypes == null) { return null; } ClassNode[] signature = new ClassNode[dummyNodeGenericsTypes.length]; for (int i = 0, n = dummyNodeGenericsTypes.length; i < n; i += 1) { final GenericsType genericsType = dummyNodeGenericsTypes[i]; signature[i] = resolveClassNode(sourceUnit, compilationUnit, mn, usage, genericsType.getType()); } return signature; } catch (Exception | LinkageError e) { sourceUnit.addError(new IncorrectTypeHintException(mn, e, usage.getLineNumber(), usage.getColumnNumber())); } return null; }
Example 4
Source File: GenericsUtils.java From groovy with Apache License 2.0 | 6 votes |
private static ClassNode resolveClassNode(final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage, final ClassNode parsedNode) { ClassNode dummyClass = new ClassNode("dummy", 0, ClassHelper.OBJECT_TYPE); dummyClass.setModule(new ModuleNode(sourceUnit)); dummyClass.setGenericsTypes(mn.getDeclaringClass().getGenericsTypes()); MethodNode dummyMN = new MethodNode( "dummy", 0, parsedNode, Parameter.EMPTY_ARRAY, ClassNode.EMPTY_ARRAY, EmptyStatement.INSTANCE ); dummyMN.setGenericsTypes(mn.getGenericsTypes()); dummyClass.addMethod(dummyMN); ResolveVisitor visitor = new ResolveVisitor(compilationUnit) { @Override public void addError(final String msg, final ASTNode expr) { sourceUnit.addError(new IncorrectTypeHintException(mn, msg, usage.getLineNumber(), usage.getColumnNumber())); } }; visitor.startResolving(dummyClass, sourceUnit); return dummyMN.getReturnType(); }
Example 5
Source File: TraitComposer.java From groovy with Apache License 2.0 | 5 votes |
private static void checkTraitAllowed(final ClassNode bottomTrait, final SourceUnit unit) { ClassNode superClass = bottomTrait.getSuperClass(); if (superClass==null || ClassHelper.OBJECT_TYPE.equals(superClass)) return; if (!Traits.isTrait(superClass)) { unit.addError(new SyntaxException("A trait can only inherit from another trait", superClass.getLineNumber(), superClass.getColumnNumber())); } }
Example 6
Source File: StaticTypesTransformation.java From groovy with Apache License 2.0 | 5 votes |
public void visit(ASTNode[] nodes, SourceUnit source) { AnnotationNode annotationInformation = (AnnotationNode) nodes[0]; Map<String,Expression> members = annotationInformation.getMembers(); Expression extensions = members.get("extensions"); AnnotatedNode node = (AnnotatedNode) nodes[1]; StaticTypeCheckingVisitor visitor = null; if (node instanceof ClassNode) { ClassNode classNode = (ClassNode) node; visitor = newVisitor(source, classNode); visitor.setCompilationUnit(compilationUnit); addTypeCheckingExtensions(visitor, extensions); visitor.initialize(); visitor.visitClass(classNode); } else if (node instanceof MethodNode) { MethodNode methodNode = (MethodNode) node; visitor = newVisitor(source, methodNode.getDeclaringClass()); visitor.setCompilationUnit(compilationUnit); addTypeCheckingExtensions(visitor, extensions); visitor.setMethodsToBeVisited(Collections.singleton(methodNode)); visitor.initialize(); visitor.visitMethod(methodNode); } else { source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber())); } if (visitor != null) { visitor.performSecondPass(); } }
Example 7
Source File: MapEntryOrKeyValue.java From groovy with Apache License 2.0 | 5 votes |
public List<ClassNode[]> getClosureSignatures(final MethodNode node, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final String[] options, final ASTNode usage) { Options opt; try { opt = Options.parse(node, usage, options); } catch (IncorrectTypeHintException e) { sourceUnit.addError(e); return Collections.emptyList(); } GenericsType[] genericsTypes = node.getParameters()[opt.parameterIndex].getOriginType().getGenericsTypes(); if (genericsTypes==null) { // would happen if you have a raw Map type for example genericsTypes = new GenericsType[] { new GenericsType(ClassHelper.OBJECT_TYPE), new GenericsType(ClassHelper.OBJECT_TYPE) }; } ClassNode[] firstSig; ClassNode[] secondSig; ClassNode mapEntry = MAPENTRY_TYPE.getPlainNodeReference(); mapEntry.setGenericsTypes(genericsTypes); if (opt.generateIndex) { firstSig = new ClassNode[] {genericsTypes[0].getType(), genericsTypes[1].getType(), ClassHelper.int_TYPE}; secondSig = new ClassNode[] {mapEntry, ClassHelper.int_TYPE}; } else { firstSig = new ClassNode[] {genericsTypes[0].getType(), genericsTypes[1].getType()}; secondSig = new ClassNode[] {mapEntry}; } return Arrays.asList(firstSig, secondSig); }
Example 8
Source File: StaticCompileTransformation.java From groovy with Apache License 2.0 | 4 votes |
@Override public void visit(final ASTNode[] nodes, final SourceUnit source) { AnnotationNode annotationInformation = (AnnotationNode) nodes[0]; AnnotatedNode node = (AnnotatedNode) nodes[1]; StaticTypeCheckingVisitor visitor = null; Map<String,Expression> members = annotationInformation.getMembers(); Expression extensions = members.get("extensions"); if (node instanceof ClassNode) { ClassNode classNode = (ClassNode) node; visitor = newVisitor(source, classNode); visitor.setCompilationUnit(compilationUnit); addTypeCheckingExtensions(visitor, extensions); classNode.putNodeMetaData(WriterControllerFactory.class, factory); node.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node)); visitor.initialize(); visitor.visitClass(classNode); } else if (node instanceof MethodNode) { MethodNode methodNode = (MethodNode) node; ClassNode declaringClass = methodNode.getDeclaringClass(); visitor = newVisitor(source, declaringClass); visitor.setCompilationUnit(compilationUnit); addTypeCheckingExtensions(visitor, extensions); methodNode.putNodeMetaData(STATIC_COMPILE_NODE, !visitor.isSkipMode(node)); if (declaringClass.getNodeMetaData(WriterControllerFactory.class) == null) { declaringClass.putNodeMetaData(WriterControllerFactory.class, factory); } visitor.setMethodsToBeVisited(Collections.singleton(methodNode)); visitor.initialize(); visitor.visitMethod(methodNode); } else { source.addError(new SyntaxException(STATIC_ERROR_PREFIX + "Unimplemented node type", node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber())); } if (visitor != null) { visitor.performSecondPass(); } StaticCompilationTransformer transformer = new StaticCompilationTransformer(source, visitor); if (node instanceof ClassNode) { transformer.visitClass((ClassNode) node); } else if (node instanceof MethodNode) { transformer.visitMethod((MethodNode) node); } }