Java Code Examples for org.eclipse.jdt.internal.corext.dom.Bindings#isSuperType()

The following examples show how to use org.eclipse.jdt.internal.corext.dom.Bindings#isSuperType() . 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: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean consumes(final SemanticToken token) {
	final SimpleName node = token.getNode();
	if (node.isDeclaration()) {
		return false;
	}

	final IBinding binding = token.getBinding();
	if (binding == null || binding.getKind() != IBinding.VARIABLE) {
		return false;
	}

	ITypeBinding currentType = Bindings.getBindingOfParentType(node);
	ITypeBinding declaringType = ((IVariableBinding) binding).getDeclaringClass();
	if (declaringType == null || currentType == declaringType) {
		return false;
	}

	return Bindings.isSuperType(declaringType, currentType);
}
 
Example 2
Source File: ModifierCorrectionSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static int getNeededVisibility(ASTNode currNode, ITypeBinding targetType, IBinding binding) {
	ITypeBinding currNodeBinding= Bindings.getBindingOfParentType(currNode);
	if (currNodeBinding == null) { // import
		return Modifier.PUBLIC;
	}

	if (Bindings.isSuperType(targetType, currNodeBinding)) {
		if (binding != null && (JdtFlags.isProtected(binding) || binding.getKind() == IBinding.TYPE)) {
			return Modifier.PUBLIC;
		}
		return Modifier.PROTECTED;
	}

	if (currNodeBinding.getPackage().getKey().equals(targetType.getPackage().getKey())) {
		return 0;
	}
	return Modifier.PUBLIC;
}
 
Example 3
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean consumes(final SemanticToken token) {
	final SimpleName node= token.getNode();
	if (node.isDeclaration()) {
		return false;
	}

	final IBinding binding= token.getBinding();
	if (binding == null || binding.getKind() != IBinding.VARIABLE) {
		return false;
	}

	ITypeBinding currentType= Bindings.getBindingOfParentType(node);
	ITypeBinding declaringType= ((IVariableBinding) binding).getDeclaringClass();
	if (declaringType == null || currentType == declaringType)
		return false;

	return Bindings.isSuperType(declaringType, currentType);
}
 
Example 4
Source File: SemanticHighlightings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	SimpleName node= token.getNode();
	if (node.isDeclaration())
		return false;

	IBinding binding= token.getBinding();
	if (binding == null || binding.getKind() != IBinding.METHOD)
		return false;

	ITypeBinding currentType= Bindings.getBindingOfParentType(node);
	ITypeBinding declaringType= ((IMethodBinding) binding).getDeclaringClass();
	if (currentType == declaringType || currentType == null)
		return false;

	return Bindings.isSuperType(declaringType, currentType);
}
 
Example 5
Source File: ConstructorFromSuperclassProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private ITypeBinding getEnclosingInstance() {
	ITypeBinding currBinding= fTypeNode.resolveBinding();
	if (currBinding == null || Modifier.isStatic(currBinding.getModifiers())) {
		return null;
	}
	ITypeBinding superBinding= currBinding.getSuperclass();
	if (superBinding == null || superBinding.getDeclaringClass() == null || Modifier.isStatic(superBinding.getModifiers())) {
		return null;
	}
	ITypeBinding enclosing= superBinding.getDeclaringClass();

	while (currBinding != null) {
		if (Bindings.isSuperType(enclosing, currBinding)) {
			return null; // enclosing in scope
		}
		if (Modifier.isStatic(currBinding.getModifiers())) {
			return null; // no more enclosing instances
		}
		currBinding= currBinding.getDeclaringClass();
	}
	return enclosing;
}
 
Example 6
Source File: OverrideMethodDialog.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public int compare(Viewer viewer, Object first, Object second) {
	if (first instanceof ITypeBinding && second instanceof ITypeBinding) {
		final ITypeBinding left= (ITypeBinding) first;
		final ITypeBinding right= (ITypeBinding) second;
		if (right.getQualifiedName().equals("java.lang.Object")) //$NON-NLS-1$
			return -1;
		if (left.isEqualTo(right))
			return 0;
		if (Bindings.isSuperType(left, right))
			return +1;
		else if (Bindings.isSuperType(right, left))
			return -1;
		return 0;
	} else
		return super.compare(viewer, first, second);
}
 
Example 7
Source File: ModifierCorrectionSubProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
private static int getNeededVisibility(ASTNode currNode, ITypeBinding targetType, IBinding binding) {
	ITypeBinding currNodeBinding = Bindings.getBindingOfParentType(currNode);
	if (currNodeBinding == null) { // import
		return Modifier.PUBLIC;
	}

	if (Bindings.isSuperType(targetType, currNodeBinding)) {
		if (binding != null && (JdtFlags.isProtected(binding) || binding.getKind() == IBinding.TYPE)) {
			return Modifier.PUBLIC;
		}
		return Modifier.PROTECTED;
	}

	if (currNodeBinding.getPackage().getKey().equals(targetType.getPackage().getKey())) {
		return 0;
	}
	return Modifier.PUBLIC;
}
 
Example 8
Source File: SemanticHighlightings.java    From eclipse.jdt.ls with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public boolean consumes(SemanticToken token) {
	SimpleName node = token.getNode();
	if (node.isDeclaration()) {
		return false;
	}

	IBinding binding = token.getBinding();
	if (binding == null || binding.getKind() != IBinding.METHOD) {
		return false;
	}

	ITypeBinding currentType = Bindings.getBindingOfParentType(node);
	ITypeBinding declaringType = ((IMethodBinding) binding).getDeclaringClass();
	if (currentType == declaringType || currentType == null) {
		return false;
	}

	return Bindings.isSuperType(declaringType, currentType);
}
 
Example 9
Source File: OverrideMethodsOperation.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
public static List<OverridableMethod> listOverridableMethods(IType type) {
	if (type == null || type.getCompilationUnit() == null) {
		return Collections.emptyList();
	}

	List<OverridableMethod> overridables = new ArrayList<>();
	RefactoringASTParser astParser = new RefactoringASTParser(IASTSharedValues.SHARED_AST_LEVEL);
	CompilationUnit astRoot = astParser.parse(type.getCompilationUnit(), true);
	try {
		ITypeBinding typeBinding = ASTNodes.getTypeBinding(astRoot, type);
		if (typeBinding == null) {
			return overridables;
		}

		IMethodBinding cloneMethod = null;
		ITypeBinding cloneable = astRoot.getAST().resolveWellKnownType("java.lang.Cloneable");
		if (Bindings.isSuperType(cloneable, typeBinding)) {
			cloneMethod = resolveWellKnownCloneMethod(astRoot.getAST());
		}

		IPackageBinding pack = typeBinding.getPackage();
		IMethodBinding[] methods = StubUtility2Core.getOverridableMethods(astRoot.getAST(), typeBinding, false);
		for (IMethodBinding method : methods) {
			if (Bindings.isVisibleInHierarchy(method, pack)) {
				boolean toImplement = Objects.equals(method, cloneMethod);
				overridables.add(convertToSerializableMethod(method, toImplement));
			}
		}
	} catch (JavaModelException e) {
		JavaLanguageServerPlugin.logException("List overridable methods", e);
	}

	return overridables;
}
 
Example 10
Source File: MoveInnerToTopRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isAccessToOuter(ITypeBinding binding) {
	binding= binding.getTypeDeclaration();
	if (Bindings.isSuperType(binding, fCurrentType, false)) {
		return false;
	}
	ITypeBinding outer= fCurrentType.getDeclaringClass();
	while (outer != null) {
		if (Bindings.isSuperType(binding, outer, false)) {
			return true;
		}
		outer= outer.getDeclaringClass();
	}
	return false;
}
 
Example 11
Source File: IntroduceIndirectionRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private RefactoringStatus findCommonParent(ITypeBinding typeBinding) {

		RefactoringStatus status= new RefactoringStatus();

		ITypeBinding highest= fIntermediaryFirstParameterType;
		ITypeBinding current= typeBinding;

		if (current.equals(highest) || Bindings.isSuperType(highest, current))
			// current is the same as highest or highest is already a supertype of current in the same hierarchy => no change
			return status;

		// find lowest common supertype with the method
		// search in bottom-up order
		ITypeBinding[] currentAndSupers= getTypeAndAllSuperTypes(current);
		ITypeBinding[] highestAndSupers= getTypeAndAllSuperTypes(highest);

		ITypeBinding foundBinding= null;
		for (int i1= 0; i1 < currentAndSupers.length; i1++) {
			for (int i2= 0; i2 < highestAndSupers.length; i2++) {
				if (highestAndSupers[i2].isEqualTo(currentAndSupers[i1])
						&& (Bindings.findMethodInHierarchy(highestAndSupers[i2], fTargetMethodBinding.getName(), fTargetMethodBinding.getParameterTypes()) != null)) {
					foundBinding= highestAndSupers[i2];
					break;
				}
			}
			if (foundBinding != null)
				break;
		}

		if (foundBinding != null) {
			fIntermediaryFirstParameterType= foundBinding;
		} else {
			String type1= BasicElementLabels.getJavaElementName(fIntermediaryFirstParameterType.getQualifiedName());
			String type2= BasicElementLabels.getJavaElementName(current.getQualifiedName());
			status.addFatalError(Messages.format(RefactoringCoreMessages.IntroduceIndirectionRefactoring_open_hierarchy_error, new String[] { type1, type2 }));
		}

		return status;
	}
 
Example 12
Source File: FullConstraintCreator.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean containsASuperType(ITypeBinding type, Set<ITypeBinding> declaringSuperTypes) {
	for (Iterator<ITypeBinding> iter= declaringSuperTypes.iterator(); iter.hasNext();) {
		ITypeBinding maybeSuperType= iter.next();
		if (! Bindings.equals(maybeSuperType, type) && Bindings.isSuperType(maybeSuperType, type))
			return true;
	}
	return false;
}
 
Example 13
Source File: LambdaExpressionsFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean visit(MethodInvocation node) {
	IMethodBinding binding= node.resolveMethodBinding();
	if (binding != null && !JdtFlags.isStatic(binding) && node.getExpression() == null
			&& Bindings.isSuperType(binding.getDeclaringClass(), fFunctionalInterface, false))
		throw new AbortSearchException();
	return true;
}
 
Example 14
Source File: QuickAssistProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isNotYetThrown(ITypeBinding binding, List<Type> thrownExceptions) {
	for (int i= 0; i < thrownExceptions.size(); i++) {
		Type name= thrownExceptions.get(i);
		ITypeBinding elem= name.resolveBinding();
		if (elem != null) {
			if (Bindings.isSuperType(elem, binding)) { // existing exception is base class of new
				return false;
			}
		}
	}
	return true;
}
 
Example 15
Source File: CodeStyleFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static String getThisExpressionQualifier(ITypeBinding declaringClass, ImportRewrite imports, SimpleName name) {
	ITypeBinding parentType= Bindings.getBindingOfParentType(name);
	ITypeBinding currType= parentType;
	while (currType != null && !Bindings.isSuperType(declaringClass, currType)) {
		currType= currType.getDeclaringClass();
	}
	if (currType == null) {
		declaringClass= declaringClass.getTypeDeclaration();
		currType= parentType;
		while (currType != null && !Bindings.isSuperType(declaringClass, currType)) {
			currType= currType.getDeclaringClass();
		}
	}
	if (currType != parentType) {
		if (currType == null)
			return null;

		if (currType.isAnonymous())
			//If we access a field of a super class of an anonymous class
			//then we can only qualify with 'this' but not with outer.this
			//see bug 115277
			return null;

		return imports.addImport(currType);
	} else {
		return ""; //$NON-NLS-1$
	}
}
 
Example 16
Source File: QuickAssistProcessor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static boolean isNotYetThrown(ITypeBinding binding, List<Type> thrownExceptions) {
	for (Type thrownException : thrownExceptions) {
		ITypeBinding elem = thrownException.resolveBinding();
		if (elem != null) {
			if (Bindings.isSuperType(elem, binding)) { // existing exception is base class of new
				return false;
			}
		}
	}
	return true;
}
 
Example 17
Source File: TypeMismatchSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static boolean isDeclaredException(ITypeBinding curr, ITypeBinding[] declared) {
	for (int i= 0; i < declared.length; i++) {
		if (Bindings.isSuperType(declared[i], curr)) {
			return true;
		}
	}
	return false;
}
 
Example 18
Source File: UnresolvedElementsSubProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
private static void addQualifierToOuterProposal(IInvocationContext context, MethodInvocation invocationNode, IMethodBinding binding, Collection<ICommandAccess> proposals) {
	ITypeBinding declaringType= binding.getDeclaringClass();
	ITypeBinding parentType= Bindings.getBindingOfParentType(invocationNode);
	ITypeBinding currType= parentType;

	boolean isInstanceMethod= !Modifier.isStatic(binding.getModifiers());

	while (currType != null && !Bindings.isSuperType(declaringType, currType)) {
		if (isInstanceMethod && Modifier.isStatic(currType.getModifiers())) {
			return;
		}
		currType= currType.getDeclaringClass();
	}
	if (currType == null || currType == parentType) {
		return;
	}

	ASTRewrite rewrite= ASTRewrite.create(invocationNode.getAST());

	String label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_changetoouter_description, ASTResolving.getTypeSignature(currType));
	Image image= JavaPluginImages.get(JavaPluginImages.IMG_CORRECTION_CHANGE);
	ASTRewriteCorrectionProposal proposal= new ASTRewriteCorrectionProposal(label, context.getCompilationUnit(), rewrite, IProposalRelevance.QUALIFY_WITH_ENCLOSING_TYPE, image);

	ImportRewrite imports= proposal.createImportRewrite(context.getASTRoot());
	ImportRewriteContext importRewriteContext= new ContextSensitiveImportRewriteContext(invocationNode, imports);
	AST ast= invocationNode.getAST();

	String qualifier= imports.addImport(currType, importRewriteContext);
	Name name= ASTNodeFactory.newName(ast, qualifier);

	Expression newExpression;
	if (isInstanceMethod) {
		ThisExpression expr= ast.newThisExpression();
		expr.setQualifier(name);
		newExpression= expr;
	} else {
		newExpression= name;
	}

	rewrite.set(invocationNode, MethodInvocation.EXPRESSION_PROPERTY, newExpression, null);

	proposals.add(proposal);
}
 
Example 19
Source File: MoveInstanceMethodProcessor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public final boolean visit(final SimpleName node) {
	Assert.isNotNull(node);
	final AST ast= node.getAST();
	final ASTRewrite rewrite= fRewrite;
	final IBinding binding= node.resolveBinding();
	if (binding instanceof ITypeBinding) {
		ITypeBinding type= (ITypeBinding) binding;
		String name= fTargetRewrite.getImportRewrite().addImport(type.getTypeDeclaration());
		if (name != null && name.indexOf('.') != -1) {
			fRewrite.replace(node, ASTNodeFactory.newName(ast, name), null);
			return false;
		}
	}
	if (Bindings.equals(fTarget, binding))
		if (fAnonymousClass > 0) {
			final ThisExpression target= ast.newThisExpression();
			target.setQualifier(ast.newSimpleName(fTargetType.getElementName()));
			fRewrite.replace(node, target, null);
		} else
			rewrite.replace(node, ast.newThisExpression(), null);
	else if (binding instanceof IVariableBinding) {
		final IVariableBinding variable= (IVariableBinding) binding;
		final IMethodBinding method= fDeclaration.resolveBinding();
		ITypeBinding declaring= variable.getDeclaringClass();
		if (method != null) {
			if (declaring != null && Bindings.isSuperType(declaring, method.getDeclaringClass(), false)) {
				declaring= declaring.getTypeDeclaration();
				if (JdtFlags.isStatic(variable))
					rewrite.replace(node, ast.newQualifiedName(ASTNodeFactory.newName(ast, fTargetRewrite.getImportRewrite().addImport(declaring)), ast.newSimpleName(node.getFullyQualifiedName())), null);
				else {
					final FieldAccess access= ast.newFieldAccess();
					access.setExpression(ast.newSimpleName(fTargetName));
					access.setName(ast.newSimpleName(node.getFullyQualifiedName()));
					rewrite.replace(node, access, null);
				}
			} else if (!(node.getParent() instanceof QualifiedName) && JdtFlags.isStatic(variable) && !fStaticImports.contains(variable) && !Checks.isEnumCase(node.getParent())) {
				rewrite.replace(node, ast.newQualifiedName(ASTNodeFactory.newName(ast, fTargetRewrite.getImportRewrite().addImport(declaring)), ast.newSimpleName(node.getFullyQualifiedName())), null);
			}
		}
	}
	return false;
}
 
Example 20
Source File: ConvertAnonymousToNestedRefactoring.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 4 votes vote down vote up
public boolean mustInnerClassBeStatic() {
    ITypeBinding typeBinding = ((AbstractTypeDeclaration) ASTNodes.getParent(fAnonymousInnerClassNode, AbstractTypeDeclaration.class)).resolveBinding();
    ASTNode current = fAnonymousInnerClassNode.getParent();
    boolean ans = false;
    while(current != null) {
        switch(current.getNodeType()) {
case ASTNode.SUPER_CONSTRUCTOR_INVOCATION:
case ASTNode.CONSTRUCTOR_INVOCATION:
	return true;
case ASTNode.ANONYMOUS_CLASS_DECLARATION:
            {
                AnonymousClassDeclaration enclosingAnonymousClassDeclaration= (AnonymousClassDeclaration)current;
                ITypeBinding binding= enclosingAnonymousClassDeclaration.resolveBinding();
                if (binding != null && Bindings.isSuperType(typeBinding, binding.getSuperclass())) {
                    return false;
                }
                break;
            }
            case ASTNode.FIELD_DECLARATION:
            {
                FieldDeclaration enclosingFieldDeclaration= (FieldDeclaration)current;
                if (Modifier.isStatic(enclosingFieldDeclaration.getModifiers())) {
                    ans = true;
                }
                break;
            }
            case ASTNode.METHOD_DECLARATION:
            {
                MethodDeclaration enclosingMethodDeclaration = (MethodDeclaration)current;
                if (Modifier.isStatic(enclosingMethodDeclaration.getModifiers())) {
                    ans = true;
                }
                break;
            }
            case ASTNode.TYPE_DECLARATION:
            {
                return ans;
            }
        }
        current = current.getParent();
    }
    return ans;
}