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

The following examples show how to use org.eclipse.jdt.core.dom.Name#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: NewCUProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static String getTypeName(int typeKind, Name node) {
	String name = ASTNodes.getSimpleNameIdentifier(node);

	if (isParameterizedType(typeKind, node)) {
		ASTNode parent = node.getParent();
		String typeArgBaseName = getGenericTypeArgBaseName(name);
		int nTypeArgs = ((ParameterizedType) parent.getParent()).typeArguments().size();
		StringBuilder buf = new StringBuilder(name);
		buf.append('<');
		if (nTypeArgs == 1) {
			buf.append(typeArgBaseName);
		} else {
			for (int i = 0; i < nTypeArgs; i++) {
				if (i != 0) {
					buf.append(", "); //$NON-NLS-1$
				}
				buf.append(typeArgBaseName).append(i + 1);
			}
		}
		buf.append('>');
		return buf.toString();
	}
	return name;
}
 
Example 2
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Is the specified name a target access?
 *
 * @param name
 *            the name to check
 * @return <code>true</code> if this name is a target access,
 *         <code>false</code> otherwise
 */
protected boolean isTargetAccess(final Name name) {
	Assert.isNotNull(name);
	final IBinding binding= name.resolveBinding();
	if (Bindings.equals(fTarget, binding))
		return true;
	if (name.getParent() instanceof FieldAccess) {
		final FieldAccess access= (FieldAccess) name.getParent();
		final Expression expression= access.getExpression();
		if (expression instanceof Name)
			return isTargetAccess((Name) expression);
	} else if (name instanceof QualifiedName) {
		final QualifiedName qualified= (QualifiedName) name;
		if (qualified.getQualifier() != null)
			return isTargetAccess(qualified.getQualifier());
	}
	return false;
}
 
Example 3
Source File: NewCUUsingWizardProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static String getTypeName(int typeKind, Name node) {
	String name= ASTNodes.getSimpleNameIdentifier(node);

	if (typeKind == K_CLASS || typeKind == K_INTERFACE) {
		ASTNode parent= node.getParent();
		if (parent.getLocationInParent() == ParameterizedType.TYPE_PROPERTY) {
			String typeArgBaseName= name.startsWith(String.valueOf('T')) ? String.valueOf('S') : String.valueOf('T'); // use 'S' or 'T'

			int nTypeArgs= ((ParameterizedType) parent.getParent()).typeArguments().size();
			StringBuffer buf= new StringBuffer(name);
			buf.append('<');
			if (nTypeArgs == 1) {
				buf.append(typeArgBaseName);
			} else {
				for (int i= 0; i < nTypeArgs; i++) {
					if (i != 0)
						buf.append(", "); //$NON-NLS-1$
					buf.append(typeArgBaseName).append(i + 1);
				}
			}
			buf.append('>');
			return buf.toString();
		}
	}
	return name;
}
 
Example 4
Source File: RenameAnalyzeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static VariableDeclaration getVariableDeclaration(Name node) {
	IBinding binding= node.resolveBinding();
	if (binding == null && node.getParent() instanceof VariableDeclaration) {
		return (VariableDeclaration) node.getParent();
	}

	if (binding != null && binding.getKind() == IBinding.VARIABLE) {
		CompilationUnit cu= ASTNodes.getParent(node, CompilationUnit.class);
		return ASTNodes.findVariableDeclaration( ((IVariableBinding) binding), cu);
	}
	return null;
}
 
Example 5
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Is the specified name a qualified entity, e.g. preceded by 'this',
 * 'super' or part of a method invocation?
 *
 * @param name
 *            the name to check
 * @return <code>true</code> if this entity is qualified,
 *         <code>false</code> otherwise
 */
protected static boolean isQualifiedEntity(final Name name) {
	Assert.isNotNull(name);
	final ASTNode parent= name.getParent();
	if (parent instanceof QualifiedName && ((QualifiedName) parent).getName().equals(name) || parent instanceof FieldAccess && ((FieldAccess) parent).getName().equals(name) || parent instanceof SuperFieldAccess)
		return true;
	else if (parent instanceof MethodInvocation) {
		final MethodInvocation invocation= (MethodInvocation) parent;
		return invocation.getExpression() != null && invocation.getName().equals(name);
	}
	return false;
}
 
Example 6
Source File: RenameAnalyzeUtil.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static VariableDeclaration getVariableDeclaration(Name node) {
	IBinding binding= node.resolveBinding();
	if (binding == null && node.getParent() instanceof VariableDeclaration)
		return (VariableDeclaration) node.getParent();

	if (binding != null && binding.getKind() == IBinding.VARIABLE) {
		CompilationUnit cu= (CompilationUnit) ASTNodes.getParent(node, CompilationUnit.class);
		return ASTNodes.findVariableDeclaration( ((IVariableBinding) binding), cu);
	}
	return null;
}
 
Example 7
Source File: InlineConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean doesParentQualify(Name fieldName) {
	ASTNode parent= fieldName.getParent();
	Assert.isNotNull(parent);

	if (parent instanceof FieldAccess && ((FieldAccess) parent).getName() == fieldName)
		return true;

	if (parent instanceof QualifiedName && ((QualifiedName) parent).getName() == fieldName)
		return true;

	if (parent instanceof MethodInvocation && ((MethodInvocation) parent).getName() == fieldName)
		return true;

	return false;
}
 
Example 8
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 9
Source File: UnresolvedElementsSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static void addSimilarTypeProposals(int kind, ICompilationUnit cu, Name node, int relevance,
		Collection<ChangeCorrectionProposal> proposals) throws CoreException {
	SimilarElement[] elements = SimilarElementsRequestor.findSimilarElement(cu, node, kind);

	// try to resolve type in context -> highest severity
	String resolvedTypeName= null;
	ITypeBinding binding= ASTResolving.guessBindingForTypeReference(node);
	if (binding != null) {
		ITypeBinding simpleBinding= binding;
		if (simpleBinding.isArray()) {
			simpleBinding= simpleBinding.getElementType();
		}
		simpleBinding= simpleBinding.getTypeDeclaration();

		if (!simpleBinding.isRecovered()) {
			resolvedTypeName= simpleBinding.getQualifiedName();
			CUCorrectionProposal proposal= createTypeRefChangeProposal(cu, resolvedTypeName, node, relevance + 2, elements.length);
			proposals.add(proposal);
			if (proposal instanceof AddImportCorrectionProposal) {
				proposal.setRelevance(relevance + elements.length + 2);
			}

			if (binding.isParameterizedType()
					&& (node.getParent() instanceof SimpleType || node.getParent() instanceof NameQualifiedType)
					&& !(node.getParent().getParent() instanceof Type)) {
				proposals.add(createTypeRefChangeFullProposal(cu, binding, node, relevance + 5));
			}
		}
	} else {
		ASTNode normalizedNode= ASTNodes.getNormalizedNode(node);
		if (!(normalizedNode.getParent() instanceof Type) && node.getParent() != normalizedNode) {
			ITypeBinding normBinding= ASTResolving.guessBindingForTypeReference(normalizedNode);
			if (normBinding != null && !normBinding.isRecovered()) {
				proposals.add(createTypeRefChangeFullProposal(cu, normBinding, normalizedNode, relevance + 5));
			}
		}
	}

	// add all similar elements
	for (int i= 0; i < elements.length; i++) {
		SimilarElement elem= elements[i];
		if ((elem.getKind() & TypeKinds.ALL_TYPES) != 0) {
			String fullName= elem.getName();
			if (!fullName.equals(resolvedTypeName)) {
				proposals.add(createTypeRefChangeProposal(cu, fullName, node, relevance, elements.length));
			}
		}
	}
}
 
Example 10
Source File: InlineConstantRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static Expression getQualifiedReference(Name fieldName) {
	if (doesParentQualify(fieldName))
		return (Expression) fieldName.getParent();

	return fieldName;
}
 
Example 11
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static void addSimilarTypeProposals(int kind, ICompilationUnit cu, Name node, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
	SimilarElement[] elements= SimilarElementsRequestor.findSimilarElement(cu, node, kind);

	// try to resolve type in context -> highest severity
	String resolvedTypeName= null;
	ITypeBinding binding= ASTResolving.guessBindingForTypeReference(node);
	if (binding != null) {
		ITypeBinding simpleBinding= binding;
		if (simpleBinding.isArray()) {
			simpleBinding= simpleBinding.getElementType();
		}
		simpleBinding= simpleBinding.getTypeDeclaration();

		if (!simpleBinding.isRecovered()) {
			resolvedTypeName= simpleBinding.getQualifiedName();
			CUCorrectionProposal proposal= createTypeRefChangeProposal(cu, resolvedTypeName, node, relevance + 2, elements.length);
			proposals.add(proposal);
			if (proposal instanceof AddImportCorrectionProposal)
				proposal.setRelevance(relevance + elements.length + 2);

			if (binding.isParameterizedType()
					&& (node.getParent() instanceof SimpleType || node.getParent() instanceof NameQualifiedType)
					&& !(node.getParent().getParent() instanceof Type)) {
				proposals.add(createTypeRefChangeFullProposal(cu, binding, node, relevance + 5));
			}
		}
	} else {
		ASTNode normalizedNode= ASTNodes.getNormalizedNode(node);
		if (!(normalizedNode.getParent() instanceof Type) && node.getParent() != normalizedNode) {
			ITypeBinding normBinding= ASTResolving.guessBindingForTypeReference(normalizedNode);
			if (normBinding != null && !normBinding.isRecovered()) {
				proposals.add(createTypeRefChangeFullProposal(cu, normBinding, normalizedNode, relevance + 5));
			}
		}
	}

	// add all similar elements
	for (int i= 0; i < elements.length; i++) {
		SimilarElement elem= elements[i];
		if ((elem.getKind() & SimilarElementsRequestor.ALL_TYPES) != 0) {
			String fullName= elem.getName();
			if (!fullName.equals(resolvedTypeName)) {
				proposals.add(createTypeRefChangeProposal(cu, fullName, node, relevance, elements.length));
			}
		}
	}
}