Java Code Examples for org.eclipse.jdt.core.dom.ITypeBinding#isInterface()
The following examples show how to use
org.eclipse.jdt.core.dom.ITypeBinding#isInterface() .
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: TypeURIHelper.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
protected void createFragment(ITypeBinding typeBinding, StringBuilder uriBuilder) { if (typeBinding.isPrimitive()) { createFragmentForPrimitive(typeBinding, uriBuilder); return; } if (typeBinding.isArray()) { createFragmentForArray(typeBinding, uriBuilder); return; } if (typeBinding.isTypeVariable()) { createFragmentForTypeVariable(typeBinding, uriBuilder); return; } if (typeBinding.isAnnotation() || typeBinding.isClass() || typeBinding.isInterface() || typeBinding.isEnum()) { createFragmentForClass(typeBinding, uriBuilder); return; } throw new IllegalStateException("Unexpected type binding: " + typeBinding); }
Example 2
Source File: TypeURIHelper.java From xtext-eclipse with Eclipse Public License 2.0 | 6 votes |
protected void createResourceURI(ITypeBinding typeBinding, StringBuilder uriBuilder) { if (typeBinding.isPrimitive()) { createResourceURIForPrimitive(uriBuilder); return; } if (typeBinding.isClass() || typeBinding.isInterface() || typeBinding.isAnnotation() || typeBinding.isEnum()) { createResourceURIForClass(typeBinding, uriBuilder); return; } if (typeBinding.isArray()) { createResourceURIForArray(typeBinding, uriBuilder); return; } if (typeBinding.isTypeVariable()) { createResourceURIForTypeVariable(typeBinding, uriBuilder); return; } throw new IllegalStateException("Unexpected type: " + typeBinding); }
Example 3
Source File: CastCorrectionProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private ITypeBinding getCastFavorite(ITypeBinding[] suggestedCasts, ITypeBinding nodeToCastBinding) { if (nodeToCastBinding == null) { return suggestedCasts[0]; } ITypeBinding favourite= suggestedCasts[0]; for (int i = 0; i < suggestedCasts.length; i++) { ITypeBinding curr= suggestedCasts[i]; if (nodeToCastBinding.isCastCompatible(curr)) { return curr; } if (curr.isInterface()) { favourite= curr; } } return favourite; }
Example 4
Source File: ScopeAnalyzer.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Evaluates if the declaration is visible in a certain context. * @param binding The binding of the declaration to examine * @param context The context to test in * @return Returns */ public static boolean isVisible(IBinding binding, ITypeBinding context) { if (binding.getKind() == IBinding.VARIABLE && !((IVariableBinding) binding).isField()) { return true; // all local variables found are visible } ITypeBinding declaring= getDeclaringType(binding); if (declaring == null) { return false; } declaring= declaring.getTypeDeclaration(); int modifiers= binding.getModifiers(); if (Modifier.isPublic(modifiers) || declaring.isInterface()) { return true; } else if (Modifier.isProtected(modifiers) || !Modifier.isPrivate(modifiers)) { if (declaring.getPackage() == context.getPackage()) { return true; } return isTypeInScope(declaring, context, Modifier.isProtected(modifiers)); } // private visibility return isTypeInScope(declaring, context, false); }
Example 5
Source File: StubUtility2.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static IMethodBinding[] getDelegateCandidates(ITypeBinding binding, ITypeBinding hierarchy) { List<IMethodBinding> allMethods= new ArrayList<IMethodBinding>(); boolean isInterface= binding.isInterface(); IMethodBinding[] typeMethods= binding.getDeclaredMethods(); for (int index= 0; index < typeMethods.length; index++) { final int modifiers= typeMethods[index].getModifiers(); if (!typeMethods[index].isConstructor() && !Modifier.isStatic(modifiers) && (isInterface || Modifier.isPublic(modifiers))) { IMethodBinding result= Bindings.findOverriddenMethodInHierarchy(hierarchy, typeMethods[index]); if (result != null && Flags.isFinal(result.getModifiers())) continue; ITypeBinding[] parameterBindings= typeMethods[index].getParameterTypes(); boolean upper= false; for (int offset= 0; offset < parameterBindings.length; offset++) { if (parameterBindings[offset].isWildcardType() && parameterBindings[offset].isUpperbound()) upper= true; } if (!upper) allMethods.add(typeMethods[index]); } } return allMethods.toArray(new IMethodBinding[allMethods.size()]); }
Example 6
Source File: NewCUUsingWizardProposal.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Fill-in the "Super Class" and "Super Interfaces" fields. * @param page the wizard page. */ private void fillInWizardPageSuperTypes(NewTypeWizardPage page) { ITypeBinding type= getPossibleSuperTypeBinding(fNode); type= Bindings.normalizeTypeBinding(type); if (type != null) { if (type.isArray()) { type= type.getElementType(); } if (type.isTopLevel() || type.isMember()) { if (type.isClass() && (fTypeKind == K_CLASS)) { page.setSuperClass(type.getQualifiedName(), true); } else if (type.isInterface()) { List<String> superInterfaces= new ArrayList<String>(); superInterfaces.add(type.getQualifiedName()); page.setSuperInterfaces(superInterfaces, true); } } } }
Example 7
Source File: JdtUtils.java From j2cl with Apache License 2.0 | 5 votes |
private static Kind getKindFromTypeBinding(ITypeBinding typeBinding) { if (typeBinding.isEnum() && !typeBinding.isAnonymous()) { // Do not consider the anonymous classes that constitute enum values as Enums, only the // enum "class" itself is considered Kind.ENUM. return Kind.ENUM; } else if (typeBinding.isClass() || (typeBinding.isEnum() && typeBinding.isAnonymous())) { return Kind.CLASS; } else if (typeBinding.isInterface()) { return Kind.INTERFACE; } throw new InternalCompilerError("Type binding %s not handled", typeBinding); }
Example 8
Source File: Bindings.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * Returns <code>true</code> if the given type is a super type of a candidate. * <code>true</code> is returned if the two type bindings are identical (TODO) * @param possibleSuperType the type to inspect * @param type the type whose super types are looked at * @param considerTypeArguments if <code>true</code>, consider type arguments of <code>type</code> * @return <code>true</code> iff <code>possibleSuperType</code> is * a super type of <code>type</code> or is equal to it */ public static boolean isSuperType(ITypeBinding possibleSuperType, ITypeBinding type, boolean considerTypeArguments) { if (type.isArray() || type.isPrimitive()) { return false; } if (! considerTypeArguments) { type= type.getTypeDeclaration(); } if (Bindings.equals(type, possibleSuperType)) { return true; } ITypeBinding superClass= type.getSuperclass(); if (superClass != null) { if (isSuperType(possibleSuperType, superClass, considerTypeArguments)) { return true; } } if (possibleSuperType.isInterface()) { ITypeBinding[] superInterfaces= type.getInterfaces(); for (int i= 0; i < superInterfaces.length; i++) { if (isSuperType(possibleSuperType, superInterfaces[i], considerTypeArguments)) { return true; } } } return false; }
Example 9
Source File: QuickAssistProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private static boolean getAssignAllParamsToFieldsProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) { node = ASTNodes.getNormalizedNode(node); ASTNode parent = node.getParent(); if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) { return false; } MethodDeclaration methodDecl = (MethodDeclaration) parent.getParent(); if (methodDecl.getBody() == null) { return false; } List<SingleVariableDeclaration> parameters = methodDecl.parameters(); if (parameters.size() <= 1) { return false; } ITypeBinding parentType = Bindings.getBindingOfParentType(node); if (parentType == null || parentType.isInterface()) { return false; } for (SingleVariableDeclaration param : parameters) { IVariableBinding binding = param.resolveBinding(); if (binding == null || binding.getType() == null) { return false; } } if (resultingCollections == null) { return true; } AssignToVariableAssistProposal fieldProposal = new AssignToVariableAssistProposal(context.getCompilationUnit(), parameters, IProposalRelevance.ASSIGN_ALL_PARAMS_TO_NEW_FIELDS); resultingCollections.add(fieldProposal); return true; }
Example 10
Source File: Bindings.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public static boolean isVisibleInHierarchy(IMethodBinding member, IPackageBinding pack) { int otherflags= member.getModifiers(); ITypeBinding declaringType= member.getDeclaringClass(); if (Modifier.isPublic(otherflags) || Modifier.isProtected(otherflags) || (declaringType != null && declaringType.isInterface())) { return true; } else if (Modifier.isPrivate(otherflags)) { return false; } return declaringType != null && pack == declaringType.getPackage(); }
Example 11
Source File: MethodObject.java From JDeodorant with MIT License | 5 votes |
private boolean overridesMethod(Set<ITypeBinding> typeBindings) { IMethodBinding methodBinding = getMethodDeclaration().resolveBinding(); Set<ITypeBinding> superTypeBindings = new LinkedHashSet<ITypeBinding>(); for(ITypeBinding typeBinding : typeBindings) { ITypeBinding superClassTypeBinding = typeBinding.getSuperclass(); if(superClassTypeBinding != null) superTypeBindings.add(superClassTypeBinding); ITypeBinding[] interfaceTypeBindings = typeBinding.getInterfaces(); for(ITypeBinding interfaceTypeBinding : interfaceTypeBindings) superTypeBindings.add(interfaceTypeBinding); if(typeBinding.isInterface()) { IMethodBinding[] interfaceMethodBindings = typeBinding.getDeclaredMethods(); for(IMethodBinding interfaceMethodBinding : interfaceMethodBindings) { if(methodBinding.overrides(interfaceMethodBinding) || methodBinding.toString().equals(interfaceMethodBinding.toString())) return true; } } else { IMethodBinding[] superClassMethodBindings = typeBinding.getDeclaredMethods(); for(IMethodBinding superClassMethodBinding : superClassMethodBindings) { if(methodBinding.overrides(superClassMethodBinding) || (methodBinding.toString().equals(superClassMethodBinding.toString()) && (superClassMethodBinding.getModifiers() & Modifier.PRIVATE) == 0) ) return true; } } } if(!superTypeBindings.isEmpty()) { return overridesMethod(superTypeBindings); } else return false; }
Example 12
Source File: MoveInnerToTopRefactoring.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private void modifyInterfaceMemberModifiers(final ITypeBinding binding) { Assert.isNotNull(binding); ITypeBinding declaring= binding.getDeclaringClass(); while (declaring != null && !declaring.isInterface()) { declaring= declaring.getDeclaringClass(); } if (declaring != null) { final ASTNode node= ASTNodes.findDeclaration(binding, fSourceRewrite.getRoot()); if (node instanceof AbstractTypeDeclaration) { ModifierRewrite.create(fSourceRewrite.getASTRewrite(), node).setVisibility(Modifier.PUBLIC, null); } } }
Example 13
Source File: SrcTreeGenerator.java From sahagin-java with Apache License 2.0 | 5 votes |
@Override public boolean visit(FieldDeclaration node) { IVariableBinding variable = getVariableBinding(node); if (variable == null) { return super.visit(node); } ITypeBinding classBinding = variable.getDeclaringClass(); if (!classBinding.isClass() && !classBinding.isInterface()) { // enum method, etc return super.visit(node); } // TODO support additional testDoc for Field String testDoc = ASTUtils.getTestDoc(variable, locales); if (testDoc == null) { return super.visit(node); } TestClass testClass = classBindingTestClass(classBinding); TestField testField = new TestField(); testField.setTestClassKey(testClass.getKey()); testField.setTestClass(testClass); testField.setKey(testClass.getKey() + "." + variable.getName()); testField.setSimpleName(variable.getName()); testField.setTestDoc(testDoc); testField.setValue(null); // TODO currently not supported fieldTable.addTestField(testField); testClass.addTestFieldKey(testField.getKey()); testClass.addTestField(testField); return super.visit(node); }
Example 14
Source File: BindingLabelProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private static int getAdornmentFlags(IBinding binding) { int adornments= 0; final int modifiers= binding.getModifiers(); if (Modifier.isAbstract(modifiers)) adornments|= JavaElementImageDescriptor.ABSTRACT; if (Modifier.isFinal(modifiers)) adornments|= JavaElementImageDescriptor.FINAL; if (Modifier.isStatic(modifiers)) adornments|= JavaElementImageDescriptor.STATIC; if (binding.isDeprecated()) adornments|= JavaElementImageDescriptor.DEPRECATED; if (binding instanceof IMethodBinding) { if (((IMethodBinding) binding).isConstructor()) adornments|= JavaElementImageDescriptor.CONSTRUCTOR; if (Modifier.isSynchronized(modifiers)) adornments|= JavaElementImageDescriptor.SYNCHRONIZED; if (Modifier.isNative(modifiers)) adornments|= JavaElementImageDescriptor.NATIVE; ITypeBinding type= ((IMethodBinding) binding).getDeclaringClass(); if (type.isInterface() && !Modifier.isAbstract(modifiers) && !Modifier.isStatic(modifiers)) adornments|= JavaElementImageDescriptor.DEFAULT_METHOD; if (((IMethodBinding) binding).getDefaultValue() != null) adornments|= JavaElementImageDescriptor.ANNOTATION_DEFAULT; } if (binding instanceof IVariableBinding && ((IVariableBinding) binding).isField()) { if (Modifier.isTransient(modifiers)) adornments|= JavaElementImageDescriptor.TRANSIENT; if (Modifier.isVolatile(modifiers)) adornments|= JavaElementImageDescriptor.VOLATILE; } return adornments; }
Example 15
Source File: ASTNodes.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
public boolean visit(ITypeBinding type) { IMethodBinding[] methods= type.getDeclaredMethods(); for (int i= 0; i < methods.length; i++) { IMethodBinding candidate= methods[i]; if (candidate.getMethodDeclaration() == fOriginalMethod.getMethodDeclaration()) { continue; } ITypeBinding candidateDeclaringType= candidate.getDeclaringClass(); if (fDeclaringType != candidateDeclaringType) { int modifiers= candidate.getModifiers(); if (candidateDeclaringType.isInterface() && Modifier.isStatic(modifiers)) { continue; } if (Modifier.isPrivate(modifiers)) { continue; } } if (fOriginalMethod.getName().equals(candidate.getName()) && !fOriginalMethod.overrides(candidate)) { ITypeBinding[] originalParameterTypes= fOriginalMethod.getParameterTypes(); ITypeBinding[] candidateParameterTypes= candidate.getParameterTypes(); boolean couldBeAmbiguous; if (originalParameterTypes.length == candidateParameterTypes.length) { couldBeAmbiguous= true; } else if (fOriginalMethod.isVarargs() || candidate.isVarargs() ) { int candidateMinArgumentCount= candidateParameterTypes.length; if (candidate.isVarargs()) candidateMinArgumentCount--; couldBeAmbiguous= fArgumentCount >= candidateMinArgumentCount; } else { couldBeAmbiguous= false; } if (couldBeAmbiguous) { ITypeBinding parameterType= ASTResolving.getParameterTypeBinding(candidate, fArgIndex); if (parameterType != null && parameterType.getFunctionalInterfaceMethod() != null) { if (!fExpressionIsExplicitlyTyped) { /* According to JLS8 15.12.2.2, implicitly typed lambda expressions are not "pertinent to applicability" * and hence potentially applicable methods are always "applicable by strict invocation", * regardless of whether argument expressions are compatible with the method's parameter types or not. * If there are multiple such methods, 15.12.2.5 results in an ambiguous method invocation. */ return false; } /* Explicitly typed lambda expressions are pertinent to applicability, and hence * compatibility with the corresponding method parameter type is checked. And since this check * separates functional interface methods by their void-compatibility state, functional interfaces * with a different void compatibility are not applicable any more and hence can't cause * an ambiguous method invocation. */ ITypeBinding origParamType= ASTResolving.getParameterTypeBinding(fOriginalMethod, fArgIndex); boolean originalIsVoidCompatible= Bindings.isVoidType(origParamType.getFunctionalInterfaceMethod().getReturnType()); boolean candidateIsVoidCompatible= Bindings.isVoidType(parameterType.getFunctionalInterfaceMethod().getReturnType()); if (originalIsVoidCompatible == candidateIsVoidCompatible) { return false; } } } } } return true; }
Example 16
Source File: CallInliner.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * @param status the status * @return <code>true</code> if explicit cast is needed otherwise <code>false</code> */ private boolean needsExplicitCast(RefactoringStatus status) { // if the return type of the method is the same as the type of the // returned expression then we don't need an explicit cast. if (fSourceProvider.returnTypeMatchesReturnExpressions()) return false; List<Expression> returnExprs= fSourceProvider.getReturnExpressions(); // it is inferred that only methods consisting of a single // return statement can be inlined as parameters in other // method invocations if (returnExprs.size() != 1) return false; if (fTargetNode.getLocationInParent() == MethodInvocation.ARGUMENTS_PROPERTY) { MethodInvocation methodInvocation= (MethodInvocation)fTargetNode.getParent(); if(methodInvocation.getExpression() == fTargetNode) return false; IMethodBinding method= methodInvocation.resolveMethodBinding(); if (method == null) { status.addError(RefactoringCoreMessages.CallInliner_cast_analysis_error, JavaStatusContext.create(fCUnit, methodInvocation)); return false; } ITypeBinding[] parameters= method.getParameterTypes(); int argumentIndex= methodInvocation.arguments().indexOf(fInvocation); ITypeBinding parameterType= returnExprs.get(0).resolveTypeBinding(); if (method.isVarargs() && argumentIndex >= parameters.length - 1) { argumentIndex= parameters.length - 1; parameterType= parameterType.createArrayType(1); } parameters[argumentIndex]= parameterType; ITypeBinding type= ASTNodes.getReceiverTypeBinding(methodInvocation); TypeBindingVisitor visitor= new AmbiguousMethodAnalyzer( fTypeEnvironment, method, fTypeEnvironment.create(parameters)); if(!visitor.visit(type)) { return true; } else if (type.isInterface()) { return !Bindings.visitInterfaces(type, visitor); } else if (Modifier.isAbstract(type.getModifiers())) { return !Bindings.visitHierarchy(type, visitor); } else { // it is not needed to visit interfaces if receiver is a concrete class return !Bindings.visitSuperclasses(type, visitor); } } else { ITypeBinding explicitCast= ASTNodes.getExplicitCast(returnExprs.get(0), (Expression)fTargetNode); return explicitCast != null; } }
Example 17
Source File: UnresolvedElementsSubProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static void addNewMethodProposals(ICompilationUnit cu, CompilationUnit astRoot, Expression sender, List<Expression> arguments, boolean isSuperInvocation, ASTNode invocationNode, String methodName, Collection<ChangeCorrectionProposal> proposals) throws JavaModelException { ITypeBinding nodeParentType= Bindings.getBindingOfParentType(invocationNode); ITypeBinding binding= null; if (sender != null) { binding= sender.resolveTypeBinding(); } else { binding= nodeParentType; if (isSuperInvocation && binding != null) { binding= binding.getSuperclass(); } } if (binding != null && binding.isFromSource()) { ITypeBinding senderDeclBinding= binding.getTypeDeclaration(); ICompilationUnit targetCU= ASTResolving.findCompilationUnitForBinding(cu, astRoot, senderDeclBinding); if (targetCU != null) { String label; ITypeBinding[] parameterTypes= getParameterTypes(arguments); if (parameterTypes != null) { String sig = org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getMethodSignature(methodName, parameterTypes, false); boolean is18OrHigher= JavaModelUtil.is18OrHigher(targetCU.getJavaProject()); boolean isSenderBindingInterface= senderDeclBinding.isInterface(); if (nodeParentType == senderDeclBinding) { label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_description, sig); } else { label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, new Object[] { sig, BasicElementLabels.getJavaElementName(senderDeclBinding.getName()) } ); } if (is18OrHigher || !isSenderBindingInterface || (nodeParentType != senderDeclBinding && (!(sender instanceof SimpleName) || !((SimpleName) sender).getIdentifier().equals(senderDeclBinding.getName())))) { proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD)); } if (senderDeclBinding.isNested() && cu.equals(targetCU) && sender == null && Bindings.findMethodInHierarchy(senderDeclBinding, methodName, (ITypeBinding[]) null) == null) { // no covering method ASTNode anonymDecl= astRoot.findDeclaringNode(senderDeclBinding); if (anonymDecl != null) { senderDeclBinding= Bindings.getBindingOfParentType(anonymDecl.getParent()); isSenderBindingInterface= senderDeclBinding.isInterface(); if (!senderDeclBinding.isAnonymous()) { if (is18OrHigher || !isSenderBindingInterface) { String[] args = new String[] { sig, org.eclipse.jdt.ls.core.internal.corrections.ASTResolving.getTypeSignature(senderDeclBinding) }; label= Messages.format(CorrectionMessages.UnresolvedElementsSubProcessor_createmethod_other_description, args); proposals.add(new NewMethodCorrectionProposal(label, targetCU, invocationNode, arguments, senderDeclBinding, IProposalRelevance.CREATE_METHOD)); } } } } } } } }
Example 18
Source File: ExtractConstantRefactoring.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { try { pm.beginTask("", 7); //$NON-NLS-1$ RefactoringStatus result = Checks.validateEdit(fCu, getValidationContext(), pm); if (result.hasFatalError()) { return result; } pm.worked(1); if (fCuRewrite == null) { CompilationUnit cuNode = RefactoringASTParser.parseWithASTProvider(fCu, true, new SubProgressMonitor(pm, 3)); fCuRewrite = new CompilationUnitRewrite(null, fCu, cuNode, fFormatterOptions); } else { pm.worked(3); } result.merge(checkSelection(new SubProgressMonitor(pm, 3))); if (result.hasFatalError()) { return result; } if (isLiteralNodeSelected()) { fReplaceAllOccurrences = false; } if (isInTypeDeclarationAnnotation(getSelectedExpression().getAssociatedNode())) { fVisibility = JdtFlags.VISIBILITY_STRING_PACKAGE; } ITypeBinding targetType = getContainingTypeBinding(); if (targetType.isInterface()) { fTargetIsInterface = true; fVisibility = JdtFlags.VISIBILITY_STRING_PUBLIC; } return result; } finally { pm.done(); } }
Example 19
Source File: QuickAssistProcessor.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static boolean getAssignParamToFieldProposals(IInvocationContext context, ASTNode node, Collection<ChangeCorrectionProposal> resultingCollections) { node = ASTNodes.getNormalizedNode(node); ASTNode parent = node.getParent(); if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) { return false; } SingleVariableDeclaration paramDecl = (SingleVariableDeclaration) parent; IVariableBinding binding = paramDecl.resolveBinding(); MethodDeclaration methodDecl = (MethodDeclaration) parent.getParent(); if (binding == null || methodDecl.getBody() == null) { return false; } ITypeBinding typeBinding = binding.getType(); if (typeBinding == null) { return false; } if (resultingCollections == null) { return true; } ITypeBinding parentType = Bindings.getBindingOfParentType(node); if (parentType != null) { if (parentType.isInterface()) { return false; } // assign to existing fields CompilationUnit root = context.getASTRoot(); IVariableBinding[] declaredFields = parentType.getDeclaredFields(); boolean isStaticContext = ASTResolving.isInStaticContext(node); for (int i = 0; i < declaredFields.length; i++) { IVariableBinding curr = declaredFields[i]; if (isStaticContext == Modifier.isStatic(curr.getModifiers()) && typeBinding.isAssignmentCompatible(curr.getType())) { ASTNode fieldDeclFrag = root.findDeclaringNode(curr); if (fieldDeclFrag instanceof VariableDeclarationFragment) { VariableDeclarationFragment fragment = (VariableDeclarationFragment) fieldDeclFrag; if (fragment.getInitializer() == null) { resultingCollections.add(new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD)); } } } } } AssignToVariableAssistProposal fieldProposal = new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD); resultingCollections.add(fieldProposal); return true; }
Example 20
Source File: QuickAssistProcessor.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private static boolean getAssignParamToFieldProposals(IInvocationContext context, ASTNode node, Collection<ICommandAccess> resultingCollections) { node= ASTNodes.getNormalizedNode(node); ASTNode parent= node.getParent(); if (!(parent instanceof SingleVariableDeclaration) || !(parent.getParent() instanceof MethodDeclaration)) { return false; } SingleVariableDeclaration paramDecl= (SingleVariableDeclaration) parent; IVariableBinding binding= paramDecl.resolveBinding(); MethodDeclaration methodDecl= (MethodDeclaration) parent.getParent(); if (binding == null || methodDecl.getBody() == null) { return false; } ITypeBinding typeBinding= binding.getType(); if (typeBinding == null) { return false; } if (resultingCollections == null) { return true; } ITypeBinding parentType= Bindings.getBindingOfParentType(node); if (parentType != null) { if (parentType.isInterface()) { return false; } // assign to existing fields CompilationUnit root= context.getASTRoot(); IVariableBinding[] declaredFields= parentType.getDeclaredFields(); boolean isStaticContext= ASTResolving.isInStaticContext(node); for (int i= 0; i < declaredFields.length; i++) { IVariableBinding curr= declaredFields[i]; if (isStaticContext == Modifier.isStatic(curr.getModifiers()) && typeBinding.isAssignmentCompatible(curr.getType())) { ASTNode fieldDeclFrag= root.findDeclaringNode(curr); if (fieldDeclFrag instanceof VariableDeclarationFragment) { VariableDeclarationFragment fragment= (VariableDeclarationFragment) fieldDeclFrag; if (fragment.getInitializer() == null) { resultingCollections.add(new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, fragment, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_EXISTING_FIELD)); } } } } } AssignToVariableAssistProposal fieldProposal= new AssignToVariableAssistProposal(context.getCompilationUnit(), paramDecl, null, typeBinding, IProposalRelevance.ASSIGN_PARAM_TO_NEW_FIELD); fieldProposal.setCommandId(ASSIGN_PARAM_TO_FIELD_ID); resultingCollections.add(fieldProposal); return true; }