Java Code Examples for org.eclipse.jdt.core.dom.IBinding#TYPE

The following examples show how to use org.eclipse.jdt.core.dom.IBinding#TYPE . 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: 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 2
Source File: RemoveDeclarationCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public String getName() {
	IBinding binding= fName.resolveBinding();
	String name= BasicElementLabels.getJavaElementName(fName.getIdentifier());
	switch (binding.getKind()) {
		case IBinding.TYPE:
			return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedtype_description, name);
		case IBinding.METHOD:
			if (((IMethodBinding) binding).isConstructor()) {
				return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedconstructor_description, name);
			} else {
				return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedmethod_description, name);
			}
		case IBinding.VARIABLE:
			if (((IVariableBinding) binding).isField()) {
				return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedfield_description, name);
			} else {
				return Messages.format(CorrectionMessages.RemoveDeclarationCorrectionProposal_removeunusedvar_description, name);
			}
		default:
			return super.getDisplayString();
	}
}
 
Example 3
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 4
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static Map<String, String> getCleanUpOptions(IBinding binding, boolean removeAll) {
	Map<String, String> result= new Hashtable<String, String>();

	result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_MEMBERS, CleanUpOptions.TRUE);
	switch (binding.getKind()) {
		case IBinding.TYPE:
			result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_TYPES, CleanUpOptions.TRUE);
			break;
		case IBinding.METHOD:
			if (((IMethodBinding) binding).isConstructor()) {
				result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_CONSTRUCTORS, CleanUpOptions.TRUE);
			} else {
				result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_METHODS, CleanUpOptions.TRUE);
			}
			break;
		case IBinding.VARIABLE:
			if (removeAll)
				return null;

			result.put(CleanUpConstants.REMOVE_UNUSED_CODE_PRIVATE_FELDS, CleanUpOptions.TRUE);
			result.put(CleanUpConstants.REMOVE_UNUSED_CODE_LOCAL_VARIABLES, CleanUpOptions.TRUE);
			break;
	}

	return result;
}
 
Example 5
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static String getDisplayString(SimpleName simpleName, IBinding binding, boolean removeAllAssignements) {
	String name= BasicElementLabels.getJavaElementName(simpleName.getIdentifier());
	switch (binding.getKind()) {
		case IBinding.TYPE:
			return Messages.format(FixMessages.UnusedCodeFix_RemoveType_description, name);
		case IBinding.METHOD:
			if (((IMethodBinding) binding).isConstructor()) {
				return Messages.format(FixMessages.UnusedCodeFix_RemoveConstructor_description, name);
			} else {
				return Messages.format(FixMessages.UnusedCodeFix_RemoveMethod_description, name);
			}
		case IBinding.VARIABLE:
			if (removeAllAssignements) {
				return Messages.format(FixMessages.UnusedCodeFix_RemoveFieldOrLocalWithInitializer_description, name);
			} else {
				return Messages.format(FixMessages.UnusedCodeFix_RemoveFieldOrLocal_description, name);
			}
		default:
			return ""; //$NON-NLS-1$
	}
}
 
Example 6
Source File: UnusedCodeFix.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void rewriteAST(CompilationUnitRewrite cuRewrite, LinkedProposalModel linkedModel) throws CoreException {
	ASTRewrite rewrite= cuRewrite.getASTRewrite();
	IBinding binding= fUnusedName.resolveBinding();
	CompilationUnit root= (CompilationUnit) fUnusedName.getRoot();
	String displayString= FixMessages.UnusedCodeFix_RemoveUnusedTypeParameter_description;
	TextEditGroup group= createTextEditGroup(displayString, cuRewrite);

	if (binding.getKind() == IBinding.TYPE) {
		ITypeBinding decl= ((ITypeBinding) binding).getTypeDeclaration();
		ASTNode declaration= root.findDeclaringNode(decl);
		if (declaration.getParent() instanceof TypeDeclarationStatement) {
			declaration= declaration.getParent();
		}
		rewrite.remove(declaration, group);
	}
}
 
Example 7
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
private static String getSignature(IBinding binding) {
	if (binding != null) {
		switch (binding.getKind()) {
			case IBinding.METHOD:
				StringBuffer buf= new StringBuffer();
				buf.append('M');
				buf.append(binding.getName()).append('(');
				ITypeBinding[] parameters= ((IMethodBinding) binding).getParameterTypes();
				for (int i= 0; i < parameters.length; i++) {
					if (i > 0) {
						buf.append(',');
					}
					ITypeBinding paramType= parameters[i].getErasure();
					buf.append(paramType.getQualifiedName());
				}
				buf.append(')');
				return buf.toString();
			case IBinding.VARIABLE:
				return 'V' + binding.getName();
			case IBinding.TYPE:
				return 'T' + binding.getName();
		}
	}
	return null;
}
 
Example 8
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 6 votes vote down vote up
public static String getImportName(IBinding binding) {
	ITypeBinding declaring= null;
	switch (binding.getKind()) {
		case IBinding.TYPE:
			return getRawQualifiedName((ITypeBinding) binding);
		case IBinding.PACKAGE:
			return binding.getName() + ".*"; //$NON-NLS-1$
		case IBinding.METHOD:
			declaring= ((IMethodBinding) binding).getDeclaringClass();
			break;
		case IBinding.VARIABLE:
			declaring= ((IVariableBinding) binding).getDeclaringClass();
			if (declaring == null) {
				return binding.getName(); // array.length
			}

			break;
		default:
			return binding.getName();
	}
	return JavaModelUtil.concatenateName(getRawQualifiedName(declaring), binding.getName());
}
 
Example 9
Source File: ScopeAnalyzer.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private static ITypeBinding getDeclaringType(IBinding binding) {
	switch (binding.getKind()) {
		case IBinding.VARIABLE:
			return ((IVariableBinding) binding).getDeclaringClass();
		case IBinding.METHOD:
			return ((IMethodBinding) binding).getDeclaringClass();
		case IBinding.TYPE:
			ITypeBinding typeBinding= (ITypeBinding) binding;
			if (typeBinding.getDeclaringClass() != null) {
				return typeBinding;
			}
			return typeBinding;
	}
	return null;
}
 
Example 10
Source File: OrganizeImportsOperation.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tries to find the given type name and add it to the import structure.
 * @param ref the name node
 */
public void add(SimpleName ref) {
	String typeName= ref.getIdentifier();

	if (fImportsAdded.contains(typeName)) {
		return;
	}

	IBinding binding= ref.resolveBinding();
	if (binding != null) {
		if (binding.getKind() != IBinding.TYPE) {
			return;
		}
		ITypeBinding typeBinding= (ITypeBinding) binding;
		if (typeBinding.isArray()) {
			typeBinding= typeBinding.getElementType();
		}
		typeBinding= typeBinding.getTypeDeclaration();
		if (!typeBinding.isRecovered()) {
			if (needsImport(typeBinding, ref)) {
				fImpStructure.addImport(typeBinding);
				fImportsAdded.add(typeName);
			}
			return;
		}
	} else {
		if (fDoIgnoreLowerCaseNames && typeName.length() > 0) {
			char ch= typeName.charAt(0);
			if (Strings.isLowerCase(ch) && Character.isLetter(ch)) {
				return;
			}
		}
	}
	fImportsAdded.add(typeName);
	fUnresolvedTypes.put(typeName, new UnresolvedTypeData(ref));
}
 
Example 11
Source File: ImportReferencesCollector.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private void possibleTypeRefFound(Name node) {
	while (node.isQualifiedName()) {
		node= ((QualifiedName) node).getQualifier();
	}
	IBinding binding= node.resolveBinding();
	if (binding == null || binding.getKind() == IBinding.TYPE) {
		// if the binding is null, we cannot determine if
		// we have a type binding or not, so we will assume
		// we do.
		addReference((SimpleName) node);
	}
}
 
Example 12
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public static IBinding getDeclaration(IBinding binding) {
	switch (binding.getKind()) {
		case IBinding.TYPE:
			return ((ITypeBinding) binding).getTypeDeclaration();
		case IBinding.VARIABLE:
			return ((IVariableBinding) binding).getVariableDeclaration();
		case IBinding.METHOD:
			return ((IMethodBinding) binding).getMethodDeclaration();
	}
	return binding;
}
 
Example 13
Source File: DualExpressionPreconditionViolation.java    From JDeodorant with MIT License 5 votes vote down vote up
public StyledString getStyledViolation() {
	StyledString styledString = new StyledString();
	BoldStyler boldStyler = new BoldStyler();
	NormalStyler normalStyler = new NormalStyler();
	if(type.equals(PreconditionViolationType.INFEASIBLE_UNIFICATION_DUE_TO_VARIABLE_TYPE_MISMATCH)) {
		Expression expression1 = this.expression1.getExpression();
		Expression expression2 = this.expression2.getExpression();
		if(expression1 instanceof Name && ((Name)expression1).resolveBinding().getKind() == IBinding.TYPE &&
				expression2 instanceof Name && ((Name)expression2).resolveBinding().getKind() == IBinding.TYPE) {
			styledString.append("Type ", normalStyler);
			styledString.append(expression1.resolveTypeBinding().getQualifiedName(), boldStyler);
			styledString.append(" does not match with ", normalStyler);
			styledString.append("type ", normalStyler);
			styledString.append(expression2.resolveTypeBinding().getQualifiedName(), boldStyler);
		}
		else {
			expression1 = ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(expression1);
			expression2 = ASTNodeDifference.getParentExpressionOfMethodNameOrTypeName(expression2);
			styledString.append("Type ", normalStyler);
			styledString.append(expression1.resolveTypeBinding().getQualifiedName(), boldStyler);
			styledString.append(" of variable ", normalStyler);
			styledString.append(expression1.toString(), boldStyler);
			styledString.append(" does not match with ", normalStyler);
			styledString.append("type ", normalStyler);
			styledString.append(expression2.resolveTypeBinding().getQualifiedName(), boldStyler);
			styledString.append(" of variable ", normalStyler);
			styledString.append(expression2.toString(), boldStyler);
		}
	}
	return styledString;
}
 
Example 14
Source File: Bindings.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Tests if the given node is a declaration, not a instance of a generic type, method or field.
 * Declarations can be found in AST with CompilationUnit.findDeclaringNode
 * @param binding binding to test
 * @return returns <code>true</code> if the binding is a declaration binding
 */
public static boolean isDeclarationBinding(IBinding binding) {
	switch (binding.getKind()) {
		case IBinding.TYPE:
			return ((ITypeBinding) binding).getTypeDeclaration() == binding;
		case IBinding.VARIABLE:
			return ((IVariableBinding) binding).getVariableDeclaration() == binding;
		case IBinding.METHOD:
			return ((IMethodBinding) binding).getMethodDeclaration() == binding;
	}
	return true;
}
 
Example 15
Source File: OccurrencesFinder.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private IBinding getBindingDeclaration(IBinding binding) {
	switch (binding.getKind()) {
		case IBinding.TYPE :
			return ((ITypeBinding)binding).getTypeDeclaration();
		case IBinding.METHOD :
			return ((IMethodBinding)binding).getMethodDeclaration();
		case IBinding.VARIABLE :
			return ((IVariableBinding)binding).getVariableDeclaration();
		default:
			return binding;
	}
}
 
Example 16
Source File: JavaEditor.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
boolean markOccurrencesOfType(IBinding binding) {

		if (binding == null)
			return false;

		int kind= binding.getKind();

		if (fMarkTypeOccurrences && kind == IBinding.TYPE)
			return true;

		if (fMarkMethodOccurrences && kind == IBinding.METHOD)
			return true;

		if (kind == IBinding.VARIABLE) {
			IVariableBinding variableBinding= (IVariableBinding)binding;
			if (variableBinding.isField()) {
				int constantModifier= IModifierConstants.ACC_STATIC | IModifierConstants.ACC_FINAL;
				boolean isConstant= (variableBinding.getModifiers() & constantModifier) == constantModifier;
				if (isConstant)
					return fMarkConstantOccurrences;
				else
					return fMarkFieldOccurrences;
			}

			return fMarkLocalVariableypeOccurrences;
		}

		return false;
	}
 
Example 17
Source File: NewMethodCorrectionProposal.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
private int evaluateModifiers(ASTNode targetTypeDecl) {
	if (getSenderBinding().isAnnotation()) {
		return 0;
	}
	if (getSenderBinding().isInterface()) {
		// for interface and annotation members copy the modifiers from an existing field
		MethodDeclaration[] methodDecls= ((TypeDeclaration) targetTypeDecl).getMethods();
		if (methodDecls.length > 0) {
			return methodDecls[0].getModifiers();
		}
		return 0;
	}
	ASTNode invocationNode= getInvocationNode();
	if (invocationNode instanceof MethodInvocation) {
		int modifiers= 0;
		Expression expression= ((MethodInvocation)invocationNode).getExpression();
		if (expression != null) {
			if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(invocationNode)) {
			modifiers |= Modifier.STATIC;
		}
		ASTNode node= ASTResolving.findParentType(invocationNode);
		if (targetTypeDecl.equals(node)) {
			modifiers |= Modifier.PRIVATE;
		} else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
			modifiers |= Modifier.PROTECTED;
			if (ASTResolving.isInStaticContext(node) && expression == null) {
				modifiers |= Modifier.STATIC;
			}
		} else {
			modifiers |= Modifier.PUBLIC;
		}
		return modifiers;
	}
	return Modifier.PUBLIC;
}
 
Example 18
Source File: TypeVisitor.java    From JDeodorant with MIT License 5 votes vote down vote up
public boolean visit(SimpleName node) {
	IBinding binding = node.resolveBinding();
	if(binding != null && binding.getKind() == IBinding.TYPE) {
		ITypeBinding typeBinding = (ITypeBinding)binding;
		typeBindings.add(typeBinding);
	}
	return super.visit(node);
}
 
Example 19
Source File: NewMethodCorrectionProposal.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private int evaluateModifiers(ASTNode targetTypeDecl) {
	if (getSenderBinding().isAnnotation()) {
		return 0;
	}
	boolean isTargetInterface= getSenderBinding().isInterface();
	if (isTargetInterface && !JavaModelUtil.is18OrHigher(getCompilationUnit().getJavaProject())) {
		// only abstract methods are allowed for interface present in less than Java 1.8
		return getInterfaceMethodModifiers(targetTypeDecl, true);
	}
	ASTNode invocationNode= getInvocationNode();
	if (invocationNode instanceof MethodInvocation) {
		int modifiers= 0;
		Expression expression= ((MethodInvocation)invocationNode).getExpression();
		if (expression != null) {
			if (expression instanceof Name && ((Name) expression).resolveBinding().getKind() == IBinding.TYPE) {
				modifiers |= Modifier.STATIC;
			}
		} else if (ASTResolving.isInStaticContext(invocationNode)) {
			modifiers |= Modifier.STATIC;
		}
		ASTNode node= ASTResolving.findParentType(invocationNode);
		boolean isParentInterface= node instanceof TypeDeclaration && ((TypeDeclaration) node).isInterface();
		if (isTargetInterface || isParentInterface) {
			if (expression == null && !targetTypeDecl.equals(node)) {
				modifiers|= Modifier.STATIC;
				if (isTargetInterface) {
					modifiers|= getInterfaceMethodModifiers(targetTypeDecl, false);
				} else {
					modifiers|= Modifier.PROTECTED;
				}
			} else if (modifiers == Modifier.STATIC) {
				modifiers= getInterfaceMethodModifiers(targetTypeDecl, false) | Modifier.STATIC;
			} else {
				modifiers= getInterfaceMethodModifiers(targetTypeDecl, true);
			}
		} else if (targetTypeDecl.equals(node)) {
			modifiers |= Modifier.PRIVATE;
		} else if (node instanceof AnonymousClassDeclaration && ASTNodes.isParent(node, targetTypeDecl)) {
			modifiers |= Modifier.PROTECTED;
			if (ASTResolving.isInStaticContext(node) && expression == null) {
				modifiers |= Modifier.STATIC;
			}
		} else {
			modifiers |= Modifier.PUBLIC;
		}
		return modifiers;
	}
	return Modifier.PUBLIC;
}
 
Example 20
Source File: SemanticTokensVisitor.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public boolean visit(SimpleName node) {
    ASTNode parent = node.getParent();
    if (parent instanceof Annotation) {
        addToken(node, TokenType.ANNOTATION);
        return false;
    }

    IBinding binding = node.resolveBinding();
    if (binding == null) {
        return super.visit(node);
    }

    TokenType tokenType = null;
    switch (binding.getKind()) {
        case IBinding.VARIABLE: {
            if (((IVariableBinding) binding).isField()) {
                tokenType = TokenType.PROPERTY;
            } else {
                tokenType = TokenType.VARIABLE;
            }
            break;
        }
        case IBinding.METHOD: {
            tokenType = TokenType.FUNCTION;
            break;
        }
        case IBinding.TYPE: {
            tokenType = TokenType.TYPE;
            break;
        }
        case IBinding.PACKAGE: {
            tokenType = TokenType.NAMESPACE;
            break;
        }
        default:
            break;
    }

    if (tokenType == null) {
        return super.visit(node);
    }

    switch (tokenType) {
        case FUNCTION:
        case VARIABLE:
        case PROPERTY:
        case MEMBER: {
            ITokenModifier[] modifiers = getModifiers(binding);
            addToken(node, tokenType, modifiers);
            break;
        }
        case TYPE:
        case NAMESPACE:
            addToken(node, tokenType, NO_MODIFIERS);
            break;
        default:
            break;
    }

    return super.visit(node);
}