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

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

Introduction

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

Prototype

public abstract char[][] getTypeName();

Source Link

Usage

From source file:com.android.tools.lint.psi.EcjPsiBuilder.java

License:Apache License

@NonNull
private EcjPsiJavaCodeReferenceElement toTypeReference(@NonNull EcjPsiSourceElement parent,
        @NonNull TypeReference reference) {
    char[][] tokens = reference.getTypeName();
    EcjPsiJavaCodeReferenceElement element = null;
    if (tokens.length == 1) {
        element = new EcjPsiJavaCodeReferenceElement(mManager, reference);
        parent.adoptChild(element);//from w ww .  j ava2 s.  co m
        String referenceName = new String(tokens[tokens.length - 1]);
        int start = reference.sourceStart;
        start += (reference.bits & ASTNode.ParenthesizedMASK) >> ASTNode.ParenthesizedSHIFT;
        int end = start + referenceName.length();
        element.setNameElement(toIdentifier(element, referenceName, new TextRange(start, end)));
    } else {
        // ECJ doesn't have a hierarchy of AST nodes, but PSI does
        int startOffset = reference.sourceStart;
        int endOffset = startOffset + tokens[0].length;
        EcjPsiJavaCodeReferenceElement prev = new EcjPsiJavaCodeReferenceElement(mManager, reference);
        prev.setNameElement(toIdentifier(prev, tokens[0], toRange(startOffset, endOffset)));
        prev.setRange(startOffset, endOffset);

        for (int i = 1; i < tokens.length; i++) {
            element = new EcjPsiJavaCodeReferenceElement(mManager, reference);
            element.setQualifier(prev);
            char[] name = tokens[i];
            endOffset += name.length + 1; // +1: dot
            element.adoptChild(prev);
            element.setNameElement(toIdentifier(element, name, toRange(endOffset - name.length, endOffset)));
            element.setRange(startOffset, endOffset);
            prev = element;
        }

        assert element != null;
        if (reference instanceof ParameterizedSingleTypeReference) {
            ParameterizedSingleTypeReference typeReference = (ParameterizedSingleTypeReference) reference;
            element.setParameterList(toTypeParameterList(element, typeReference.typeArguments));
        }

        parent.adoptChild(element);
    }

    if (reference instanceof ParameterizedSingleTypeReference) {
        ParameterizedSingleTypeReference typeReference = (ParameterizedSingleTypeReference) reference;
        if (typeReference.typeArguments.length > 0) {
            EcjPsiReferenceParameterList parameterList = toTypeParameterList(element,
                    typeReference.typeArguments);
            element.setParameterList(parameterList);
            // Widen offset range: ECJ doesn't seem to include bounds of type parameters
            int endOffset = parameterList.getTextRange().getEndOffset();
            element.setRange(element.getTextRange().getStartOffset(), endOffset);
            //noinspection ConstantConditions
            while (parent != null) {
                if (parent.getTextRange().getEndOffset() < endOffset) {
                    parent.setRange(parent.getTextRange().getStartOffset(), endOffset);
                } else {
                    break;
                }
                parent = parent.getParent();
            }
        }
    }

    return element;
}

From source file:com.android.tools.lint.psi.EcjPsiJavaCodeReferenceElement.java

License:Apache License

@Override
public String getQualifiedName() {
    if (mNativeNode instanceof ImportReference) {
        return getTypeName(((ImportReference) mNativeNode).getImportName());
    }/*w ww  .  j  a v a2  s.  c  o m*/
    TypeReference reference = (TypeReference) mNativeNode;
    if (reference.resolvedType instanceof ReferenceBinding) {
        return getTypeName((ReferenceBinding) reference.resolvedType);
    } else {
        return getTypeName(reference.getTypeName());
    }
}

From source file:com.android.tools.lint.psi.EcjPsiManager.java

License:Apache License

@NonNull
public static String getTypeName(@NonNull TypeReference typeReference) {
    return getTypeName(typeReference.getTypeName());
}

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.
 *///from  www  .jav a 2s .  c o  m
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.search.matching.MatchLocator.java

License:Open Source License

/**
 * Create an handle for a local variable declaration (may be a local variable or type parameter).
 */// w w  w  . ja  v  a 2  s .com
protected IJavaElement createHandle(Annotation annotation, IAnnotatable parent) {
    if (parent == null)
        return null;
    TypeReference typeRef = annotation.type;
    char[][] typeName = typeRef.getTypeName();
    String name = new String(typeName[typeName.length - 1]);
    try {
        IAnnotation[] annotations = parent.getAnnotations();
        int length = annotations == null ? 0 : annotations.length;
        for (int i = 0; i < length; i++) {
            if (annotations[i].getElementName().equals(name)) {
                return annotations[i];
            }
        }
    } catch (JavaModelException jme) {
        // skip
    }
    return null;
}

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

License:Open Source License

/**
 * Returns whether the given type reference matches the given pattern.
 *//* www  .  j  ava 2 s  . com*/
protected boolean matchesTypeReference(char[] pattern, TypeReference type) {
    if (pattern == null)
        return true; // null is as if it was "*"
    if (type == null)
        return true; // treat as an inexact match

    char[][] compoundName = type.getTypeName();
    char[] simpleName = compoundName[compoundName.length - 1];
    int dimensions = type.dimensions() * 2;
    if (dimensions > 0) {
        int length = simpleName.length;
        char[] result = new char[length + dimensions];
        System.arraycopy(simpleName, 0, result, 0, length);
        for (int i = length, l = result.length; i < l;) {
            result[i++] = '[';
            result[i++] = ']';
        }
        simpleName = result;
    }

    return matchesName(pattern, simpleName);
}

From source file:com.google.gwt.dev.javac.JavaSourceParser.java

License:Open Source License

/**
 * Compares an unresolved JDT type to a TypeOracle type to see if they match.
 * /*from   w  w  w  . j  ava  2 s.  com*/
 * @param jdtType
 * @param toType
 * @return true if the two type objects resolve to the same 
 */
private static boolean typeMatches(TypeReference jdtType, JType toType) {
    List<char[]> toNameComponents = getClassChain(toType.getQualifiedBinaryName());
    int toLen = toNameComponents.size();
    char[][] jdtNameComponents = jdtType.getTypeName();
    int jdtLen = jdtNameComponents.length;
    int maxToCompare = Math.min(toLen, jdtLen);

    // compare from the end
    for (int i = 1; i <= maxToCompare; ++i) {
        if (!Arrays.equals(jdtNameComponents[jdtLen - i], toNameComponents.get(toLen - i))) {
            return false;
        }
    }
    return true;
}

From source file:lombok.eclipse.Eclipse.java

License:Open Source License

/**
 * Searches the given field node for annotations and returns each one that matches the provided regular expression pattern.
 * //  w ww . ja  v a 2 s.  c o  m
 * Only the simple name is checked - the package and any containing class are ignored.
 */
public static Annotation[] findAnnotations(FieldDeclaration field, Pattern namePattern) {
    List<Annotation> result = new ArrayList<Annotation>();
    if (field.annotations == null)
        return EMPTY_ANNOTATIONS_ARRAY;
    for (Annotation annotation : field.annotations) {
        TypeReference typeRef = annotation.type;
        if (typeRef != null && typeRef.getTypeName() != null) {
            char[][] typeName = typeRef.getTypeName();
            String suspect = new String(typeName[typeName.length - 1]);
            if (namePattern.matcher(suspect).matches()) {
                result.add(annotation);
            }
        }
    }
    return result.toArray(EMPTY_ANNOTATIONS_ARRAY);
}

From source file:lombok.eclipse.Eclipse.java

License:Open Source License

/**
 * Checks if the given type reference represents a primitive type.
 *//*from  w ww  .  j av  a2 s  .  com*/
public static boolean isPrimitive(TypeReference ref) {
    if (ref.dimensions() > 0)
        return false;
    return PRIMITIVE_TYPE_NAME_PATTERN.matcher(toQualifiedName(ref.getTypeName())).matches();
}

From source file:lombok.eclipse.handlers.ast.EclipseField.java

License:Open Source License

public boolean isOfType(final String typeName) {
    TypeReference variableType = get().type;
    if (variableType == null)
        return false;
    StringBuilder sb = new StringBuilder();
    boolean first = true;
    for (char[] elem : variableType.getTypeName()) {
        if (first)
            first = false;//from  www.  ja  v  a 2s  .co m
        else
            sb.append('.');
        sb.append(elem);
    }
    String type = sb.toString();
    return type.endsWith(typeName);
}