Java Code Examples for org.eclipse.jdt.core.dom.AnonymousClassDeclaration#bodyDeclarations()

The following examples show how to use org.eclipse.jdt.core.dom.AnonymousClassDeclaration#bodyDeclarations() . 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: UMLModelASTReader.java    From RefactoringMiner with MIT License 6 votes vote down vote up
private UMLAnonymousClass processAnonymousClassDeclaration(CompilationUnit cu, AnonymousClassDeclaration anonymous, String packageName, String binaryName, String codePath, String sourceFile) {
	List<BodyDeclaration> bodyDeclarations = anonymous.bodyDeclarations();
	LocationInfo locationInfo = generateLocationInfo(cu, sourceFile, anonymous, CodeElementType.ANONYMOUS_CLASS_DECLARATION);
	UMLAnonymousClass anonymousClass = new UMLAnonymousClass(packageName, binaryName, codePath, locationInfo);
	
	for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
		if(bodyDeclaration instanceof FieldDeclaration) {
			FieldDeclaration fieldDeclaration = (FieldDeclaration)bodyDeclaration;
			List<UMLAttribute> attributes = processFieldDeclaration(cu, fieldDeclaration, false, sourceFile);
    		for(UMLAttribute attribute : attributes) {
    			attribute.setClassName(anonymousClass.getCodePath());
    			anonymousClass.addAttribute(attribute);
    		}
		}
		else if(bodyDeclaration instanceof MethodDeclaration) {
			MethodDeclaration methodDeclaration = (MethodDeclaration)bodyDeclaration;
			UMLOperation operation = processMethodDeclaration(cu, methodDeclaration, packageName, false, sourceFile);
			operation.setClassName(anonymousClass.getCodePath());
			anonymousClass.addOperation(operation);
		}
	}
	
	return anonymousClass;
}
 
Example 2
Source File: ExpressionExtractor.java    From JDeodorant with MIT License 6 votes vote down vote up
private List<Expression> getExpressions(AnonymousClassDeclaration anonymousClassDeclaration) {
	List<Expression> expressionList = new ArrayList<Expression>();
	List<BodyDeclaration> bodyDeclarations = anonymousClassDeclaration.bodyDeclarations();
	for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
		if(bodyDeclaration instanceof MethodDeclaration) {
			MethodDeclaration methodDeclaration = (MethodDeclaration)bodyDeclaration;
			Block body = methodDeclaration.getBody();
			if(body != null) {
				List<Statement> statements = body.statements();
				for(Statement statement : statements) {
					expressionList.addAll(getExpressions(statement));
				}
			}
		}
	}
	return expressionList;
}
 
Example 3
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 6 votes vote down vote up
private Set<MethodDeclaration> getMethodDeclarationsWithinAnonymousClassDeclarations(MethodDeclaration methodDeclaration) {
	Set<MethodDeclaration> methods = new LinkedHashSet<MethodDeclaration>();
	ExpressionExtractor expressionExtractor = new ExpressionExtractor();
	List<Expression> classInstanceCreations = expressionExtractor.getClassInstanceCreations(methodDeclaration.getBody());
	for(Expression expression : classInstanceCreations) {
		ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)expression;
		AnonymousClassDeclaration anonymousClassDeclaration = classInstanceCreation.getAnonymousClassDeclaration();
		if(anonymousClassDeclaration != null) {
			List<BodyDeclaration> bodyDeclarations = anonymousClassDeclaration.bodyDeclarations();
			for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
				if(bodyDeclaration instanceof MethodDeclaration)
					methods.add((MethodDeclaration)bodyDeclaration);
			}
		}
	}
	return methods;
}
 
Example 4
Source File: ReplaceTypeCodeWithStateStrategy.java    From JDeodorant with MIT License 6 votes vote down vote up
private Set<MethodDeclaration> getMethodDeclarationsWithinAnonymousClassDeclarations(FieldDeclaration fieldDeclaration) {
	Set<MethodDeclaration> methods = new LinkedHashSet<MethodDeclaration>();
	List<VariableDeclarationFragment> fragments = fieldDeclaration.fragments();
	for(VariableDeclarationFragment fragment : fragments) {
		Expression expression = fragment.getInitializer();
		if(expression != null && expression instanceof ClassInstanceCreation) {
			ClassInstanceCreation classInstanceCreation = (ClassInstanceCreation)expression;
			AnonymousClassDeclaration anonymousClassDeclaration = classInstanceCreation.getAnonymousClassDeclaration();
			if(anonymousClassDeclaration != null) {
				List<BodyDeclaration> bodyDeclarations = anonymousClassDeclaration.bodyDeclarations();
				for(BodyDeclaration bodyDeclaration : bodyDeclarations) {
					if(bodyDeclaration instanceof MethodDeclaration)
						methods.add((MethodDeclaration)bodyDeclaration);
				}
			}
		}
	}
	return methods;
}
 
Example 5
Source File: TestQ20.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(AnonymousClassDeclaration node) {
//	classes++;
	for (Object d : node.bodyDeclarations()) {
		if (d instanceof FieldDeclaration)
			((FieldDeclaration)d).accept(this);
		if (d instanceof MethodDeclaration)
			((MethodDeclaration)d).accept(this);
	}
	return false;
}
 
Example 6
Source File: TestQ21.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(AnonymousClassDeclaration node) {
//	classes++;
	for (Object d : node.bodyDeclarations()) {
		if (d instanceof FieldDeclaration)
			((FieldDeclaration)d).accept(this);
		if (d instanceof MethodDeclaration)
			((MethodDeclaration)d).accept(this);
	}
	return false;
}
 
Example 7
Source File: LambdaExpressionsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
static boolean isFunctionalAnonymous(ClassInstanceCreation node) {
	ITypeBinding typeBinding= node.resolveTypeBinding();
	if (typeBinding == null)
		return false;
	ITypeBinding[] interfaces= typeBinding.getInterfaces();
	if (interfaces.length != 1)
		return false;
	if (interfaces[0].getFunctionalInterfaceMethod() == null)
		return false;

	AnonymousClassDeclaration anonymTypeDecl= node.getAnonymousClassDeclaration();
	if (anonymTypeDecl == null || anonymTypeDecl.resolveBinding() == null)
		return false;
	
	List<BodyDeclaration> bodyDeclarations= anonymTypeDecl.bodyDeclarations();
	// cannot convert if there are fields or additional methods
	if (bodyDeclarations.size() != 1)
		return false;
	BodyDeclaration bodyDeclaration= bodyDeclarations.get(0);
	if (!(bodyDeclaration instanceof MethodDeclaration))
		return false;

	MethodDeclaration methodDecl= (MethodDeclaration) bodyDeclaration;
	IMethodBinding methodBinding= methodDecl.resolveBinding();

	if (methodBinding == null)
		return false;
	// generic lambda expressions are not allowed
	if (methodBinding.isGenericMethod())
		return false;

	// lambda cannot refer to 'this'/'super' literals
	if (SuperThisReferenceFinder.hasReference(methodDecl))
		return false;
	
	if (!isInTargetTypeContext(node))
		return false;
	
	return true;
}
 
Example 8
Source File: LambdaExpressionsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
/**
		 * {@inheritDoc}
		 */
		@Override
		public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel model) throws CoreException {

			ASTRewrite rewrite= cuRewrite.getASTRewrite();
			ImportRemover importRemover= cuRewrite.getImportRemover();
			AST ast= rewrite.getAST();

			HashMap<ClassInstanceCreation, HashSet<String>> cicToNewNames= new HashMap<ClassInstanceCreation, HashSet<String>>();
			for (int i= 0; i < fExpressions.size(); i++) {
				ClassInstanceCreation classInstanceCreation= fExpressions.get(i);
				TextEditGroup group= createTextEditGroup(FixMessages.LambdaExpressionsFix_convert_to_lambda_expression, cuRewrite);

				AnonymousClassDeclaration anonymTypeDecl= classInstanceCreation.getAnonymousClassDeclaration();
				List<BodyDeclaration> bodyDeclarations= anonymTypeDecl.bodyDeclarations();

				Object object= bodyDeclarations.get(0);
				if (!(object instanceof MethodDeclaration))
					continue;
				MethodDeclaration methodDeclaration= (MethodDeclaration) object;
				HashSet<String> excludedNames= new HashSet<String>();
				if (i != 0) {
					for (ClassInstanceCreation convertedCic : fExpressions.subList(0, i)) {
						if (ASTNodes.isParent(classInstanceCreation, convertedCic)) {
							excludedNames.addAll(cicToNewNames.get(convertedCic));
						}
					}
				}
				HashSet<String> newNames= makeNamesUnique(excludedNames, methodDeclaration, rewrite, group);
				cicToNewNames.put(classInstanceCreation, new HashSet<String>(newNames));
				List<SingleVariableDeclaration> methodParameters= methodDeclaration.parameters();

				// use short form with inferred parameter types and without parentheses if possible
				LambdaExpression lambdaExpression= ast.newLambdaExpression();
				List<VariableDeclaration> lambdaParameters= lambdaExpression.parameters();
				lambdaExpression.setParentheses(methodParameters.size() != 1);
				for (SingleVariableDeclaration methodParameter : methodParameters) {
					VariableDeclarationFragment lambdaParameter= ast.newVariableDeclarationFragment();
					lambdaParameter.setName((SimpleName) rewrite.createCopyTarget(methodParameter.getName()));
					lambdaParameters.add(lambdaParameter);
				}
				
				Block body= methodDeclaration.getBody();
				List<Statement> statements= body.statements();
				ASTNode lambdaBody= body;
				if (statements.size() == 1) {
					// use short form with just an expression body if possible
					Statement statement= statements.get(0);
					if (statement instanceof ExpressionStatement) {
						lambdaBody= ((ExpressionStatement) statement).getExpression();
					} else if (statement instanceof ReturnStatement) {
						Expression returnExpression= ((ReturnStatement) statement).getExpression();
						if (returnExpression != null) {
							lambdaBody= returnExpression;
						}
					}
				}
				//TODO: Bug 421479: [1.8][clean up][quick assist] convert anonymous to lambda must consider lost scope of interface
//				lambdaBody.accept(new InterfaceAccessQualifier(rewrite, classInstanceCreation.getType().resolveBinding())); //TODO: maybe need a separate ASTRewrite and string placeholder
				
				lambdaExpression.setBody(rewrite.createCopyTarget(lambdaBody));
				Expression replacement= lambdaExpression;
				if (ASTNodes.isTargetAmbiguous(classInstanceCreation, lambdaParameters.isEmpty())) {
					CastExpression cast= ast.newCastExpression();
					cast.setExpression(lambdaExpression);
					ImportRewrite importRewrite= cuRewrite.getImportRewrite();
					ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(classInstanceCreation, importRewrite);
					Type castType= importRewrite.addImport(classInstanceCreation.getType().resolveBinding(), ast, importRewriteContext);
					cast.setType(castType);
					importRemover.registerAddedImports(castType);
					replacement= cast;
				}
				rewrite.replace(classInstanceCreation, replacement, group);

				importRemover.registerRemovedNode(classInstanceCreation);
				importRemover.registerRetainedNode(lambdaBody);
			}
		}