Search in sources :

Example 6 with IPackageBinding

use of org.eclipse.jdt.core.dom.IPackageBinding in project j2objc by google.

the class BindingConverter method getPackageElement.

/**
   * JDT package bindings do not include annotations, so add them from the
   * package's AST node.
   */
public static JdtPackageElement getPackageElement(PackageDeclaration pkg) {
    IPackageBinding binding = pkg.resolveBinding();
    JdtPackageElement pkgElement = (JdtPackageElement) getElement(binding);
    if (pkgElement.getAnnotationMirrors().isEmpty() && pkg.annotations().size() > 0) {
        for (Object modifier : pkg.annotations()) {
            IAnnotationBinding annotation = ((org.eclipse.jdt.core.dom.Annotation) modifier).resolveAnnotationBinding();
            pkgElement.addAnnotation(new JdtAnnotationMirror(annotation));
        }
    }
    return pkgElement;
}
Also used : IAnnotationBinding(org.eclipse.jdt.core.dom.IAnnotationBinding) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 7 with IPackageBinding

use of org.eclipse.jdt.core.dom.IPackageBinding in project che by eclipse.

the class BindingLabelProvider method getTypeLabel.

private static void getTypeLabel(ITypeBinding binding, long flags, StringBuffer buffer) {
    if ((flags & JavaElementLabels.T_FULLY_QUALIFIED) != 0) {
        final IPackageBinding pack = binding.getPackage();
        if (pack != null && !pack.isUnnamed()) {
            buffer.append(pack.getName());
            buffer.append('.');
        }
    }
    if ((flags & (JavaElementLabels.T_FULLY_QUALIFIED | JavaElementLabels.T_CONTAINER_QUALIFIED)) != 0) {
        final ITypeBinding declaring = binding.getDeclaringClass();
        if (declaring != null) {
            getTypeLabel(declaring, JavaElementLabels.T_CONTAINER_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer);
            buffer.append('.');
        }
        final IMethodBinding declaringMethod = binding.getDeclaringMethod();
        if (declaringMethod != null) {
            getMethodLabel(declaringMethod, 0, buffer);
            buffer.append('.');
        }
    }
    if (binding.isCapture()) {
        getTypeLabel(binding.getWildcard(), flags & JavaElementLabels.T_TYPE_PARAMETERS, buffer);
    } else if (binding.isWildcardType()) {
        buffer.append('?');
        ITypeBinding bound = binding.getBound();
        if (bound != null) {
            if (binding.isUpperbound()) {
                //$NON-NLS-1$
                buffer.append(" extends ");
            } else {
                //$NON-NLS-1$
                buffer.append(" super ");
            }
            getTypeLabel(bound, flags & JavaElementLabels.T_TYPE_PARAMETERS, buffer);
        }
    } else if (binding.isArray()) {
        getTypeLabel(binding.getElementType(), flags & JavaElementLabels.T_TYPE_PARAMETERS, buffer);
        appendDimensions(binding.getDimensions(), buffer);
    } else {
        // type variables, primitive, reftype
        String name = binding.getTypeDeclaration().getName();
        if (name.length() == 0) {
            if (binding.isEnum()) {
                buffer.append('{' + JavaElementLabels.ELLIPSIS_STRING + '}');
            } else if (binding.isAnonymous()) {
                ITypeBinding[] superInterfaces = binding.getInterfaces();
                ITypeBinding baseType;
                if (superInterfaces.length > 0) {
                    baseType = superInterfaces[0];
                } else {
                    baseType = binding.getSuperclass();
                }
                if (baseType != null) {
                    StringBuffer anonymBaseType = new StringBuffer();
                    getTypeLabel(baseType, flags & JavaElementLabels.T_TYPE_PARAMETERS, anonymBaseType);
                    buffer.append(Messages.format(JavaUIMessages.JavaElementLabels_anonym_type, anonymBaseType.toString()));
                } else {
                    buffer.append(JavaUIMessages.JavaElementLabels_anonym);
                }
            } else {
                //$NON-NLS-1$
                buffer.append("UNKNOWN");
            }
        } else {
            buffer.append(name);
        }
        if ((flags & JavaElementLabels.T_TYPE_PARAMETERS) != 0) {
            if (binding.isGenericType()) {
                getTypeParametersLabel(binding.getTypeParameters(), buffer);
            } else if (binding.isParameterizedType()) {
                getTypeArgumentsLabel(binding.getTypeArguments(), flags, buffer);
            }
        }
    }
    if ((flags & JavaElementLabels.T_POST_QUALIFIED) != 0) {
        final IMethodBinding declaringMethod = binding.getDeclaringMethod();
        final ITypeBinding declaringType = binding.getDeclaringClass();
        if (declaringMethod != null) {
            buffer.append(JavaElementLabels.CONCAT_STRING);
            getMethodLabel(declaringMethod, JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer);
        } else if (declaringType != null) {
            buffer.append(JavaElementLabels.CONCAT_STRING);
            getTypeLabel(declaringType, JavaElementLabels.T_FULLY_QUALIFIED | (flags & JavaElementLabels.P_COMPRESSED), buffer);
        } else {
            final IPackageBinding pack = binding.getPackage();
            if (pack != null && !pack.isUnnamed()) {
                buffer.append(JavaElementLabels.CONCAT_STRING);
                buffer.append(pack.getName());
            }
        }
    }
}
Also used : IMethodBinding(org.eclipse.jdt.core.dom.IMethodBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Example 8 with IPackageBinding

use of org.eclipse.jdt.core.dom.IPackageBinding in project che by eclipse.

the class StubUtility2 method createDelegationStub.

public static MethodDeclaration createDelegationStub(ICompilationUnit unit, ASTRewrite rewrite, ImportRewrite imports, ImportRewriteContext context, IMethodBinding delegate, IVariableBinding delegatingField, CodeGenerationSettings settings) throws CoreException {
    Assert.isNotNull(delegate);
    Assert.isNotNull(delegatingField);
    Assert.isNotNull(settings);
    AST ast = rewrite.getAST();
    MethodDeclaration decl = ast.newMethodDeclaration();
    decl.modifiers().addAll(ASTNodeFactory.newModifiers(ast, delegate.getModifiers() & ~Modifier.SYNCHRONIZED & ~Modifier.ABSTRACT & ~Modifier.NATIVE));
    decl.setName(ast.newSimpleName(delegate.getName()));
    decl.setConstructor(false);
    createTypeParameters(imports, context, ast, delegate, decl);
    decl.setReturnType2(imports.addImport(delegate.getReturnType(), ast, context));
    List<SingleVariableDeclaration> params = createParameters(unit.getJavaProject(), imports, context, ast, delegate, null, decl);
    createThrownExceptions(decl, delegate, imports, context, ast);
    Block body = ast.newBlock();
    decl.setBody(body);
    String delimiter = StubUtility.getLineDelimiterUsed(unit);
    Statement statement = null;
    MethodInvocation invocation = ast.newMethodInvocation();
    invocation.setName(ast.newSimpleName(delegate.getName()));
    List<Expression> arguments = invocation.arguments();
    for (int i = 0; i < params.size(); i++) arguments.add(ast.newSimpleName(params.get(i).getName().getIdentifier()));
    if (settings.useKeywordThis) {
        FieldAccess access = ast.newFieldAccess();
        access.setExpression(ast.newThisExpression());
        access.setName(ast.newSimpleName(delegatingField.getName()));
        invocation.setExpression(access);
    } else
        invocation.setExpression(ast.newSimpleName(delegatingField.getName()));
    if (delegate.getReturnType().isPrimitive() && delegate.getReturnType().getName().equals("void")) {
        //$NON-NLS-1$
        statement = ast.newExpressionStatement(invocation);
    } else {
        ReturnStatement returnStatement = ast.newReturnStatement();
        returnStatement.setExpression(invocation);
        statement = returnStatement;
    }
    body.statements().add(statement);
    ITypeBinding declaringType = delegatingField.getDeclaringClass();
    if (declaringType == null) {
        // can be null for
        return decl;
    }
    String qualifiedName = declaringType.getQualifiedName();
    IPackageBinding packageBinding = declaringType.getPackage();
    if (packageBinding != null) {
        if (packageBinding.getName().length() > 0 && qualifiedName.startsWith(packageBinding.getName()))
            qualifiedName = qualifiedName.substring(packageBinding.getName().length());
    }
    if (settings.createComments) {
        /*
			 * TODO: have API for delegate method comments This is an inlined
			 * version of
			 * {@link CodeGeneration#getMethodComment(ICompilationUnit, String, MethodDeclaration, IMethodBinding, String)}
			 */
        delegate = delegate.getMethodDeclaration();
        String declaringClassQualifiedName = delegate.getDeclaringClass().getQualifiedName();
        String linkToMethodName = delegate.getName();
        String[] parameterTypesQualifiedNames = StubUtility.getParameterTypeNamesForSeeTag(delegate);
        String string = StubUtility.getMethodComment(unit, qualifiedName, decl, delegate.isDeprecated(), linkToMethodName, declaringClassQualifiedName, parameterTypesQualifiedNames, true, delimiter);
        if (string != null) {
            Javadoc javadoc = (Javadoc) rewrite.createStringPlaceholder(string, ASTNode.JAVADOC);
            decl.setJavadoc(javadoc);
        }
    }
    return decl;
}
Also used : AST(org.eclipse.jdt.core.dom.AST) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) Statement(org.eclipse.jdt.core.dom.Statement) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Javadoc(org.eclipse.jdt.core.dom.Javadoc) MethodInvocation(org.eclipse.jdt.core.dom.MethodInvocation) SuperMethodInvocation(org.eclipse.jdt.core.dom.SuperMethodInvocation) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding) Expression(org.eclipse.jdt.core.dom.Expression) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ReturnStatement(org.eclipse.jdt.core.dom.ReturnStatement) Block(org.eclipse.jdt.core.dom.Block) FieldAccess(org.eclipse.jdt.core.dom.FieldAccess)

Example 9 with IPackageBinding

use of org.eclipse.jdt.core.dom.IPackageBinding in project che by eclipse.

the class UnresolvedElementsSubProcessor method addNewTypeProposals.

public static void addNewTypeProposals(ICompilationUnit cu, Name refNode, int kind, int relevance, Collection<ICommandAccess> proposals) throws CoreException {
    Name node = refNode;
    do {
        String typeName = ASTNodes.getSimpleNameIdentifier(node);
        Name qualifier = null;
        // only propose to create types for qualifiers when the name starts with upper case
        boolean isPossibleName = isLikelyTypeName(typeName) || node == refNode;
        if (isPossibleName) {
            IPackageFragment enclosingPackage = null;
            IType enclosingType = null;
            if (node.isSimpleName()) {
                enclosingPackage = (IPackageFragment) cu.getParent();
            // don't suggest member type, user can select it in wizard
            } else {
                Name qualifierName = ((QualifiedName) node).getQualifier();
                IBinding binding = qualifierName.resolveBinding();
                if (binding != null && binding.isRecovered()) {
                    binding = null;
                }
                if (binding instanceof ITypeBinding) {
                    enclosingType = (IType) binding.getJavaElement();
                } else if (binding instanceof IPackageBinding) {
                    qualifier = qualifierName;
                    enclosingPackage = (IPackageFragment) binding.getJavaElement();
                } else {
                    IJavaElement[] res = cu.codeSelect(qualifierName.getStartPosition(), qualifierName.getLength());
                    if (res != null && res.length > 0 && res[0] instanceof IType) {
                        enclosingType = (IType) res[0];
                    } else {
                        qualifier = qualifierName;
                        enclosingPackage = JavaModelUtil.getPackageFragmentRoot(cu).getPackageFragment(ASTResolving.getFullName(qualifierName));
                    }
                }
            }
            int rel = relevance;
            if (enclosingPackage != null && isLikelyPackageName(enclosingPackage.getElementName())) {
                rel += 3;
            }
            if (enclosingPackage != null && !enclosingPackage.getCompilationUnit(typeName + JavaModelUtil.DEFAULT_CU_SUFFIX).exists() || enclosingType != null && !enclosingType.isReadOnly() && !enclosingType.getType(typeName).exists()) {
                // new member type
                IJavaElement enclosing = enclosingPackage != null ? (IJavaElement) enclosingPackage : enclosingType;
                //TODO NewCUUsingWizardProposal
                if ((kind & SimilarElementsRequestor.CLASSES) != 0) {
                //						proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_CLASS, enclosing, rel+3));
                }
                if ((kind & SimilarElementsRequestor.INTERFACES) != 0) {
                //						proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_INTERFACE, enclosing, rel+2));
                }
                if ((kind & SimilarElementsRequestor.ENUMS) != 0) {
                //						proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ENUM, enclosing, rel));
                }
                if ((kind & SimilarElementsRequestor.ANNOTATIONS) != 0) {
                    //						proposals.add(new NewCUUsingWizardProposal(cu, node, NewCUUsingWizardProposal.K_ANNOTATION, enclosing, rel + 1));
                    addNullityAnnotationTypesProposals(cu, node, proposals);
                }
            }
        }
        node = qualifier;
    } while (node != null);
    // type parameter proposals
    if (refNode.isSimpleName() && (kind & SimilarElementsRequestor.VARIABLES) != 0) {
        CompilationUnit root = (CompilationUnit) refNode.getRoot();
        String name = ((SimpleName) refNode).getIdentifier();
        BodyDeclaration declaration = ASTResolving.findParentBodyDeclaration(refNode);
        int baseRel = relevance;
        if (isLikelyTypeParameterName(name)) {
            baseRel += 8;
        }
        while (declaration != null) {
            IBinding binding = null;
            int rel = baseRel;
            if (declaration instanceof MethodDeclaration) {
                binding = ((MethodDeclaration) declaration).resolveBinding();
                if (isLikelyMethodTypeParameterName(name))
                    rel += 2;
            } else if (declaration instanceof TypeDeclaration) {
                binding = ((TypeDeclaration) declaration).resolveBinding();
                rel++;
            }
            if (binding != null) {
                AddTypeParameterProposal proposal = new AddTypeParameterProposal(cu, binding, root, name, null, rel);
                proposals.add(proposal);
            }
            if (!Modifier.isStatic(declaration.getModifiers())) {
                declaration = ASTResolving.findParentBodyDeclaration(declaration.getParent());
            } else {
                declaration = null;
            }
        }
    }
}
Also used : CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) AddTypeParameterProposal(org.eclipse.jdt.internal.ui.text.correction.proposals.AddTypeParameterProposal) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) IBinding(org.eclipse.jdt.core.dom.IBinding) SimpleName(org.eclipse.jdt.core.dom.SimpleName) SimpleName(org.eclipse.jdt.core.dom.SimpleName) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) Name(org.eclipse.jdt.core.dom.Name) IType(org.eclipse.jdt.core.IType) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) TypeDeclaration(org.eclipse.jdt.core.dom.TypeDeclaration)

Example 10 with IPackageBinding

use of org.eclipse.jdt.core.dom.IPackageBinding in project xtext-eclipse by eclipse.

the class JdtBasedTypeFactory method createType.

private JvmDeclaredType createType(IType type, ITypeBinding binding) {
    // Maintain a string builder we pass along during recursion
    // that contains the prefix for the fully qualified name of the binding instance being traversed.
    // 
    StringBuilder fqn = new StringBuilder(100);
    IPackageBinding packageBinding = binding.getPackage();
    String packageName = null;
    if (packageBinding != null && !packageBinding.isUnnamed()) {
        packageName = packageBinding.getName();
        fqn.append(packageBinding.getName());
        fqn.append('.');
    }
    JvmDeclaredType result = createType(binding, type.getHandleIdentifier(), Lists.<String>newArrayList(), fqn);
    result.setPackageName(packageName);
    return result;
}
Also used : JvmDeclaredType(org.eclipse.xtext.common.types.JvmDeclaredType) IPackageBinding(org.eclipse.jdt.core.dom.IPackageBinding)

Aggregations

IPackageBinding (org.eclipse.jdt.core.dom.IPackageBinding)11 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)7 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)4 IJavaElement (org.eclipse.jdt.core.IJavaElement)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 UnhandledException (org.autorefactor.util.UnhandledException)2 CoreException (org.eclipse.core.runtime.CoreException)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)2 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)2 SearchEngine (org.eclipse.jdt.core.search.SearchEngine)2 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)2 SearchRequestor (org.eclipse.jdt.core.search.SearchRequestor)2 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)1 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)1 IType (org.eclipse.jdt.core.IType)1 AST (org.eclipse.jdt.core.dom.AST)1 ASTNode (org.eclipse.jdt.core.dom.ASTNode)1 Annotation (org.eclipse.jdt.core.dom.Annotation)1 Block (org.eclipse.jdt.core.dom.Block)1