Search in sources :

Example 11 with TypeParameter

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

the class JavadocTagsSubProcessor method getPreviousTypeParamNames.

public static Set<String> getPreviousTypeParamNames(List<TypeParameter> typeParams, ASTNode missingNode) {
    Set<String> previousNames = new HashSet<String>();
    for (int i = 0; i < typeParams.size(); i++) {
        TypeParameter curr = typeParams.get(i);
        if (curr == missingNode) {
            return previousNames;
        }
        previousNames.add('<' + curr.getName().getIdentifier() + '>');
    }
    return previousNames;
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) HashSet(java.util.HashSet)

Example 12 with TypeParameter

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

the class TypeContextChecker method appendTypeParameters.

private static void appendTypeParameters(StringBuffer buf, List<TypeParameter> typeParameters) {
    int typeParametersCount = typeParameters.size();
    if (typeParametersCount > 0) {
        buf.append('<');
        for (int i = 0; i < typeParametersCount; i++) {
            TypeParameter typeParameter = typeParameters.get(i);
            buf.append(ASTNodes.asString(typeParameter));
            if (i < typeParametersCount - 1)
                buf.append(',');
        }
        buf.append('>');
    }
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) ITypeParameter(org.eclipse.jdt.core.ITypeParameter)

Example 13 with TypeParameter

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

the class ExtractMethodRefactoring method createNewMethodDeclaration.

private MethodDeclaration createNewMethodDeclaration() {
    MethodDeclaration result = fAST.newMethodDeclaration();
    int modifiers = fVisibility;
    BodyDeclaration enclosingBodyDeclaration = fAnalyzer.getEnclosingBodyDeclaration();
    boolean isDestinationInterface = isDestinationInterface();
    if (isDestinationInterface && !(enclosingBodyDeclaration instanceof MethodDeclaration && enclosingBodyDeclaration.getParent() == fDestination && Modifier.isPublic(enclosingBodyDeclaration.getModifiers()))) {
        modifiers = Modifier.NONE;
    }
    boolean shouldBeStatic = false;
    ASTNode currentParent = enclosingBodyDeclaration;
    do {
        if (currentParent instanceof BodyDeclaration) {
            shouldBeStatic = shouldBeStatic || JdtFlags.isStatic((BodyDeclaration) currentParent);
        }
        currentParent = currentParent.getParent();
    } while (!shouldBeStatic && currentParent != null && currentParent != fDestination);
    if (shouldBeStatic || fAnalyzer.getForceStatic() || forceStatic()) {
        modifiers |= Modifier.STATIC;
    } else if (isDestinationInterface) {
        modifiers |= Modifier.DEFAULT;
    }
    ITypeBinding[] typeVariables = computeLocalTypeVariables(modifiers);
    List<TypeParameter> typeParameters = result.typeParameters();
    for (int i = 0; i < typeVariables.length; i++) {
        TypeParameter parameter = fAST.newTypeParameter();
        parameter.setName(fAST.newSimpleName(typeVariables[i].getName()));
        ITypeBinding[] bounds = typeVariables[i].getTypeBounds();
        for (int j = 0; j < bounds.length; j++) if (//$NON-NLS-1$
        !"java.lang.Object".equals(bounds[j].getQualifiedName()))
            parameter.typeBounds().add(fImportRewriter.addImport(bounds[j], fAST));
        typeParameters.add(parameter);
    }
    result.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, modifiers));
    result.setReturnType2((Type) ASTNode.copySubtree(fAST, fAnalyzer.getReturnType()));
    result.setName(fAST.newSimpleName(fMethodName));
    ImportRewriteContext context = new ContextSensitiveImportRewriteContext(enclosingBodyDeclaration, fImportRewriter);
    List<SingleVariableDeclaration> parameters = result.parameters();
    for (int i = 0; i < fParameterInfos.size(); i++) {
        ParameterInfo info = fParameterInfos.get(i);
        VariableDeclaration infoDecl = getVariableDeclaration(info);
        SingleVariableDeclaration parameter = fAST.newSingleVariableDeclaration();
        parameter.modifiers().addAll(ASTNodeFactory.newModifiers(fAST, ASTNodes.getModifiers(infoDecl)));
        parameter.setType(ASTNodeFactory.newType(fAST, infoDecl, fImportRewriter, context));
        parameter.setName(fAST.newSimpleName(info.getNewName()));
        parameter.setVarargs(info.isNewVarargs());
        parameters.add(parameter);
    }
    List<Type> exceptions = result.thrownExceptionTypes();
    ITypeBinding[] exceptionTypes = fAnalyzer.getExceptions(fThrowRuntimeExceptions);
    for (int i = 0; i < exceptionTypes.length; i++) {
        ITypeBinding exceptionType = exceptionTypes[i];
        exceptions.add(fImportRewriter.addImport(exceptionType, fAST, context));
    }
    return result;
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) MethodDeclaration(org.eclipse.jdt.core.dom.MethodDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration) ParameterInfo(org.eclipse.jdt.internal.corext.refactoring.ParameterInfo) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) Type(org.eclipse.jdt.core.dom.Type) ImportRewriteContext(org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext) ContextSensitiveImportRewriteContext(org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding) ASTNode(org.eclipse.jdt.core.dom.ASTNode) BodyDeclaration(org.eclipse.jdt.core.dom.BodyDeclaration) VariableDeclaration(org.eclipse.jdt.core.dom.VariableDeclaration) SingleVariableDeclaration(org.eclipse.jdt.core.dom.SingleVariableDeclaration)

Example 14 with TypeParameter

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

the class IntroduceFactoryRefactoring method copyTypeParameters.

/**
	 * Copies the constructor's parent type's type parameters, if any, as
	 * method type parameters of the new static factory method. (Recall
	 * that static methods can't refer to type arguments of the enclosing
	 * class, since they have no instance to serve as a context.)<br>
	 * Makes sure to copy the bounds from the owning type, to ensure that the
	 * return type of the factory method satisfies the bounds of the type
	 * being instantiated.<br>
	 * E.g., for ctor Foo() in the type Foo<T extends Number>, be sure that
	 * the factory method is declared as<br>
	 * <code>static <T extends Number> Foo<T> createFoo()</code><br>
	 * and not simply<br>
	 * <code>static <T> Foo<T> createFoo()</code><br>
	 * or the compiler will bark.
	 * @param ast utility object needed to create ASTNode's for the new method
	 * @param newMethod the method onto which to copy the type parameters
	 */
private void copyTypeParameters(AST ast, MethodDeclaration newMethod) {
    ITypeBinding[] ctorOwnerTypeParms = fCtorBinding.getDeclaringClass().getTypeParameters();
    List<TypeParameter> factoryMethodTypeParms = newMethod.typeParameters();
    for (int i = 0; i < ctorOwnerTypeParms.length; i++) {
        TypeParameter newParm = ast.newTypeParameter();
        ITypeBinding[] parmTypeBounds = ctorOwnerTypeParms[i].getTypeBounds();
        List<Type> newParmBounds = newParm.typeBounds();
        newParm.setName(ast.newSimpleName(ctorOwnerTypeParms[i].getName()));
        for (int b = 0; b < parmTypeBounds.length; b++) {
            if (parmTypeBounds[b].isClass() && parmTypeBounds[b].getSuperclass() == null)
                continue;
            Type newBound = fImportRewriter.addImport(parmTypeBounds[b], ast);
            newParmBounds.add(newBound);
        }
        factoryMethodTypeParms.add(newParm);
    }
}
Also used : TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) IType(org.eclipse.jdt.core.IType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) Type(org.eclipse.jdt.core.dom.Type) ITypeBinding(org.eclipse.jdt.core.dom.ITypeBinding)

Example 15 with TypeParameter

use of org.eclipse.jdt.core.dom.TypeParameter in project flux by eclipse.

the class ASTResolving method internalGetPossibleTypeKinds.

private static int internalGetPossibleTypeKinds(ASTNode node) {
    int kind = SimilarElementsRequestor.ALL_TYPES;
    int mask = SimilarElementsRequestor.ALL_TYPES | SimilarElementsRequestor.VOIDTYPE;
    ASTNode parent = node.getParent();
    while (parent instanceof QualifiedName) {
        if (node.getLocationInParent() == QualifiedName.QUALIFIER_PROPERTY) {
            return SimilarElementsRequestor.REF_TYPES;
        }
        node = parent;
        parent = parent.getParent();
        mask = SimilarElementsRequestor.REF_TYPES;
    }
    while (parent instanceof Type) {
        if (parent instanceof QualifiedType) {
            if (node.getLocationInParent() == QualifiedType.QUALIFIER_PROPERTY) {
                return mask & (SimilarElementsRequestor.REF_TYPES);
            }
            mask &= SimilarElementsRequestor.REF_TYPES;
        } else if (parent instanceof NameQualifiedType) {
            if (node.getLocationInParent() == NameQualifiedType.QUALIFIER_PROPERTY) {
                return mask & (SimilarElementsRequestor.REF_TYPES);
            }
            mask &= SimilarElementsRequestor.REF_TYPES;
        } else if (parent instanceof ParameterizedType) {
            if (node.getLocationInParent() == ParameterizedType.TYPE_ARGUMENTS_PROPERTY) {
                return mask & SimilarElementsRequestor.REF_TYPES_AND_VAR;
            }
            mask &= SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
        } else if (parent instanceof WildcardType) {
            if (node.getLocationInParent() == WildcardType.BOUND_PROPERTY) {
                return mask & SimilarElementsRequestor.REF_TYPES_AND_VAR;
            }
        }
        node = parent;
        parent = parent.getParent();
    }
    switch(parent.getNodeType()) {
        case ASTNode.TYPE_DECLARATION:
            if (node.getLocationInParent() == TypeDeclaration.SUPER_INTERFACE_TYPES_PROPERTY) {
                kind = SimilarElementsRequestor.INTERFACES;
            } else if (node.getLocationInParent() == TypeDeclaration.SUPERCLASS_TYPE_PROPERTY) {
                kind = SimilarElementsRequestor.CLASSES;
            }
            break;
        case ASTNode.ENUM_DECLARATION:
            kind = SimilarElementsRequestor.INTERFACES;
            break;
        case ASTNode.METHOD_DECLARATION:
            if (node.getLocationInParent() == MethodDeclaration.THROWN_EXCEPTION_TYPES_PROPERTY) {
                kind = SimilarElementsRequestor.CLASSES;
            } else if (node.getLocationInParent() == MethodDeclaration.RETURN_TYPE2_PROPERTY) {
                kind = SimilarElementsRequestor.ALL_TYPES | SimilarElementsRequestor.VOIDTYPE;
            }
            break;
        case ASTNode.ANNOTATION_TYPE_MEMBER_DECLARATION:
            kind = SimilarElementsRequestor.PRIMITIVETYPES | SimilarElementsRequestor.ANNOTATIONS | SimilarElementsRequestor.ENUMS;
            break;
        case ASTNode.INSTANCEOF_EXPRESSION:
            kind = SimilarElementsRequestor.REF_TYPES;
            break;
        case ASTNode.THROW_STATEMENT:
            kind = SimilarElementsRequestor.CLASSES;
            break;
        case ASTNode.CLASS_INSTANCE_CREATION:
            if (((ClassInstanceCreation) parent).getAnonymousClassDeclaration() == null) {
                kind = SimilarElementsRequestor.CLASSES;
            } else {
                kind = SimilarElementsRequestor.CLASSES | SimilarElementsRequestor.INTERFACES;
            }
            break;
        case ASTNode.SINGLE_VARIABLE_DECLARATION:
            int superParent = parent.getParent().getNodeType();
            if (superParent == ASTNode.CATCH_CLAUSE) {
                kind = SimilarElementsRequestor.CLASSES;
            } else if (superParent == ASTNode.ENHANCED_FOR_STATEMENT) {
                kind = SimilarElementsRequestor.REF_TYPES;
            }
            break;
        case ASTNode.TAG_ELEMENT:
            kind = SimilarElementsRequestor.REF_TYPES;
            break;
        case ASTNode.MARKER_ANNOTATION:
        case ASTNode.SINGLE_MEMBER_ANNOTATION:
        case ASTNode.NORMAL_ANNOTATION:
            kind = SimilarElementsRequestor.ANNOTATIONS;
            break;
        case ASTNode.TYPE_PARAMETER:
            if (((TypeParameter) parent).typeBounds().indexOf(node) > 0) {
                kind = SimilarElementsRequestor.INTERFACES;
            } else {
                kind = SimilarElementsRequestor.REF_TYPES_AND_VAR;
            }
            break;
        case ASTNode.TYPE_LITERAL:
            kind = SimilarElementsRequestor.REF_TYPES;
            break;
        default:
    }
    return kind & mask;
}
Also used : ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) ArrayType(org.eclipse.jdt.core.dom.ArrayType) ParameterizedType(org.eclipse.jdt.core.dom.ParameterizedType) SimpleType(org.eclipse.jdt.core.dom.SimpleType) Type(org.eclipse.jdt.core.dom.Type) WildcardType(org.eclipse.jdt.core.dom.WildcardType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) PrimitiveType(org.eclipse.jdt.core.dom.PrimitiveType) WildcardType(org.eclipse.jdt.core.dom.WildcardType) TypeParameter(org.eclipse.jdt.core.dom.TypeParameter) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType) QualifiedType(org.eclipse.jdt.core.dom.QualifiedType) QualifiedName(org.eclipse.jdt.core.dom.QualifiedName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) NameQualifiedType(org.eclipse.jdt.core.dom.NameQualifiedType)

Aggregations

TypeParameter (org.eclipse.jdt.core.dom.TypeParameter)16 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)6 MethodDeclaration (org.eclipse.jdt.core.dom.MethodDeclaration)6 ASTNode (org.eclipse.jdt.core.dom.ASTNode)5 Type (org.eclipse.jdt.core.dom.Type)5 TypeDeclaration (org.eclipse.jdt.core.dom.TypeDeclaration)4 ITypeParameter (org.eclipse.jdt.core.ITypeParameter)3 AbstractTypeDeclaration (org.eclipse.jdt.core.dom.AbstractTypeDeclaration)3 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)3 SingleVariableDeclaration (org.eclipse.jdt.core.dom.SingleVariableDeclaration)3 ArrayList (java.util.ArrayList)2 CoreException (org.eclipse.core.runtime.CoreException)2 AST (org.eclipse.jdt.core.dom.AST)2 ArrayType (org.eclipse.jdt.core.dom.ArrayType)2 BodyDeclaration (org.eclipse.jdt.core.dom.BodyDeclaration)2 Javadoc (org.eclipse.jdt.core.dom.Javadoc)2 ParameterizedType (org.eclipse.jdt.core.dom.ParameterizedType)2 PrimitiveType (org.eclipse.jdt.core.dom.PrimitiveType)2 ImportRewriteContext (org.eclipse.jdt.core.dom.rewrite.ImportRewrite.ImportRewriteContext)2 ContextSensitiveImportRewriteContext (org.eclipse.jdt.internal.corext.codemanipulation.ContextSensitiveImportRewriteContext)2