Java Code Examples for org.eclipse.jdt.internal.corext.dom.ASTNodes#getParent()

The following examples show how to use org.eclipse.jdt.internal.corext.dom.ASTNodes#getParent() . 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: AddGetterSetterOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Generates a new setter method for the specified field
 * 
 * @param field the field
 * @param astRewrite the AST rewrite to use
 * @param rewrite the list rewrite to use
 * @throws CoreException if an error occurs
 * @throws OperationCanceledException if the operation has been cancelled
 */
private void generateSetterMethod(final IField field, ASTRewrite astRewrite, final ListRewrite rewrite) throws CoreException, OperationCanceledException {
	final IType type= field.getDeclaringType();
	final String name= GetterSetterUtil.getSetterName(field, null);
	final IMethod existing= JavaModelUtil.findMethod(name, new String[] { field.getTypeSignature()}, false, type);
	if (existing == null || !querySkipExistingMethods(existing)) {
		IJavaElement sibling= null;
		if (existing != null) {
			sibling= StubUtility.findNextSibling(existing);
			removeExistingAccessor(existing, rewrite);
		} else
			sibling= fInsert;
		ASTNode insertion= StubUtility2.getNodeToInsertBefore(rewrite, sibling);
		addNewAccessor(type, field, GetterSetterUtil.getSetterStub(field, name, fSettings.createComments, fVisibility | (field.getFlags() & Flags.AccStatic)), rewrite, insertion);
		if (Flags.isFinal(field.getFlags())) {
			ASTNode fieldDecl= ASTNodes.getParent(NodeFinder.perform(fASTRoot, field.getNameRange()), FieldDeclaration.class);
			if (fieldDecl != null) {
				ModifierRewrite.create(astRewrite, fieldDecl).setModifiers(0, Modifier.FINAL, null);
			}
		}

	}
}
 
Example 2
Source File: ExtractTempRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isUsedInExplicitConstructorCall() throws JavaModelException {
	Expression selectedExpression= getSelectedExpression().getAssociatedExpression();
	if (ASTNodes.getParent(selectedExpression, ConstructorInvocation.class) != null)
		return true;
	if (ASTNodes.getParent(selectedExpression, SuperConstructorInvocation.class) != null)
		return true;
	return false;
}
 
Example 3
Source File: InlineConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private Set<String> getNamesDeclaredLocallyAtNewLocation() {
	if (fNamesDeclaredLocallyAtNewLocation != null)
		return fNamesDeclaredLocallyAtNewLocation;

	BodyDeclaration enclosingBodyDecl= (BodyDeclaration) ASTNodes.getParent(fNewLocation, BodyDeclaration.class);
	Assert.isTrue(!(enclosingBodyDecl instanceof AbstractTypeDeclaration));

	return fNamesDeclaredLocallyAtNewLocation= getLocallyDeclaredNames(enclosingBodyDecl);
}
 
Example 4
Source File: AddUnimplementedConstructorsAction.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public AddUnimplementedConstructorsContentProvider(IType type) throws JavaModelException {
	RefactoringASTParser parser= new RefactoringASTParser(ASTProvider.SHARED_AST_LEVEL);
	fUnit= parser.parse(type.getCompilationUnit(), true);
	AbstractTypeDeclaration declaration= (AbstractTypeDeclaration) ASTNodes.getParent(NodeFinder.perform(fUnit, type.getNameRange()), AbstractTypeDeclaration.class);
	if (declaration != null) {
		ITypeBinding binding= declaration.resolveBinding();
		if (binding != null)
			fMethodsList= StubUtility2.getVisibleConstructors(binding, true, false);
	}
}
 
Example 5
Source File: SelfEncapsulateFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private void checkInHierarchy(RefactoringStatus status, boolean usingLocalGetter, boolean usingLocalSetter) {
	AbstractTypeDeclaration declaration = ASTNodes.getParent(fFieldDeclaration, AbstractTypeDeclaration.class);
	ITypeBinding type = declaration.resolveBinding();
	if (type != null) {
		ITypeBinding fieldType = fFieldDeclaration.resolveBinding().getType();
		checkMethodInHierarchy(type, fGetterName, fieldType, new ITypeBinding[0], status, usingLocalGetter);
		checkMethodInHierarchy(type, fSetterName, fFieldDeclaration.getAST().resolveWellKnownType("void"), //$NON-NLS-1$
				new ITypeBinding[] { fieldType }, status, usingLocalSetter);
	}
}
 
Example 6
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void addNestedClass(CompilationUnitRewrite rewrite, ITypeBinding[] typeParameters) throws CoreException {
    final AbstractTypeDeclaration declarations= (AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class);
    int index= findIndexOfFistNestedClass(declarations.bodyDeclarations());
    if (index == -1)
        index= 0;
    rewrite.getASTRewrite().getListRewrite(declarations, declarations.getBodyDeclarationsProperty()).insertAt(createNewNestedClass(rewrite, typeParameters), index, null);
}
 
Example 7
Source File: NullAnnotationsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isComplainingAboutArgument(ASTNode selectedNode) {
	if (!(selectedNode instanceof SimpleName))
		return false;
	SimpleName nameNode= (SimpleName) selectedNode;
	IBinding binding= nameNode.resolveBinding();
	if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
		return true;
	VariableDeclaration argDecl= (VariableDeclaration) ASTNodes.getParent(selectedNode, VariableDeclaration.class);
	if (argDecl != null)
		binding= argDecl.resolveBinding();
	if (binding.getKind() == IBinding.VARIABLE && ((IVariableBinding) binding).isParameter())
		return true;
	return false;
}
 
Example 8
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private boolean isUsedInExplicitConstructorCall() throws JavaModelException {
	Expression selectedExpression = getSelectedExpression().getAssociatedExpression();
	if (ASTNodes.getParent(selectedExpression, ConstructorInvocation.class) != null) {
		return true;
	}
	if (ASTNodes.getParent(selectedExpression, SuperConstructorInvocation.class) != null) {
		return true;
	}
	return false;
}
 
Example 9
Source File: Checks.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static boolean isDeclaredIn(VariableDeclaration tempDeclaration, Class<? extends ASTNode> astNodeClass) {
	ASTNode initializer= ASTNodes.getParent(tempDeclaration, astNodeClass);
	if (initializer == null)
		return false;
	ASTNode anonymous= ASTNodes.getParent(tempDeclaration, AnonymousClassDeclaration.class);
	if (anonymous == null)
		return true;
	// stupid code. Is to find out if the variable declaration isn't a field.
	if (ASTNodes.isParent(anonymous, initializer))
		return false;
	return true;
}
 
Example 10
Source File: ASTNodeSearchUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static EnumDeclaration getEnumDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
	return (EnumDeclaration) ASTNodes.getParent(getNameNode(iType, cuNode), EnumDeclaration.class);
}
 
Example 11
Source File: SelfEncapsulateFieldRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private MethodDeclaration createSetterMethod(AST ast, ASTRewrite rewriter, String lineDelimiter) throws CoreException {
	FieldDeclaration field= (FieldDeclaration)ASTNodes.getParent(fFieldDeclaration, FieldDeclaration.class);
	Type type= field.getType();
	MethodDeclaration result= ast.newMethodDeclaration();
	result.setName(ast.newSimpleName(fSetterName));
	result.modifiers().addAll(ASTNodeFactory.newModifiers(ast, createModifiers()));
	if (fSetterMustReturnValue) {
		result.setReturnType2((Type)rewriter.createCopyTarget(type));
	}
	SingleVariableDeclaration param= ast.newSingleVariableDeclaration();
	result.parameters().add(param);
	param.setName(ast.newSimpleName(fArgName));
	param.setType((Type)rewriter.createCopyTarget(type));
	List<Dimension> extraDimensions= DimensionRewrite.copyDimensions(fFieldDeclaration.extraDimensions(), rewriter);
	param.extraDimensions().addAll(extraDimensions);

	Block block= ast.newBlock();
	result.setBody(block);

	String fieldAccess= createFieldAccess();
	String body= CodeGeneration.getSetterMethodBodyContent(fField.getCompilationUnit(), getTypeName(field.getParent()), fSetterName, fieldAccess, fArgName, lineDelimiter);
	if (body != null) {
		ASTNode setterNode= rewriter.createStringPlaceholder(body, ASTNode.BLOCK);
		block.statements().add(setterNode);
	} else {
		Assignment ass= ast.newAssignment();
		ass.setLeftHandSide((Expression) rewriter.createStringPlaceholder(fieldAccess, ASTNode.QUALIFIED_NAME));
		ass.setRightHandSide(ast.newSimpleName(fArgName));
		block.statements().add(ass);
	}
       if (fSetterMustReturnValue) {
       	ReturnStatement rs= ast.newReturnStatement();
       	rs.setExpression(ast.newSimpleName(fArgName));
       	block.statements().add(rs);
       }
       if (fGenerateJavadoc) {
		String string= CodeGeneration.getSetterComment(
			fField.getCompilationUnit() , getTypeName(field.getParent()), fSetterName,
			fField.getElementName(), ASTNodes.asString(type), fArgName,
			StubUtility.getBaseName(fField),
			lineDelimiter);
		if (string != null) {
			Javadoc javadoc= (Javadoc)fRewriter.createStringPlaceholder(string, ASTNode.JAVADOC);
			result.setJavadoc(javadoc);
		}
	}
	return result;
}
 
Example 12
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private boolean isInLocalTypeInsideInputType(ASTNode node, AbstractTypeDeclaration declaration) {
	final TypeDeclarationStatement statement= (TypeDeclarationStatement) ASTNodes.getParent(node, TypeDeclarationStatement.class);
	return statement != null && ASTNodes.isParent(statement, declaration);
}
 
Example 13
Source File: ExtractConstantRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private boolean isInTypeDeclarationAnnotation(ASTNode node) throws JavaModelException {
	ASTNode enclosingAnnotation = ASTNodes.getParent(node, Annotation.class);
	return enclosingAnnotation != null && enclosingAnnotation.getParent() == getContainingTypeDeclarationNode();
}
 
Example 14
Source File: RefactoringAnalyzeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static Block getBlock(TextEdit edit, TextChange change, CompilationUnit cuNode) {
	ASTNode decl = RefactoringAnalyzeUtil.findSimpleNameNode(RefactoringAnalyzeUtil.getNewTextRange(edit, change), cuNode);
	return (ASTNodes.getParent(decl, Block.class));
}
 
Example 15
Source File: SnippetFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public MethodDeclaration getEnclosingMethod() {
	ASTNode first= fNodes.get(0);
	return (MethodDeclaration)ASTNodes.getParent(first, ASTNode.METHOD_DECLARATION);
}
 
Example 16
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private ASTNode getEnclosingTypeDeclaration() throws JavaModelException {
	if (isDeclaredInLambdaExpression()) {
		return ASTNodes.getParent(getSelectedExpression().getAssociatedNode(), AbstractTypeDeclaration.class);
	}
	return getMethodDeclaration().getParent();
}
 
Example 17
Source File: ReturnTypeVariable.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public static MethodDeclaration getMethod(ReturnStatement returnStatement) {
	return (MethodDeclaration)ASTNodes.getParent(returnStatement, MethodDeclaration.class);
}
 
Example 18
Source File: ASTNodeSearchUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static AbstractTypeDeclaration getAbstractTypeDeclarationNode(IType iType, CompilationUnit cuNode) throws JavaModelException {
	return ASTNodes.getParent(getNameNode(iType, cuNode), AbstractTypeDeclaration.class);
}
 
Example 19
Source File: ExtractFieldRefactoring.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException, OperationCanceledException {
	try {
		pm.beginTask("", 16); //$NON-NLS-1$

		RefactoringStatus result = Checks.validateModifiesFiles(ResourceUtil.getFiles(new ICompilationUnit[] { fCu }), getValidationContext(), pm);
		if (result.hasFatalError()) {
			return result;
		}

		if (fCompilationUnitNode == null) {
			fCompilationUnitNode = RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3));
		}
		pm.worked(1);

		if (fCURewrite == null) {
			fCURewrite = new CompilationUnitRewrite(fCu, fCompilationUnitNode);
			fCURewrite.setFormattingOptions(fFormatterOptions);
			fCURewrite.getASTRewrite().setTargetSourceRangeComputer(new NoCommentSourceRangeComputer());
		}
		pm.worked(1);

		// Check the conditions for extracting an expression to a variable.
		IExpressionFragment selectedExpression = getSelectedExpression();
		if (selectedExpression == null) {
			String message = RefactoringCoreMessages.ExtractTempRefactoring_select_expression;
			return CodeRefactoringUtil.checkMethodSyntaxErrors(fSelectionStart, fSelectionLength, fCompilationUnitNode, message);
		}
		pm.worked(1);

		if (isUsedInExplicitConstructorCall()) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_explicit_constructor);
		}
		pm.worked(1);

		ASTNode associatedNode = selectedExpression.getAssociatedNode();
		if (getEnclosingBodyNode() == null || ASTNodes.getParent(associatedNode, Annotation.class) != null) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_expr_in_method_or_initializer);
		}
		pm.worked(1);

		if (associatedNode instanceof Name && associatedNode.getParent() instanceof ClassInstanceCreation && associatedNode.getLocationInParent() == ClassInstanceCreation.TYPE_PROPERTY) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_name_in_new);
		}
		pm.worked(1);

		result.merge(checkExpression());
		if (result.hasFatalError()) {
			return result;
		}
		pm.worked(1);

		result.merge(checkExpressionFragmentIsRValue());
		if (result.hasFatalError()) {
			return result;
		}
		pm.worked(1);

		Expression associatedExpression = selectedExpression.getAssociatedExpression();
		if (isUsedInForInitializerOrUpdater(associatedExpression)) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_for_initializer_updater);
		}
		pm.worked(1);

		if (isReferringToLocalVariableFromFor(associatedExpression)) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_refers_to_for_variable);
		}
		pm.worked(1);

		// Check the conditions for extracting an expression to field.
		ASTNode declaringType = getEnclosingTypeDeclaration();
		if (declaringType instanceof TypeDeclaration && ((TypeDeclaration) declaringType).isInterface()) {
			return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractFieldRefactoring_interface_methods);
		}
		pm.worked(1);

		result.merge(checkTempTypeForLocalTypeUsage());
		if (result.hasFatalError()) {
			return result;
		}
		pm.worked(1);

		checkTempInitializerForLocalTypeUsage();
		initializeDefaults();
		pm.worked(1);

		return result;
	} finally {
		pm.done();
	}
}
 
Example 20
Source File: ASTNodeSearchUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
public static AnnotationTypeMemberDeclaration getAnnotationTypeMemberDeclarationNode(IMethod iMethod, CompilationUnit cuNode) throws JavaModelException {
	return ASTNodes.getParent(getNameNode(iMethod, cuNode), AnnotationTypeMemberDeclaration.class);
}