Java Code Examples for org.codehaus.groovy.ast.ASTNode#visit()

The following examples show how to use org.codehaus.groovy.ast.ASTNode#visit() . 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: BuildGradleParser.java    From synopsys-detect with Apache License 2.0 6 votes vote down vote up
public Optional<DependencyGraph> parse(final InputStream inputStream) {
    final MutableDependencyGraph dependencyGraph = new MutableMapDependencyGraph();

    try {
        final String sourceContents = IOUtils.toString(inputStream, StandardCharsets.UTF_8);
        final AstBuilder astBuilder = new AstBuilder();
        final List<ASTNode> nodes = astBuilder.buildFromString(sourceContents);

        final DependenciesVisitor dependenciesVisitor = new DependenciesVisitor(externalIdFactory);
        for (final ASTNode node : nodes) {
            node.visit(dependenciesVisitor);
        }

        final List<Dependency> dependencies = dependenciesVisitor.getDependencies();
        dependencyGraph.addChildrenToRoot(dependencies);

        return Optional.of(dependencyGraph);
    } catch (final IOException e) {
        logger.error("Could not get the build file contents: " + e.getMessage());
    }

    return Optional.empty();
}
 
Example 2
Source File: GroovyGradleParser.java    From size-analyzer with Apache License 2.0 6 votes vote down vote up
public static GradleContext.Builder parseGradleBuildFile(
    String content,
    int defaultMinSdkVersion,
    @Nullable AndroidPluginVersion defaultAndroidPluginVersion) {
  // We need to have an abstract syntax tree, which is what the conversion phase produces,
  // Anything more will try to semantically understand the groovy code.
  List<ASTNode> astNodes = new AstBuilder().buildFromString(CompilePhase.CONVERSION, content);
  GroovyGradleParser parser =
      new GroovyGradleParser(content, defaultMinSdkVersion, defaultAndroidPluginVersion);

  for (ASTNode node : astNodes) {
    if (node instanceof ClassNode) {
      // class nodes do not implement the visit method, and will throw a runtime exception.
      continue;
    }
    node.visit(parser);
  }
  return parser.getGradleContextBuilder();
}
 
Example 3
Source File: FindAndroidVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public static final boolean visit(File buildGradle) throws FileNotFoundException, IOException {
    AstBuilder builder = new AstBuilder();
    List<ASTNode> nodes = builder.buildFromString(IOUtils.toString(new FileInputStream(buildGradle), "UTF-8"));
    FindAndroidVisitor visitor = new FindAndroidVisitor();
    for (ASTNode node : nodes) {
        node.visit(visitor);
    }
    return visitor.androidProject;
}
 
Example 4
Source File: FindAndroidVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public static final boolean visit(File buildGradle) throws FileNotFoundException, IOException {
    AstBuilder builder = new AstBuilder();
    List<ASTNode> nodes = builder.buildFromString(IOUtils.toString(new FileInputStream(buildGradle), "UTF-8"));
    FindAndroidVisitor visitor = new FindAndroidVisitor();
    for (ASTNode node : nodes) {
        node.visit(visitor);
    }
    return visitor.androidProject;
}
 
Example 5
Source File: FindRepositoriesVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public static final List<Repository> visit(File buildGradle) throws FileNotFoundException, IOException {
    AstBuilder builder = new AstBuilder();
    List<ASTNode> nodes = builder.buildFromString(IOUtils.toString(new FileInputStream(buildGradle), "UTF-8"));
    FindRepositoriesVisitor visitor = new FindRepositoriesVisitor();
    for (ASTNode node : nodes) {
        node.visit(visitor);
    }
    return visitor.repositories;
}
 
Example 6
Source File: AndroidGradleDependenciesVisitor.java    From NBANDROID-V2 with Apache License 2.0 5 votes vote down vote up
public static AndroidGradleDependenciesVisitor parse(File file) throws IOException {
    AstBuilder builder = new AstBuilder();
    AndroidGradleDependenciesVisitor visitor = new AndroidGradleDependenciesVisitor();
    List<ASTNode> nodes = builder.buildFromString(IOUtils.toString(new FileInputStream(file), "UTF-8"));
    for (ASTNode node : nodes) {
        visitor.setRootBlockStatement((BlockStatement) node);
        node.visit(visitor);
    }
    return visitor;
}
 
Example 7
Source File: GithubImporter.java    From scava with Eclipse Public License 2.0 4 votes vote down vote up
public void walkScript(GroovyCodeVisitor visitor, List<ASTNode> nodes) {
	for (ASTNode node : nodes) {
		node.visit(visitor);
	}
}
 
Example 8
Source File: MacroGroovyMethods.java    From groovy with Apache License 2.0 4 votes vote down vote up
public static ListExpression buildSubstitutions(final SourceUnit source, final ASTNode expr) {
    final ListExpression listExpression = new ListExpression();

    ClassCodeVisitorSupport visitor = new ClassCodeVisitorSupport() {
        @Override
        protected SourceUnit getSourceUnit() {
            return null;
        }

        @Override
        public void visitClass(final ClassNode node) {
            super.visitClass(node);
            Iterator<InnerClassNode> it = node.getInnerClasses();
            while (it.hasNext()) {
                InnerClassNode next = it.next();
                visitClass(next);
            }
        }

        @Override
        public void visitMethodCallExpression(MethodCallExpression call) {
            super.visitMethodCallExpression(call);

            if (DOLLAR_VALUE.equals(call.getMethodAsString())) {
                ClosureExpression substitutionClosureExpression = getClosureArgument(source, call);

                if (substitutionClosureExpression == null) {
                    return;
                }

                Statement code = substitutionClosureExpression.getCode();
                if (code instanceof BlockStatement) {
                    ((BlockStatement) code).setVariableScope(null);
                }

                listExpression.addExpression(substitutionClosureExpression);
            }
        }
    };
    if (expr instanceof ClassNode) {
        visitor.visitClass((ClassNode) expr);
    } else {
        expr.visit(visitor);
    }
    return listExpression;
}
 
Example 9
Source File: ProcessVariableRenamer.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
public MultiTextEdit rename(final ASTNode node, final List<ReferenceDiff> diffs) {
    this.diffs = diffs;
    // no need to visit parameters since they have already been changed
    node.visit(this);
    return edits;
}
 
Example 10
Source File: ContractInputCompletionProposalComputer.java    From bonita-studio with GNU General Public License v2.0 4 votes vote down vote up
@Override
public List<ICompletionProposal> computeCompletionProposals(final ContentAssistInvocationContext context,
        final IProgressMonitor monitor) {
    if (!(context instanceof JavaContentAssistInvocationContext)
            || context instanceof JavaContentAssistInvocationContext
                    && !(((JavaContentAssistInvocationContext) context)
                            .getCompilationUnit() instanceof GroovyCompilationUnit)) {
        return Collections.emptyList();
    }
    final List<ContractInput> inputs = getContractInputs(context);
    if (inputs.isEmpty()) {
        return Collections.emptyList();
    }
    final JavaContentAssistInvocationContext javaContext = (JavaContentAssistInvocationContext) context;
    GroovyCompilationUnit compilationUnit = (GroovyCompilationUnit) javaContext.getCompilationUnit();
    if (compilationUnit.getModuleNode() == null) {
        return Collections.emptyList();
    }
    final ContentAssistContext contentAssistContext = createContentAssistContext(
            compilationUnit,
            context.getInvocationOffset(), context.getDocument());
    if (contentAssistContext == null) {
        return Collections.emptyList();
    }
    CharSequence computeIdentifierPrefix = "";
    try {
        computeIdentifierPrefix = javaContext.computeIdentifierPrefix();
    } catch (final BadLocationException e) {
        BonitaStudioLog.error("Failed to compute identifier prefix in ContractConstraint expression editor", e,
                ContractPlugin.PLUGIN_ID);
        return Collections.emptyList();
    }
    final CodeVisitorSupportContext codeVisitorSupportContext = new CodeVisitorSupportContext(
            computeIdentifierPrefix.toString(),
            (JavaContentAssistInvocationContext) context,
            contentAssistContext,
            getProjectClassloader(compilationUnit),
            new GroovyCompletionProposalComputer(),
            createMethodProposalCreator(),
            compilationUnit.getModuleNode());
    final ContractInputProposalsCodeVisitorSupport codeVistor = new ContractInputProposalsCodeVisitorSupport(inputs,
            codeVisitorSupportContext,
            monitor);
    final ASTNode completionNode = contentAssistContext.getPerceivedCompletionNode();
    if (completionNode != null) {
        completionNode.visit(codeVistor);
    }
    final List<ICompletionProposal> proposals = codeVistor.getProposals();
    if (proposals == null || proposals.isEmpty()) {
        return super.computeCompletionProposals(context, monitor);
    }
    return proposals;
}