org.eclipse.jdt.core.dom.Initializer Java Examples

The following examples show how to use org.eclipse.jdt.core.dom.Initializer. 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: NullAnnotationsCorrectionProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Fix for {@link IProblem#NullableFieldReference}
 * @param context context
 * @param problem problem to be fixed
 * @param proposals accumulator for computed proposals
 */
public static void addExtractCheckedLocalProposal(IInvocationContext context, IProblemLocation problem, Collection<ICommandAccess> proposals) {
	CompilationUnit compilationUnit = context.getASTRoot();
	ICompilationUnit cu= (ICompilationUnit) compilationUnit.getJavaElement();

	ASTNode selectedNode= problem.getCoveringNode(compilationUnit);

	SimpleName name= findProblemFieldName(selectedNode, problem.getProblemId());
	if (name == null)
		return;

	ASTNode method= ASTNodes.getParent(selectedNode, MethodDeclaration.class);
	if (method == null)
		method= ASTNodes.getParent(selectedNode, Initializer.class);
	if (method == null)
		return;
	
	proposals.add(new ExtractToNullCheckedLocalProposal(cu, compilationUnit, name, method));
}
 
Example #2
Source File: RenameLocalVariableProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
	initAST();
	if (fTempDeclarationNode == null || fTempDeclarationNode.resolveBinding() == null) {
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_must_select_local);
	}

	if (!Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class)
			&& !Checks.isDeclaredIn(fTempDeclarationNode, Initializer.class)
			&& !Checks.isDeclaredIn(fTempDeclarationNode, LambdaExpression.class)) {
		if (JavaModelUtil.is18OrHigher(fCu.getJavaProject())) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_initializers_and_lambda);
		}

		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_and_initializers);
	}

	initNames();
	return new RefactoringStatus();
}
 
Example #3
Source File: ExtractTempRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ASTNode getEnclosingBodyNode() throws JavaModelException {
	ASTNode node = getSelectedExpression().getAssociatedNode();

	// expression must be in a method, lambda or initializer body
	// make sure it is not in method or parameter annotation
	StructuralPropertyDescriptor location = null;
	while (node != null && !(node instanceof BodyDeclaration)) {
		location = node.getLocationInParent();
		node = node.getParent();
		if (node instanceof LambdaExpression) {
			break;
		}
	}
	if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) {
		return (ASTNode) node.getStructuralProperty(location);
	}
	return null;
}
 
Example #4
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private ASTNode getEnclosingBodyNode() throws JavaModelException {
	ASTNode node = getSelectedExpression().getAssociatedNode();

	// expression must be in a method, lambda or initializer body.
	// make sure it is not in method or parameter annotation
	StructuralPropertyDescriptor location = null;
	while (node != null && !(node instanceof BodyDeclaration)) {
		location = node.getLocationInParent();
		node = node.getParent();
		if (node instanceof LambdaExpression) {
			break;
		}
	}
	if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY || (location == LambdaExpression.BODY_PROPERTY && ((LambdaExpression) node).resolveMethodBinding() != null)) {
		return (ASTNode) node.getStructuralProperty(location);
	}
	return null;
}
 
Example #5
Source File: ExtractMethodRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private void initializeDestinations() {
	List<ASTNode> result = new ArrayList<>();
	BodyDeclaration decl = fAnalyzer.getEnclosingBodyDeclaration();
	ASTNode current = ASTResolving.findParentType(decl.getParent());
	if (fAnalyzer.isValidDestination(current)) {
		result.add(current);
	}
	if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
		ITypeBinding binding = ASTNodes.getEnclosingType(current);
		ASTNode next = ASTResolving.findParentType(current.getParent());
		while (next != null && binding != null && binding.isNested()) {
			if (fAnalyzer.isValidDestination(next)) {
				result.add(next);
			}
			current = next;
			binding = ASTNodes.getEnclosingType(current);
			next = ASTResolving.findParentType(next.getParent());
		}
	}
	fDestinations = result.toArray(new ASTNode[result.size()]);
	fDestination = fDestinations[fDestinationIndex];
}
 
Example #6
Source File: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static boolean isStaticFieldOrStaticInitializer(BodyDeclaration node) {
	if (node instanceof MethodDeclaration || node instanceof AbstractTypeDeclaration) {
		return false;
	}

	int modifiers;
	if (node instanceof FieldDeclaration) {
		modifiers = ((FieldDeclaration) node).getModifiers();
	} else if (node instanceof Initializer) {
		modifiers = ((Initializer) node).getModifiers();
	} else {
		Assert.isTrue(false);
		return false;
	}

	if (!Modifier.isStatic(modifiers)) {
		return false;
	}

	return true;
}
 
Example #7
Source File: ExtractConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static boolean isStaticFieldOrStaticInitializer(BodyDeclaration node) {
	if(node instanceof MethodDeclaration || node instanceof AbstractTypeDeclaration)
		return false;

	int modifiers;
	if(node instanceof FieldDeclaration) {
		modifiers = ((FieldDeclaration) node).getModifiers();
	} else if(node instanceof Initializer) {
		modifiers = ((Initializer) node).getModifiers();
	} else {
		Assert.isTrue(false);
		return false;
	}

	if(!Modifier.isStatic(modifiers))
		return false;

	return true;
}
 
Example #8
Source File: RenameLocalVariableProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
	initAST();
	if (fTempDeclarationNode == null || fTempDeclarationNode.resolveBinding() == null)
		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_must_select_local);

	if (!Checks.isDeclaredIn(fTempDeclarationNode, MethodDeclaration.class)
			&& !Checks.isDeclaredIn(fTempDeclarationNode, Initializer.class)
			&& !Checks.isDeclaredIn(fTempDeclarationNode, LambdaExpression.class)) {
		if (JavaModelUtil.is18OrHigher(fCu.getJavaProject()))
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_initializers_and_lambda);

		return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.RenameTempRefactoring_only_in_methods_and_initializers);
	}

	initNames();
	return new RefactoringStatus();
}
 
Example #9
Source File: ExtractMethodRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private void initializeDestinations() {
	List<ASTNode> result= new ArrayList<ASTNode>();
	BodyDeclaration decl= fAnalyzer.getEnclosingBodyDeclaration();
	ASTNode current= ASTResolving.findParentType(decl.getParent());
	if (fAnalyzer.isValidDestination(current)) {
		result.add(current);
	}
	if (current != null && (decl instanceof MethodDeclaration || decl instanceof Initializer || decl instanceof FieldDeclaration)) {
		ITypeBinding binding= ASTNodes.getEnclosingType(current);
		ASTNode next= ASTResolving.findParentType(current.getParent());
		while (next != null && binding != null && binding.isNested()) {
			if (fAnalyzer.isValidDestination(next)) {
				result.add(next);
			}
			current= next;
			binding= ASTNodes.getEnclosingType(current);
			next= ASTResolving.findParentType(next.getParent());
		}
	}
	fDestinations= result.toArray(new ASTNode[result.size()]);
	fDestination= fDestinations[fDestinationIndex];
}
 
Example #10
Source File: TestQ23.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public void endVisit(Initializer node) {
	if (maxFields < condition2)
		maxFields = condition2;
	if (minFields > condition2)
		minFields = condition2;
	condition2 = conditionStack.pop();
}
 
Example #11
Source File: ASTNodeSearchUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static Initializer getInitializerNode(IInitializer initializer, CompilationUnit cuNode) throws JavaModelException {
	ASTNode node= findNode(initializer.getSourceRange(), cuNode);
	if (node instanceof Initializer) {
		return (Initializer) node;
	}
	if (node instanceof Block && node.getParent() instanceof Initializer) {
		return (Initializer) node.getParent();
	}
	return null;
}
 
Example #12
Source File: AstVisitor.java    From jdt2famix with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(Initializer node) {
	if (importer.topOfContainerStack() instanceof Type) {
		Method method = importer.ensureInitializerMethod();
		importer.pushOnContainerStack(method);
		importer.createSourceAnchor(method, node);
		importer.ensureCommentFromBodyDeclaration(method, node);
	}
	return true;
}
 
Example #13
Source File: ASTNodeSearchUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static Initializer getInitializerNode(IInitializer initializer, CompilationUnit cuNode) throws JavaModelException {
	ASTNode node= findNode(initializer.getSourceRange(), cuNode);
	if (node instanceof Initializer)
		return (Initializer) node;
	if (node instanceof Block && node.getParent() instanceof Initializer)
		return (Initializer) node.getParent();
	return null;
}
 
Example #14
Source File: TargetProvider.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void endVisit(Initializer node) {
	if (fCurrent.hasInvocations()) {
		result.put(node, fCurrent);
	}
	endVisitBodyDeclaration();
}
 
Example #15
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Block getEnclosingBodyNode() throws JavaModelException {
	ASTNode node= getSelectedExpression().getAssociatedNode();

	// expression must be in a method or initializer body
	// make sure it is not in method or parameter annotation
	StructuralPropertyDescriptor location= null;
	while (node != null && !(node instanceof BodyDeclaration)) {
		location= node.getLocationInParent();
		node= node.getParent();
	}
	if (location == MethodDeclaration.BODY_PROPERTY || location == Initializer.BODY_PROPERTY) {
		return (Block) node.getStructuralProperty(location);
	}
	return null;
}
 
Example #16
Source File: ExtractConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void computeConstantDeclarationLocation() throws JavaModelException {
	if (isDeclarationLocationComputed())
		return;

	BodyDeclaration lastStaticDependency= null;
	Iterator<BodyDeclaration> decls= getContainingTypeDeclarationNode().bodyDeclarations().iterator();

	while (decls.hasNext()) {
		BodyDeclaration decl= decls.next();

		int modifiers;
		if (decl instanceof FieldDeclaration)
			modifiers= ((FieldDeclaration) decl).getModifiers();
		else if (decl instanceof Initializer)
			modifiers= ((Initializer) decl).getModifiers();
		else {
			continue; /* this declaration is not a field declaration
			              or initializer, so the placement of the constant
			              declaration relative to it does not matter */
		}

		if (Modifier.isStatic(modifiers) && depends(getSelectedExpression(), decl))
			lastStaticDependency= decl;
	}

	if(lastStaticDependency == null)
		fInsertFirst= true;
	else
		fToInsertAfter= lastStaticDependency;
}
 
Example #17
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean addLocalDeclarations(ASTNode node, int offset, int flags, IBindingRequestor requestor) {
	if (hasFlag(VARIABLES, flags) || hasFlag(TYPES, flags)) {
		BodyDeclaration declaration= ASTResolving.findParentBodyDeclaration(node);
		if (declaration instanceof MethodDeclaration || declaration instanceof Initializer || declaration instanceof FieldDeclaration) {
			ScopeAnalyzerVisitor visitor= new ScopeAnalyzerVisitor(offset, flags, requestor);
			declaration.accept(visitor);
			return visitor.fBreak;
		}
	}
	return false;
}
 
Example #18
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void handleVariable(SimpleName node, IVariableBinding varbinding) {
	if (!varbinding.isField())
		return;

	if (varbinding.isEnumConstant())
		return;

	ITypeBinding declaringClass= varbinding.getDeclaringClass();
	if (Modifier.isStatic(varbinding.getModifiers())) {
		if (fFindUnqualifiedStaticAccesses) {
			Initializer initializer= (Initializer) ASTNodes.getParent(node, Initializer.class);
			//Do not qualify assignments to static final fields in static initializers (would result in compile error)
			StructuralPropertyDescriptor parentDescription= node.getLocationInParent();
			if (initializer != null && Modifier.isStatic(initializer.getModifiers())
					&& Modifier.isFinal(varbinding.getModifiers()) && parentDescription == Assignment.LEFT_HAND_SIDE_PROPERTY)
				return;

			//Do not qualify static fields if defined inside an anonymous class
			if (declaringClass.isAnonymous())
				return;

			fResult.add(new AddStaticQualifierOperation(declaringClass, node));
		}
	} else if (fFindUnqualifiedAccesses){
		String qualifier= getThisExpressionQualifier(declaringClass, fImportRewrite, node);
		if (qualifier == null)
			return;

		if (qualifier.length() == 0)
			qualifier= null;

		fResult.add(new AddThisQualifierOperation(qualifier, node));
	}
}
 
Example #19
Source File: ASTResolving.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isInStaticContext(ASTNode selectedNode) {
	BodyDeclaration decl= ASTResolving.findParentBodyDeclaration(selectedNode);
	if (decl instanceof MethodDeclaration) {
		if (isInsideConstructorInvocation((MethodDeclaration) decl, selectedNode)) {
			return true;
		}
		return Modifier.isStatic(decl.getModifiers());
	} else if (decl instanceof Initializer) {
		return Modifier.isStatic(((Initializer)decl).getModifiers());
	} else if (decl instanceof FieldDeclaration) {
		return Modifier.isStatic(((FieldDeclaration)decl).getModifiers());
	}
	return false;
}
 
Example #20
Source File: TestQ28.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public void endVisit(Initializer node) {
	if (maxLocals < locals2)
		maxLocals = locals2;
	if (minLocals > locals2)
		minLocals = locals2;
	locals2 = tryStack.pop();
}
 
Example #21
Source File: TestQ28.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean preVisit2(ASTNode node) {
	if (node instanceof MethodDeclaration || node instanceof Initializer
			|| node instanceof AnnotationTypeMemberDeclaration) {
		classes++;
		tryStack.push(locals2);
		locals2 = 0;
	}
	return true;
}
 
Example #22
Source File: TestQ23.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean preVisit2(ASTNode node) {
	if (node instanceof MethodDeclaration || node instanceof Initializer || node instanceof AnnotationTypeMemberDeclaration) {
		classes++;
		conditionStack.push(condition2);
		condition2 = 0;
	}
	return true;
}
 
Example #23
Source File: TestQ25.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public void endVisit(Initializer node) {
	if (maxTry < try2)
		maxTry = try2;
	if (minTry > try2)
		minTry = try2;
	try2 = tryStack.pop();
}
 
Example #24
Source File: TestQ25.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean preVisit2(ASTNode node) {
	if (node instanceof MethodDeclaration || node instanceof Initializer || node instanceof AnnotationTypeMemberDeclaration) {
		classes++;
		tryStack.push(try2);
		try2 = 0;
	}
	return true;
}
 
Example #25
Source File: TestQ26.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public void endVisit(Initializer node) {
	if (maxThrow < throw2)
		maxThrow = throw2;
	if (minThrow > throw2)
		minThrow = throw2;
	throw2 = tryStack.pop();
}
 
Example #26
Source File: TestQ26.java    From compiler with Apache License 2.0 5 votes vote down vote up
@Override
public boolean preVisit2(ASTNode node) {
	if (node instanceof MethodDeclaration || node instanceof Initializer || node instanceof AnnotationTypeMemberDeclaration) {
		classes++;
		tryStack.push(throw2);
		throw2 = 0;
	}
	return true;
}
 
Example #27
Source File: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void computeConstantDeclarationLocation() throws JavaModelException {
	if (isDeclarationLocationComputed()) {
		return;
	}

	BodyDeclaration lastStaticDependency = null;
	Iterator<BodyDeclaration> decls = getContainingTypeDeclarationNode().bodyDeclarations().iterator();

	while (decls.hasNext()) {
		BodyDeclaration decl = decls.next();

		int modifiers;
		if (decl instanceof FieldDeclaration) {
			modifiers = ((FieldDeclaration) decl).getModifiers();
		} else if (decl instanceof Initializer) {
			modifiers = ((Initializer) decl).getModifiers();
		} else {
			continue; /* this declaration is not a field declaration
						    or initializer, so the placement of the constant
						    declaration relative to it does not matter */
		}

		if (Modifier.isStatic(modifiers) && depends(getSelectedExpression(), decl)) {
			lastStaticDependency = decl;
		}
	}

	if (lastStaticDependency == null) {
		fInsertFirst = true;
	} else {
		fToInsertAfter = lastStaticDependency;
	}
}
 
Example #28
Source File: FlowAnalyzer.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void endVisit(Initializer node) {
	if (skipNode(node)) {
		return;
	}
	assignFlowInfo(node, node.getBody());
}
 
Example #29
Source File: GenericVisitor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void endVisit(Initializer node) {
	endVisitNode(node);
}
 
Example #30
Source File: DOMFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean visit(Initializer node) {
	// note that no binding exists for an Initializer
	found(node, node);
	return true;
}