Java Code Examples for org.eclipse.jdt.core.dom.Name#getLocationInParent()

The following examples show how to use org.eclipse.jdt.core.dom.Name#getLocationInParent() . 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: IntroduceParameterObjectProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
protected boolean shouldReport(IProblem problem, CompilationUnit cu) {
	if (!super.shouldReport(problem, cu))
		return false;
	ASTNode node= ASTNodeSearchUtil.getAstNode(cu, problem.getSourceStart(), problem.getSourceEnd() - problem.getSourceStart() + 1);
	if (node instanceof Type) {
		Type type= (Type) node;
		if (problem.getID() == IProblem.UndefinedType && getClassName().equals(ASTNodes.getTypeName(type))) {
			return false;
		}
	}
	if (node instanceof Name) {
		Name name= (Name) node;
		if (problem.getID() == IProblem.ImportNotFound && getPackage().indexOf(name.getFullyQualifiedName()) != -1)
			return false;
		if (problem.getID() == IProblem.MissingTypeInMethod) {
			StructuralPropertyDescriptor locationInParent= name.getLocationInParent();
			String[] arguments= problem.getArguments();
			if ((locationInParent == MethodInvocation.NAME_PROPERTY || locationInParent == SuperMethodInvocation.NAME_PROPERTY)
					&& arguments.length > 3
					&& arguments[3].endsWith(getClassName()))
				return false;
		}
	}
	return true;
}
 
Example 2
Source File: InlineConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean visit(Name name) {
	StructuralPropertyDescriptor locationInParent= name.getLocationInParent();
	if (locationInParent == ExpressionMethodReference.NAME_PROPERTY
			|| locationInParent == TypeMethodReference.NAME_PROPERTY
			|| locationInParent == SuperMethodReference.NAME_PROPERTY) {
		return false;
	}

	SimpleName leftmost= getLeftmost(name);

	IBinding leftmostBinding= leftmost.resolveBinding();
	if (leftmostBinding instanceof IVariableBinding || leftmostBinding instanceof IMethodBinding || leftmostBinding instanceof ITypeBinding) {
		if (shouldUnqualify(leftmost))
			unqualifyMemberName(leftmost);
		else
			qualifyUnqualifiedMemberNameIfNecessary(leftmost);
	}

	if (leftmostBinding instanceof ITypeBinding) {
		String addedImport= fNewLocationCuRewrite.getImportRewrite().addImport((ITypeBinding)leftmostBinding, fNewLocationContext);
		fNewLocationCuRewrite.getImportRemover().registerAddedImport(addedImport);
	}

	return false;
}
 
Example 3
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static void addNullityAnnotationTypesProposals(ICompilationUnit cu, Name node, Collection<ICommandAccess> proposals) throws CoreException {
	ASTNode parent= node.getParent();
	boolean isAnnotationName= parent instanceof Annotation && ((Annotation) parent).getTypeNameProperty() == node.getLocationInParent();
	if (!isAnnotationName) {
		boolean isImportName= parent instanceof ImportDeclaration && ImportDeclaration.NAME_PROPERTY == node.getLocationInParent();
		if (!isImportName)
			return;
	}
	
	final IJavaProject javaProject= cu.getJavaProject();
	String name= node.getFullyQualifiedName();
	
	String nullityAnnotation= null;
	String[] annotationNameOptions= { JavaCore.COMPILER_NULLABLE_ANNOTATION_NAME, JavaCore.COMPILER_NONNULL_ANNOTATION_NAME, JavaCore.COMPILER_NONNULL_BY_DEFAULT_ANNOTATION_NAME };
	Hashtable<String, String> defaultOptions= JavaCore.getDefaultOptions();
	for (String annotationNameOption : annotationNameOptions) {
		String annotationName= javaProject.getOption(annotationNameOption, true);
		if (! annotationName.equals(defaultOptions.get(annotationNameOption)))
			return;
		if (JavaModelUtil.isMatchingName(name, annotationName)) {
			nullityAnnotation= annotationName;
		}
	}
	if (nullityAnnotation == null)
		return;
	if (javaProject.findType(defaultOptions.get(annotationNameOptions[0])) != null)
		return;
	String version= JavaModelUtil.is18OrHigher(javaProject) ? "2" : "[1.1.0,2.0.0)"; //$NON-NLS-1$ //$NON-NLS-2$
	Bundle[] annotationsBundles= JavaPlugin.getDefault().getBundles("org.eclipse.jdt.annotation", version); //$NON-NLS-1$
	if (annotationsBundles == null)
		return;
	
	if (! cu.getJavaProject().getProject().hasNature("org.eclipse.pde.PluginNature")) //$NON-NLS-1$
		addCopyAnnotationsJarProposal(cu, node, nullityAnnotation, annotationsBundles[0], proposals);
}
 
Example 4
Source File: ExtractMethodAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void endVisit(CompilationUnit node) {
	RefactoringStatus status= getStatus();
	superCall: {
		if (status.hasFatalError())
			break superCall;
		if (!hasSelectedNodes()) {
			ASTNode coveringNode= getLastCoveringNode();
			if (coveringNode instanceof Block && coveringNode.getParent() instanceof MethodDeclaration) {
				MethodDeclaration methodDecl= (MethodDeclaration)coveringNode.getParent();
				Message[] messages= ASTNodes.getMessages(methodDecl, ASTNodes.NODE_ONLY);
				if (messages.length > 0) {
					status.addFatalError(Messages.format(
						RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors,
						BasicElementLabels.getJavaElementName(methodDecl.getName().getIdentifier())), JavaStatusContext.create(fCUnit, methodDecl));
					break superCall;
				}
			}
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
			break superCall;
		}
		fEnclosingBodyDeclaration= (BodyDeclaration)ASTNodes.getParent(getFirstSelectedNode(), BodyDeclaration.class);
		if (fEnclosingBodyDeclaration == null ||
				(fEnclosingBodyDeclaration.getNodeType() != ASTNode.METHOD_DECLARATION &&
				 fEnclosingBodyDeclaration.getNodeType() != ASTNode.FIELD_DECLARATION &&
				 fEnclosingBodyDeclaration.getNodeType() != ASTNode.INITIALIZER)) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_invalid_selection);
			break superCall;
		} else if (ASTNodes.getEnclosingType(fEnclosingBodyDeclaration) == null) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_compile_errors_no_parent_binding);
			break superCall;
		} else if (fEnclosingBodyDeclaration.getNodeType() == ASTNode.METHOD_DECLARATION) {
			fEnclosingMethodBinding= ((MethodDeclaration)fEnclosingBodyDeclaration).resolveBinding();
		}
		if (!isSingleExpressionOrStatementSet()) {
			status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_single_expression_or_set);
			break superCall;
		}
		if (isExpressionSelected()) {
			ASTNode expression= getFirstSelectedNode();
			if (expression instanceof Name) {
				Name name= (Name)expression;
				if (name.resolveBinding() instanceof ITypeBinding) {
					status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_type_reference);
					break superCall;
				}
				if (name.resolveBinding() instanceof IMethodBinding) {
					status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_method_name_reference);
					break superCall;
				}
				if (name.resolveBinding() instanceof IVariableBinding) {
					StructuralPropertyDescriptor locationInParent= name.getLocationInParent();
					if (locationInParent == QualifiedName.NAME_PROPERTY || (locationInParent == FieldAccess.NAME_PROPERTY && !(((FieldAccess) name.getParent()).getExpression() instanceof ThisExpression)))  {
						status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_part_of_qualified_name);
						break superCall;
					}
				}
				if (name.isSimpleName() && ((SimpleName)name).isDeclaration()) {
					status.addFatalError(RefactoringCoreMessages.ExtractMethodAnalyzer_cannot_extract_name_in_declaration);
					break superCall;
				}
			}
			fForceStatic=
				ASTNodes.getParent(expression, ASTNode.SUPER_CONSTRUCTOR_INVOCATION) != null ||
				ASTNodes.getParent(expression, ASTNode.CONSTRUCTOR_INVOCATION) != null;
		}
		status.merge(LocalTypeAnalyzer.perform(fEnclosingBodyDeclaration, getSelection()));
		computeLastStatementSelected();
	}
	super.endVisit(node);
}