Refine search
private ClassNode getSourceUnitClass(VariableExpression ve) { List<ClassNode> classes = source.getAST().getClasses(); for (ClassNode classNode : classes) { if (classNode.getNameWithoutPackage().equals(ve.getName())) return classNode; } return null; }
public void error(String msg, Object... args) { sourceUnit.getErrorCollector().addErrorAndContinue( new SimpleMessage(String.format(msg, args), sourceUnit)); }
/** * @return the underlying character stream description */ public String getDescription() { if( context != null ) { return context.getName(); } else { return this.description; } }
/** * Adds a SourceUnit to the unit. */ public SourceUnit addSource(SourceUnit source) { String name = source.getName(); source.setClassLoader(this.classLoader); for (SourceUnit su : queuedSources) { if (name.equals(su.getName())) return su; } queuedSources.add(source); return source; }
@Override protected GroovyCodeVisitor getTransformer(ASTNode[] nodes, SourceUnit sourceUnit) { // todo : are there other import types that can be specified? return new AstBuilderInvocationTrap( sourceUnit.getAST().getImports(), sourceUnit.getAST().getStarImports(), sourceUnit.getSource(), sourceUnit ); }
/** * Attempts to parse the specified code with the specified tolerance. * Updates the <code>parser</code> and <code>error</code> members * appropriately. Returns true if the text parsed, false otherwise. * The attempts to identify and suppress errors resulting from the * unfinished source text. */ private boolean parse(final String code, final int tolerance) { assert code != null; boolean parsed = false; parser = null; error = null; // Create the parser and attempt to parse the text as a top-level statement. try { parser = SourceUnit.create("groovysh-script", code, tolerance); parser.parse(); parsed = true; } // We report errors other than unexpected EOF to the user. catch (CompilationFailedException e) { if (parser.getErrorCollector().getErrorCount() > 1 || !parser.failedWithUnexpectedEOF()) { error = e; } } catch (Exception e) { error = e; } return parsed; }
grapesAliases = new HashSet<String>(); grabResolverAliases = new HashSet<String>(); for (ImportNode im : mn.getImports()) { String alias = im.getAlias(); String className = im.getClassName(); List<Map<String,Object>> grabExcludeMaps = new ArrayList<Map<String,Object>>(); for (ClassNode classNode : sourceUnit.getAST().getClasses()) { grabAnnotations = new ArrayList<AnnotationNode>(); grabExcludeAnnotations = new ArrayList<AnnotationNode>(); if (!(getSourceUnit().getSource() instanceof StringReaderSource)) { sourceURI = getSourceUnit().getSource().getURI(); basicArgs.put("classLoader", loader != null ? loader : sourceUnit.getClassLoader()); if (!grabExcludeMaps.isEmpty()) basicArgs.put("excludes", grabExcludeMaps); if (autoDownload != null) basicArgs.put(AUTO_DOWNLOAD_SETTING, autoDownload); source.addException(re);
public ModuleNode buildAST(SourceUnit sourceUnit, ClassLoader classLoader, Reduction cst) throws ParserException { setClassLoader(classLoader); makeModule(); try { convertGroovy(ast); if (output.getStatementBlock().isEmpty() && output.getMethods().isEmpty() && output.getClasses().isEmpty()) { output.addStatement(ReturnStatement.RETURN_NULL_OR_VOID); } // set the script source position ClassNode scriptClassNode = output.getScriptClassDummy(); if (scriptClassNode != null) { List<Statement> statements = output.getStatementBlock().getStatements(); if (!statements.isEmpty()) { Statement firstStatement = statements.get(0); Statement lastStatement = statements.get(statements.size() - 1); scriptClassNode.setSourcePosition(firstStatement); scriptClassNode.setLastColumnNumber(lastStatement.getLastColumnNumber()); scriptClassNode.setLastLineNumber(lastStatement.getLastLineNumber()); } } } catch (ASTRuntimeException e) { throw new ASTParserException(e.getMessage() + ". File: " + sourceUnit.getName(), e); } return output; }
public ExpressionInfo build() { try { SourceUnit unit = SourceUnit.create("Spec expression", adjustedText); unit.parse(); unit.completePhase(); unit.convert(); BlockStatement blockStat = unit.getAST().getStatementBlock(); Assert.that(blockStat != null && blockStat.getStatements().size() == 1); Statement stat = blockStat.getStatements().get(0);
SourceUnit nodeSource = node.getModule().getContext(); SourceUnit storedSource = stored.getModule().getContext(); String txt = "Invalid duplicate class definition of class " + node.getName() + " : "; if (nodeSource == storedSource) { txt += "The source " + nodeSource.getName() + " contains at least two definitions of the class " + node.getName() + ".\n"; if (node.isScriptBody() || stored.isScriptBody()) { txt += "One of the classes is an explicit generated class using the class statement, the other is a class generated from" + txt += "The sources " + nodeSource.getName() + " and " + storedSource.getName() + " each contain a class with the name " + node.getName() + ".\n"; nodeSource.getErrorCollector().addErrorAndContinue( new SyntaxErrorMessage(new SyntaxException(txt, node.getLineNumber(), node.getColumnNumber(), node.getLastLineNumber(), node.getLastColumnNumber()), nodeSource) );
@Override public void call(final SourceUnit source, final GeneratorContext context, final ClassNode classNode) throws CompilationFailedException { final ModuleNode ast = source.getAST(); for (Import anImport : imports) { switch (anImport.type) { case regular: ast.addImport(anImport.alias, anImport.classNode); break; case staticImport: ast.addStaticImport(anImport.classNode, anImport.field, anImport.alias); break; case staticStar: ast.addStaticStarImport(anImport.alias, anImport.classNode); break; case star: ast.addStarImport(anImport.star); break; } } }
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); @SuppressWarnings("unchecked")
private String flattenErrorMessage() { StringWriter stringWriter = new StringWriter(); PrintWriter writer = new PrintWriter(stringWriter, true); for (int i = source.getErrorCollector().getErrorCount() - 1; i >= 0; i--) { source.getErrorCollector().getError(i).write(writer); } writer.close(); return stringWriter.toString(); } }
private List<String> getTransformClassNames(AnnotationNode annotation, Annotation transformClassAnnotation) { List<String> result = new ArrayList<String>(); try { Method valueMethod = transformClassAnnotation.getClass().getMethod("value"); String[] names = (String[]) valueMethod.invoke(transformClassAnnotation); result.addAll(Arrays.asList(names)); Method classesMethod = transformClassAnnotation.getClass().getMethod("classes"); Class[] classes = (Class[]) classesMethod.invoke(transformClassAnnotation); for (Class klass : classes) { result.add(klass.getName()); } if (names.length > 0 && classes.length > 0) { source.getErrorCollector().addError(new SimpleMessage("@GroovyASTTransformationClass in " + annotation.getClassNode().getName() + " should specify transforms only by class names or by classes and not by both", source)); } } catch (Exception e) { source.addException(e); } return result; } }
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) { for (Object statement : source.getAST().getStatementBlock().getStatements()) ((ExpressionStatement) statement).visit(new CustomCodeVisitorSupport()); } }
@Override public void call(SourceUnit source) throws CompilationFailedException { module = source.getAST(); throw new AstSuccessfullyCaptured(); } }, compilePhase.getPhaseNumber());
/** * A convenience routine to create a standalone SourceUnit on a String * with defaults for almost everything that is configurable. */ public static SourceUnit create(String name, String source, int tolerance) { CompilerConfiguration configuration = new CompilerConfiguration(); configuration.setTolerance(tolerance); return new SourceUnit(name, source, configuration, null, new ErrorCollector(configuration)); }