Example usage for org.eclipse.jdt.internal.compiler.ast TypeReference getParameterizedTypeName

List of usage examples for org.eclipse.jdt.internal.compiler.ast TypeReference getParameterizedTypeName

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.compiler.ast TypeReference getParameterizedTypeName.

Prototype

public char[][] getParameterizedTypeName() 

Source Link

Usage

From source file:com.codenvy.ide.ext.java.server.internal.core.search.matching.MatchLocator.java

License:Open Source License

/**
 * Creates an IMethod from the given method declaration and type.
 *//* ww  w . jav  a2 s .  c  om*/
protected IJavaElement createHandle(AbstractMethodDeclaration method, IJavaElement parent) {
    if (!(parent instanceof IType))
        return parent;

    IType type = (IType) parent;
    Argument[] arguments = method.arguments;
    int argCount = arguments == null ? 0 : arguments.length;
    if (type.isBinary()) {
        // don't cache the methods of the binary type
        // fall thru if its a constructor with a synthetic argument... find it the slower way
        ClassFileReader reader = classFileReader(type);
        if (reader != null) {
            // build arguments names
            boolean firstIsSynthetic = false;
            if (reader.isMember() && method.isConstructor() && !Flags.isStatic(reader.getModifiers())) { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=48261
                firstIsSynthetic = true;
                argCount++;
            }
            char[][] argumentTypeNames = new char[argCount][];
            for (int i = 0; i < argCount; i++) {
                char[] typeName = null;
                if (i == 0 && firstIsSynthetic) {
                    typeName = type.getDeclaringType().getFullyQualifiedName().toCharArray();
                } else if (arguments != null) {
                    TypeReference typeRef = arguments[firstIsSynthetic ? i - 1 : i].type;
                    typeName = CharOperation.concatWith(typeRef.getTypeName(), '.');
                    for (int k = 0, dim = typeRef.dimensions(); k < dim; k++)
                        typeName = CharOperation.concat(typeName, new char[] { '[', ']' });
                }
                if (typeName == null) {
                    // invalid type name
                    return null;
                }
                argumentTypeNames[i] = typeName;
            }
            // return binary method
            IMethod binaryMethod = createBinaryMethodHandle(type, method.selector, argumentTypeNames);
            if (binaryMethod == null) {
                // when first attempt fails, try with similar matches if any...
                PossibleMatch similarMatch = this.currentPossibleMatch.getSimilarMatch();
                while (similarMatch != null) {
                    type = ((ClassFile) similarMatch.openable).getType();
                    binaryMethod = createBinaryMethodHandle(type, method.selector, argumentTypeNames);
                    if (binaryMethod != null) {
                        return binaryMethod;
                    }
                    similarMatch = similarMatch.getSimilarMatch();
                }
            }
            return binaryMethod;
        }
        if (BasicSearchEngine.VERBOSE) {
            System.out.println("Not able to createHandle for the method " + //$NON-NLS-1$
                    CharOperation.charToString(method.selector) + " May miss some results"); //$NON-NLS-1$
        }
        return null;
    }

    String[] parameterTypeSignatures = new String[argCount];
    if (arguments != null) {
        for (int i = 0; i < argCount; i++) {
            TypeReference typeRef = arguments[i].type;
            char[] typeName = CharOperation.concatWith(typeRef.getParameterizedTypeName(), '.');
            parameterTypeSignatures[i] = Signature.createTypeSignature(typeName, false);
        }
    }

    return createMethodHandle(type, new String(method.selector), parameterTypeSignatures);
}

From source file:com.codenvy.ide.ext.java.server.internal.core.util.Util.java

License:Open Source License

public static String typeSignature(TypeReference type) {
    String signature = null;// w  w w .  j  a v  a 2 s .  c  om
    if ((type.bits & ASTNode.IsUnionType) != 0) {
        // special treatment for union type reference
        UnionTypeReference unionTypeReference = (UnionTypeReference) type;
        TypeReference[] typeReferences = unionTypeReference.typeReferences;
        int length = typeReferences.length;
        String[] typeSignatures = new String[length];
        for (int i = 0; i < length; i++) {
            char[][] compoundName = typeReferences[i].getParameterizedTypeName();
            char[] typeName = CharOperation.concatWith(compoundName, '.');
            typeSignatures[i] = Signature.createTypeSignature(typeName, false/*
                                                                             * don 't resolve
                                                                             */);
        }
        signature = Signature.createIntersectionTypeSignature(typeSignatures);
    } else {
        char[][] compoundName = type.getParameterizedTypeName();
        char[] typeName = CharOperation.concatWith(compoundName, '.');
        signature = Signature.createTypeSignature(typeName, false/*
                                                                 * don't resolve
                                                                 */);
    }
    return signature;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementNotifier.java

License:Open Source License

protected char[] getSuperclassName(TypeDeclaration typeDeclaration) {
    TypeReference superclass = typeDeclaration.superclass;
    return superclass != null ? CharOperation.concatWith(superclass.getParameterizedTypeName(), '.') : null;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementNotifier.java

License:Open Source License

protected char[][] getTypeParameterBounds(TypeParameter typeParameter) {
    TypeReference firstBound = typeParameter.type;
    TypeReference[] otherBounds = typeParameter.bounds;
    char[][] typeParameterBounds = null;
    if (firstBound != null) {
        if (otherBounds != null) {
            int otherBoundsLength = otherBounds.length;
            char[][] boundNames = new char[otherBoundsLength + 1][];
            boundNames[0] = CharOperation.concatWith(firstBound.getParameterizedTypeName(), '.');
            for (int j = 0; j < otherBoundsLength; j++) {
                boundNames[j + 1] = CharOperation.concatWith(otherBounds[j].getParameterizedTypeName(), '.');
            }/*  www . ja va  2  s.c  o  m*/
            typeParameterBounds = boundNames;
        } else {
            typeParameterBounds = new char[][] {
                    CharOperation.concatWith(firstBound.getParameterizedTypeName(), '.') };
        }
    } else {
        typeParameterBounds = CharOperation.NO_CHAR_CHAR;
    }

    return typeParameterBounds;
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementNotifier.java

License:Open Source License

protected void notifySourceElementRequestor(AbstractMethodDeclaration methodDeclaration) {

    // range check
    boolean isInRange = initialPosition <= methodDeclaration.declarationSourceStart
            && eofPosition >= methodDeclaration.declarationSourceEnd;

    if (methodDeclaration.isClinit()) {
        this.visitIfNeeded(methodDeclaration);
        return;/*from   w  w  w .jav a2  s .  c om*/
    }

    if (methodDeclaration.isDefaultConstructor()) {
        if (reportReferenceInfo) {
            ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) methodDeclaration;
            ExplicitConstructorCall constructorCall = constructorDeclaration.constructorCall;
            if (constructorCall != null) {
                switch (constructorCall.accessMode) {
                case ExplicitConstructorCall.This:
                    requestor.acceptConstructorReference(typeNames[nestedTypeIndex - 1],
                            constructorCall.arguments == null ? 0 : constructorCall.arguments.length,
                            constructorCall.sourceStart);
                    break;
                case ExplicitConstructorCall.Super:
                case ExplicitConstructorCall.ImplicitSuper:
                    requestor.acceptConstructorReference(superTypeNames[nestedTypeIndex - 1],
                            constructorCall.arguments == null ? 0 : constructorCall.arguments.length,
                            constructorCall.sourceStart);
                    break;
                }
            }
        }
        return;
    }
    char[][] argumentTypes = null;
    char[][] argumentNames = null;
    boolean isVarArgs = false;
    Argument[] arguments = methodDeclaration.arguments;
    if (arguments != null) {
        char[][][] argumentTypesAndNames = this.getArguments(arguments);
        argumentTypes = argumentTypesAndNames[0];
        argumentNames = argumentTypesAndNames[1];

        isVarArgs = arguments[arguments.length - 1].isVarArgs();
    }
    char[][] thrownExceptionTypes = getThrownExceptions(methodDeclaration);
    // by default no selector end position
    int selectorSourceEnd = -1;
    if (methodDeclaration.isConstructor()) {
        selectorSourceEnd = this.sourceEnds.get(methodDeclaration);
        if (isInRange) {
            int currentModifiers = methodDeclaration.modifiers;
            if (isVarArgs)
                currentModifiers |= ClassFileConstants.AccVarargs;

            // remember deprecation so as to not lose it below
            boolean deprecated = (currentModifiers & ClassFileConstants.AccDeprecated) != 0
                    || hasDeprecatedAnnotation(methodDeclaration.annotations);

            ISourceElementRequestor.MethodInfo methodInfo = new ISourceElementRequestor.MethodInfo();
            methodInfo.isConstructor = true;
            methodInfo.declarationStart = methodDeclaration.declarationSourceStart;
            methodInfo.modifiers = deprecated
                    ? (currentModifiers & ExtraCompilerModifiers.AccJustFlag) | ClassFileConstants.AccDeprecated
                    : currentModifiers & ExtraCompilerModifiers.AccJustFlag;
            methodInfo.name = methodDeclaration.selector;
            methodInfo.nameSourceStart = methodDeclaration.sourceStart;
            methodInfo.nameSourceEnd = selectorSourceEnd;
            methodInfo.parameterTypes = argumentTypes;
            methodInfo.parameterNames = argumentNames;
            methodInfo.exceptionTypes = thrownExceptionTypes;
            methodInfo.typeParameters = getTypeParameterInfos(methodDeclaration.typeParameters());
            methodInfo.categories = (char[][]) this.nodesToCategories.get(methodDeclaration);
            methodInfo.annotations = methodDeclaration.annotations;
            methodInfo.node = methodDeclaration;
            requestor.enterConstructor(methodInfo);
        }
        if (reportReferenceInfo) {
            ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) methodDeclaration;
            ExplicitConstructorCall constructorCall = constructorDeclaration.constructorCall;
            if (constructorCall != null) {
                switch (constructorCall.accessMode) {
                case ExplicitConstructorCall.This:
                    requestor.acceptConstructorReference(typeNames[nestedTypeIndex - 1],
                            constructorCall.arguments == null ? 0 : constructorCall.arguments.length,
                            constructorCall.sourceStart);
                    break;
                case ExplicitConstructorCall.Super:
                case ExplicitConstructorCall.ImplicitSuper:
                    requestor.acceptConstructorReference(superTypeNames[nestedTypeIndex - 1],
                            constructorCall.arguments == null ? 0 : constructorCall.arguments.length,
                            constructorCall.sourceStart);
                    break;
                }
            }
        }
        this.visitIfNeeded(methodDeclaration);
        if (isInRange) {
            requestor.exitConstructor(methodDeclaration.declarationSourceEnd);
        }
        return;
    }
    selectorSourceEnd = this.sourceEnds.get(methodDeclaration);

    // AspectJ Change Begin
    // recreate source locations for Pointcuts and Advice
    org.aspectj.org.eclipse.jdt.internal.compiler.ast.MethodDeclaration ajmDec = new org.aspectj.org.eclipse.jdt.internal.compiler.ast.MethodDeclaration(
            new org.aspectj.org.eclipse.jdt.internal.compiler.CompilationResult(
                    methodDeclaration.compilationResult.fileName, methodDeclaration.compilationResult.unitIndex,
                    methodDeclaration.compilationResult.totalUnitsKnown, 500));
    if (ajmDec instanceof PointcutDeclaration) {
        selectorSourceEnd = methodDeclaration.sourceStart + methodDeclaration.selector.length - 1;
    }
    if (ajmDec instanceof AdviceDeclaration) {
        selectorSourceEnd = methodDeclaration.sourceStart + ((AdviceDeclaration) ajmDec).kind.getName().length()
                - 1;
    }
    // AspectJ Change End

    if (isInRange) {
        int currentModifiers = methodDeclaration.modifiers;
        if (isVarArgs)
            currentModifiers |= ClassFileConstants.AccVarargs;

        // remember deprecation so as to not lose it below
        boolean deprecated = (currentModifiers & ClassFileConstants.AccDeprecated) != 0
                || hasDeprecatedAnnotation(methodDeclaration.annotations);

        TypeReference returnType = methodDeclaration instanceof MethodDeclaration
                ? ((MethodDeclaration) methodDeclaration).returnType
                : null;
        ISourceElementRequestor.MethodInfo methodInfo = new ISourceElementRequestor.MethodInfo();
        methodInfo.isAnnotation = methodDeclaration instanceof AnnotationMethodDeclaration;
        methodInfo.declarationStart = methodDeclaration.declarationSourceStart;
        methodInfo.modifiers = deprecated
                ? (currentModifiers & ExtraCompilerModifiers.AccJustFlag) | ClassFileConstants.AccDeprecated
                : currentModifiers & ExtraCompilerModifiers.AccJustFlag;
        methodInfo.returnType = returnType == null ? null
                : CharOperation.concatWith(returnType.getParameterizedTypeName(), '.');
        methodInfo.name = methodDeclaration.selector;
        methodInfo.nameSourceStart = methodDeclaration.sourceStart;
        methodInfo.nameSourceEnd = selectorSourceEnd;
        methodInfo.parameterTypes = argumentTypes;
        methodInfo.parameterNames = argumentNames;
        methodInfo.exceptionTypes = thrownExceptionTypes;
        methodInfo.typeParameters = getTypeParameterInfos(methodDeclaration.typeParameters());
        methodInfo.categories = (char[][]) this.nodesToCategories.get(methodDeclaration);
        methodInfo.annotations = methodDeclaration.annotations;
        methodInfo.node = methodDeclaration;
        requestor.enterMethod(methodInfo);
    }

    this.visitIfNeeded(methodDeclaration);

    if (isInRange) {
        if (methodDeclaration instanceof AnnotationMethodDeclaration) {
            AnnotationMethodDeclaration annotationMethodDeclaration = (AnnotationMethodDeclaration) methodDeclaration;
            Expression expression = annotationMethodDeclaration.defaultValue;
            if (expression != null) {
                requestor.exitMethod(methodDeclaration.declarationSourceEnd, expression);
                return;
            }
        }
        requestor.exitMethod(methodDeclaration.declarationSourceEnd,
                (org.aspectj.org.eclipse.jdt.internal.compiler.ast.Expression) null);
    }
}

From source file:org.eclipse.ajdt.core.parserbridge.AJSourceElementParser2.java

License:Open Source License

public TypeReference getTypeReference(int dim) {
    /* build a Reference on a variable that may be qualified or not
     * This variable is a type reference and dim will be its dimensions
     *///ww w  .j  a va 2s. c o  m
    int length = identifierLengthStack[identifierLengthPtr--];
    if (length < 0) { //flag for precompiled type reference on base types
        TypeReference ref = TypeReference.baseTypeReference(-length, dim);
        ref.sourceStart = intStack[intPtr--];
        if (dim == 0) {
            ref.sourceEnd = intStack[intPtr--];
        } else {
            intPtr--; // no need to use this position as it is an array
            ref.sourceEnd = endPosition;
        }
        if (reportReferenceInfo) {
            requestor.acceptTypeReference(ref.getParameterizedTypeName(), ref.sourceStart, ref.sourceEnd);
        }
        return ref;
    } else {
        int numberOfIdentifiers = this.genericsIdentifiersLengthStack[this.genericsIdentifiersLengthPtr--];
        if (length != numberOfIdentifiers || this.genericsLengthStack[this.genericsLengthPtr] != 0) {
            // generic type
            TypeReference ref = getTypeReferenceForGenericType(dim, length, numberOfIdentifiers);
            if (reportReferenceInfo) {
                if (length == 1 && numberOfIdentifiers == 1) {
                    ParameterizedSingleTypeReference parameterizedSingleTypeReference = (ParameterizedSingleTypeReference) ref;
                    requestor.acceptTypeReference(parameterizedSingleTypeReference.token,
                            parameterizedSingleTypeReference.sourceStart);
                } else {
                    ParameterizedQualifiedTypeReference parameterizedQualifiedTypeReference = (ParameterizedQualifiedTypeReference) ref;
                    requestor.acceptTypeReference(parameterizedQualifiedTypeReference.tokens,
                            parameterizedQualifiedTypeReference.sourceStart,
                            parameterizedQualifiedTypeReference.sourceEnd);
                }
            }
            return ref;
        } else if (length == 1) {
            // single variable reference
            this.genericsLengthPtr--; // pop the 0
            if (dim == 0) {
                SingleTypeReference ref = new SingleTypeReference(identifierStack[identifierPtr],
                        identifierPositionStack[identifierPtr--]);
                if (reportReferenceInfo) {
                    requestor.acceptTypeReference(ref.token, ref.sourceStart);
                }
                return ref;
            } else {
                ArrayTypeReference ref = new ArrayTypeReference(identifierStack[identifierPtr], dim,
                        identifierPositionStack[identifierPtr--]);
                ref.sourceEnd = endPosition;
                if (reportReferenceInfo) {
                    requestor.acceptTypeReference(ref.token, ref.sourceStart);
                }
                return ref;
            }
        } else {//Qualified variable reference
            this.genericsLengthPtr--;
            char[][] tokens = new char[length][];
            identifierPtr -= length;
            long[] positions = new long[length];
            System.arraycopy(identifierStack, identifierPtr + 1, tokens, 0, length);
            System.arraycopy(identifierPositionStack, identifierPtr + 1, positions, 0, length);
            if (dim == 0) {
                QualifiedTypeReference ref = new QualifiedTypeReference(tokens, positions);
                if (reportReferenceInfo) {
                    requestor.acceptTypeReference(ref.tokens, ref.sourceStart, ref.sourceEnd);
                }
                return ref;
            } else {
                ArrayQualifiedTypeReference ref = new ArrayQualifiedTypeReference(tokens, dim, positions);
                ref.sourceEnd = endPosition;
                if (reportReferenceInfo) {
                    requestor.acceptTypeReference(ref.tokens, ref.sourceStart, ref.sourceEnd);
                }
                return ref;
            }
        }
    }
}

From source file:spoon.support.compiler.jdt.ReferenceBuilder.java

License:Open Source License

CtTypeReference<Object> getTypeParameterReference(TypeBinding binding, TypeReference ref) {
    CtTypeReference<Object> ctRef = getTypeReference(binding);
    if (ctRef != null && isCorrectTypeReference(ref)) {
        if (!(ctRef instanceof CtTypeParameterReference)) {
            CtTypeParameterReference typeParameterRef = this.jdtTreeBuilder.getFactory().Core()
                    .createTypeParameterReference();
            typeParameterRef.setSimpleName(ctRef.getSimpleName());
            typeParameterRef.setDeclaringType(ctRef.getDeclaringType());
            typeParameterRef.setPackage(ctRef.getPackage());
            ctRef = typeParameterRef;/*w  ww  .  ja  va 2 s .com*/
        }
        insertGenericTypesInNoClasspathFromJDTInSpoon(ref, ctRef);
        return ctRef;
    }
    return getTypeParameterReference(CharOperation.toString(ref.getParameterizedTypeName()));
}

From source file:spoon.support.compiler.jdt.ReferenceBuilder.java

License:Open Source License

/**
 * JDT doesn't return a correct AST with the resolved type of the reference.
 * This method try to build a correct Spoon AST from the name of the JDT
 * reference, thanks to the parsing of the string, the name parameterized from
 * the JDT reference and java convention.
 * Returns a complete Spoon AST when the name is correct, otherwise a spoon type
 * reference with a name that correspond to the name of the JDT type reference.
 *//*from w  ww.  j  a v  a2 s  .  co  m*/
<T> CtTypeReference<T> getTypeReference(TypeReference ref) {
    if (ref == null) {
        return null;
    }
    CtTypeReference<T> res = null;
    CtTypeReference inner = null;
    final String[] namesParameterized = CharOperation.charArrayToStringArray(ref.getParameterizedTypeName());
    String nameParameterized = CharOperation.toString(ref.getParameterizedTypeName());
    String typeName = CharOperation.toString(ref.getTypeName());

    int index = namesParameterized.length - 1;
    for (; index >= 0; index--) {
        // Start at the end to get the class name first.
        CtTypeReference main = getTypeReference(namesParameterized[index]);
        if (main == null) {
            break;
        }
        if (res == null) {
            res = (CtTypeReference<T>) main;
        } else {
            inner.setDeclaringType((CtTypeReference<?>) main);
        }
        inner = main;
    }
    if (res == null) {
        return this.jdtTreeBuilder.getFactory().Type().createReference(nameParameterized);
    }

    if (inner.getPackage() == null) {
        PackageFactory packageFactory = this.jdtTreeBuilder.getFactory().Package();
        CtPackageReference packageReference = index >= 0
                ? packageFactory.getOrCreate(concatSubArray(namesParameterized, index)).getReference()
                : packageFactory.topLevel();
        inner.setPackage(packageReference);
    }
    if (!res.toString().replace(", ?", ",?").endsWith(nameParameterized)) {
        // verify that we did not match a class that have the same name in a different package
        return this.jdtTreeBuilder.getFactory().Type().createReference(typeName);
    }
    return res;
}