Java Code Examples for org.eclipse.jdt.core.dom.Block#accept()

The following examples show how to use org.eclipse.jdt.core.dom.Block#accept() . 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: SORecommender.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
private HashMap<String, HashSet<String>> parseKStatements(String snippet) throws IOException {
	HashMap<String, HashSet<String>> tokens = new HashMap<String, HashSet<String>>();
	ASTParser parser = ASTParser.newParser(AST.JLS8);
	parser.setResolveBindings(true);
	parser.setKind(ASTParser.K_STATEMENTS);
	parser.setBindingsRecovery(true);
	Map<String, String> options = JavaCore.getOptions();
	parser.setCompilerOptions(options);
	parser.setUnitName("test");
	String src = snippet;
	String[] sources = {};
	String[] classpath = { "/usr/lib/jvm/java-8-openjdk-amd64" };
	parser.setEnvironment(classpath, sources, new String[] {}, true);
	parser.setSource(src.toCharArray());
	try {
		Block block = (Block) parser.createAST(null);
		MyASTVisitor myVisitor = new MyASTVisitor();
		block.accept(myVisitor);
		tokens = myVisitor.getTokens();
	} catch (Exception exc) {
		logger.error("JDT parsing error");
	}
	return tokens;
}
 
Example 2
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean visit(TryStatement node) {
	if (traverseNode(node)) {
		fFlowContext.pushExcptions(node);
		for (Iterator<VariableDeclarationExpression> iterator = node.resources().iterator(); iterator.hasNext();) {
			iterator.next().accept(this);
		}
		node.getBody().accept(this);
		fFlowContext.popExceptions();
		List<CatchClause> catchClauses = node.catchClauses();
		for (Iterator<CatchClause> iter = catchClauses.iterator(); iter.hasNext();) {
			iter.next().accept(this);
		}
		Block finallyBlock = node.getFinally();
		if (finallyBlock != null) {
			finallyBlock.accept(this);
		}
	}
	return false;
}
 
Example 3
Source File: CreateDoPrivilegedBlockResolution.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
protected void updateLocalVariableDeclarations(final ASTRewrite rewrite, final Set<String> variables, Block block) {
    Assert.isNotNull(rewrite);
    Assert.isNotNull(block);
    Assert.isNotNull(variables);

    final AST ast = rewrite.getAST();
    block.accept(new ASTVisitor() {

        @Override
        public boolean visit(VariableDeclarationFragment fragment) {
            if (variables.contains(fragment.getName().getFullyQualifiedName())) {
                ASTNode parent = fragment.getParent();
                if (parent instanceof VariableDeclarationStatement) {
                    ListRewrite listRewrite = rewrite
                            .getListRewrite(parent, VariableDeclarationStatement.MODIFIERS2_PROPERTY);
                    listRewrite.insertLast(ast.newModifier(ModifierKeyword.FINAL_KEYWORD), null);
                }
            }
            return true;
        }

    });

}
 
Example 4
Source File: SourceAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public void initialize() {
	Block body= fDeclaration.getBody();
	// first collect the static imports. This is necessary to not mark
	// static imported fields and methods as implicit visible.
	fTypesToImport= new ArrayList<SimpleName>();
	fStaticsToImport= new ArrayList<SimpleName>();
	ImportReferencesCollector.collect(body, fTypeRoot.getJavaProject(), null, fTypesToImport, fStaticsToImport);

	// Now collect implicit references and name references
	body.accept(new UpdateCollector());

	int numberOfLocals= LocalVariableIndex.perform(fDeclaration);
	FlowContext context= new FlowContext(0, numberOfLocals + 1);
	context.setConsiderAccessMode(true);
	context.setComputeMode(FlowContext.MERGE);
	InOutFlowAnalyzer flowAnalyzer= new InOutFlowAnalyzer(context);
	FlowInfo info= flowAnalyzer.perform(getStatements());

	for (Iterator<SingleVariableDeclaration> iter= fDeclaration.parameters().iterator(); iter.hasNext();) {
		SingleVariableDeclaration element= iter.next();
		IVariableBinding binding= element.resolveBinding();
		ParameterData data= (ParameterData)element.getProperty(ParameterData.PROPERTY);
		data.setAccessMode(info.getAccessMode(context, binding));
	}
}
 
Example 5
Source File: FlowAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(TryStatement node) {
	if (traverseNode(node)) {
		fFlowContext.pushExcptions(node);
		node.getBody().accept(this);
		fFlowContext.popExceptions();
		List<CatchClause> catchClauses= node.catchClauses();
		for (Iterator<CatchClause> iter= catchClauses.iterator(); iter.hasNext();) {
			iter.next().accept(this);
		}
		Block finallyBlock= node.getFinally();
		if (finallyBlock != null) {
			finallyBlock.accept(this);
		}
	}
	return false;
}
 
Example 6
Source File: IntroduceParameterObjectProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void updateBody(MethodDeclaration methodDeclaration, final CompilationUnitRewrite cuRewrite, RefactoringStatus result) throws CoreException {
	// ensure that the parameterObject is imported
	fParameterObjectFactory.createType(fCreateAsTopLevel, cuRewrite, methodDeclaration.getStartPosition());
	if (cuRewrite.getCu().equals(getCompilationUnit()) && !fParameterClassCreated) {
		createParameterClass(methodDeclaration, cuRewrite);
		fParameterClassCreated= true;
	}
	Block body= methodDeclaration.getBody();
	final List<SingleVariableDeclaration> parameters= methodDeclaration.parameters();
	if (body != null) { // abstract methods don't have bodies
		final ASTRewrite rewriter= cuRewrite.getASTRewrite();
		ListRewrite bodyStatements= rewriter.getListRewrite(body, Block.STATEMENTS_PROPERTY);
		List<ParameterInfo> managedParams= getParameterInfos();
		for (Iterator<ParameterInfo> iter= managedParams.iterator(); iter.hasNext();) {
			final ParameterInfo pi= iter.next();
			if (isValidField(pi)) {
				if (isReadOnly(pi, body, parameters, null)) {
					body.accept(new ASTVisitor(false) {

						@Override
						public boolean visit(SimpleName node) {
							updateSimpleName(rewriter, pi, node, parameters, cuRewrite.getCu().getJavaProject());
							return false;
						}

					});
					pi.setInlined(true);
				} else {
					ExpressionStatement initializer= fParameterObjectFactory.createInitializer(pi, getParameterName(), cuRewrite);
					bodyStatements.insertFirst(initializer, null);
				}
			}
		}
	}


}
 
Example 7
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(MethodDeclaration node) {
	if (isInside(node)) {
		Block body= node.getBody();
		if (body != null) {
			body.accept(this);
		}
		visitBackwards(node.parameters());
		visitBackwards(node.typeParameters());
	}
	return false;
}