Example usage for org.eclipse.jdt.core IType getTypes

List of usage examples for org.eclipse.jdt.core IType getTypes

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getTypes.

Prototype

IType[] getTypes() throws JavaModelException;

Source Link

Document

Returns the immediate member types declared by this type.

Usage

From source file:cfgrecognition.loader.SootClassLoader.java

License:Open Source License

protected void dfsDomTree(IJavaElement element) throws Exception {
    //      if (monitor.isCanceled()) {
    //         return;
    //      }//from ww  w .  j a  v  a 2s .co m

    if (element.isReadOnly())
        return;

    int elementType = element.getElementType();
    if (elementType == IJavaElement.COMPILATION_UNIT) {
        ICompilationUnit cu = (ICompilationUnit) element;
        IType[] allTypes = cu.getTypes();
        for (IType aType : allTypes) {
            dfsDomTree(aType);
        }
    } else if (elementType == IJavaElement.TYPE) {
        IType aType = (IType) element;

        if (aType.isClass()) {
            // Load a type in Soot
            load(aType.getFullyQualifiedName());
        }

        // Go inside the methods to look for Anonymous Inner Class
        for (IMethod m : aType.getMethods()) {
            IJavaElement[] elems = m.getChildren();
            for (IJavaElement elem : elems) {
                if (elem.getElementType() == IJavaElement.TYPE) {
                    dfsDomTree(elem);
                }
            }
        }

        // For inner classes
        IType[] allTypes = aType.getTypes();
        for (IType childType : allTypes) {
            dfsDomTree(childType);
        }
    }

}

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

License:Open Source License

private TypeDeclaration convert(IType type, IType alreadyComputedMember,
        TypeDeclaration alreadyComputedMemberDeclaration) throws JavaModelException {
    /* create type declaration - can be member type */
    TypeDeclaration typeDeclaration = new TypeDeclaration(this.compilationResult);

    if (type.getDeclaringType() != null) {
        typeDeclaration.bits |= ASTNode.IsMemberType;
    }//from  w w w  . j a v  a 2  s .  c om
    typeDeclaration.name = type.getElementName().toCharArray();
    typeDeclaration.modifiers = type.getFlags();

    /* set superclass and superinterfaces */
    if (type.getSuperclassName() != null) {
        TypeReference typeReference = createTypeReference(type.getSuperclassTypeSignature());
        if (typeReference != null) {
            typeDeclaration.superclass = typeReference;
            typeDeclaration.superclass.bits |= ASTNode.IsSuperType;
        }
    }

    String[] interfaceTypes = type.getSuperInterfaceTypeSignatures();
    int interfaceCount = interfaceTypes == null ? 0 : interfaceTypes.length;
    typeDeclaration.superInterfaces = new TypeReference[interfaceCount];
    int count = 0;
    for (int i = 0; i < interfaceCount; i++) {
        TypeReference typeReference = createTypeReference(interfaceTypes[i]);
        if (typeReference != null) {
            typeDeclaration.superInterfaces[count] = typeReference;
            typeDeclaration.superInterfaces[count++].bits |= ASTNode.IsSuperType;
        }
    }
    if (count != interfaceCount) {
        System.arraycopy(typeDeclaration.fields, 0,
                typeDeclaration.superInterfaces = new TypeReference[interfaceCount], 0, interfaceCount);
    }

    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {

        /* convert type parameters */
        ITypeParameter[] typeParameters = type.getTypeParameters();
        if (typeParameters != null && typeParameters.length > 0) {
            int parameterCount = typeParameters.length;
            org.eclipse.jdt.internal.compiler.ast.TypeParameter[] typeParams = new org.eclipse.jdt.internal.compiler.ast.TypeParameter[parameterCount];
            for (int i = 0; i < parameterCount; i++) {
                ITypeParameter typeParameter = typeParameters[i];
                typeParams[i] = createTypeParameter(typeParameter.getElementName().toCharArray(),
                        stringArrayToCharArray(typeParameter.getBounds()), 0, 0);
            }

            typeDeclaration.typeParameters = typeParams;
        }
    }

    /* convert member types */
    IType[] memberTypes = type.getTypes();
    int memberTypeCount = memberTypes == null ? 0 : memberTypes.length;
    typeDeclaration.memberTypes = new TypeDeclaration[memberTypeCount];
    for (int i = 0; i < memberTypeCount; i++) {
        if (alreadyComputedMember != null && alreadyComputedMember.getFullyQualifiedName()
                .equals(memberTypes[i].getFullyQualifiedName())) {
            typeDeclaration.memberTypes[i] = alreadyComputedMemberDeclaration;
        } else {
            typeDeclaration.memberTypes[i] = convert(memberTypes[i], null, null);
        }
        typeDeclaration.memberTypes[i].enclosingType = typeDeclaration;
    }

    /* convert fields */
    IField[] fields = type.getFields();
    int fieldCount = fields == null ? 0 : fields.length;
    typeDeclaration.fields = new FieldDeclaration[fieldCount];
    count = 0;
    for (int i = 0; i < fieldCount; i++) {
        FieldDeclaration fieldDeclaration = convert(fields[i], type);
        if (fieldDeclaration != null) {
            typeDeclaration.fields[count++] = fieldDeclaration;
        }
    }
    if (count != fieldCount) {
        System.arraycopy(typeDeclaration.fields, 0, typeDeclaration.fields = new FieldDeclaration[count], 0,
                count);
    }

    /* convert methods - need to add default constructor if necessary */
    IMethod[] methods = type.getMethods();
    int methodCount = methods == null ? 0 : methods.length;

    /* source type has a constructor ?           */
    /* by default, we assume that one is needed. */
    int neededCount = 1;
    for (int i = 0; i < methodCount; i++) {
        if (methods[i].isConstructor()) {
            neededCount = 0;
            // Does not need the extra constructor since one constructor already exists.
            break;
        }
    }
    boolean isInterface = type.isInterface();
    neededCount = isInterface ? 0 : neededCount;
    typeDeclaration.methods = new AbstractMethodDeclaration[methodCount + neededCount];
    if (neededCount != 0) { // add default constructor in first position
        typeDeclaration.methods[0] = typeDeclaration.createDefaultConstructor(false, false);
    }
    boolean hasAbstractMethods = false;
    count = 0;
    for (int i = 0; i < methodCount; i++) {
        AbstractMethodDeclaration method = convert(methods[i], type);
        if (method != null) {
            boolean isAbstract;
            if ((isAbstract = method.isAbstract()) || isInterface) { // fix-up flag
                method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
            }
            if (isAbstract) {
                hasAbstractMethods = true;
            }
            typeDeclaration.methods[neededCount + (count++)] = method;
        }
    }
    if (count != methodCount) {
        System.arraycopy(typeDeclaration.methods, 0,
                typeDeclaration.methods = new AbstractMethodDeclaration[count + neededCount], 0,
                count + neededCount);
    }
    if (hasAbstractMethods) {
        typeDeclaration.bits |= ASTNode.HasAbstractMethods;
    }
    return typeDeclaration;
}

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

License:Open Source License

/**
 * @see org.eclipse.jdt.core.ICompilationUnit#getAllTypes()
 *///  ww w  .  ja va2  s  . c  om
public IType[] getAllTypes() throws JavaModelException {
    IJavaElement[] types = getTypes();
    int i;
    ArrayList allTypes = new ArrayList(types.length);
    ArrayList typesToTraverse = new ArrayList(types.length);
    for (i = 0; i < types.length; i++) {
        typesToTraverse.add(types[i]);
    }
    while (!typesToTraverse.isEmpty()) {
        IType type = (IType) typesToTraverse.get(0);
        typesToTraverse.remove(type);
        allTypes.add(type);
        types = type.getTypes();
        for (i = 0; i < types.length; i++) {
            typesToTraverse.add(types[i]);
        }
    }
    IType[] arrayOfAllTypes = new IType[allTypes.size()];
    allTypes.toArray(arrayOfAllTypes);
    return arrayOfAllTypes;
}

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

License:Open Source License

/**
 * Notifies the given requestor of all types (classes and interfaces) in the
 * given type with the given (possibly qualified) name. Checks
 * the requestor at regular intervals to see if the requestor
 * has canceled./*from  w  w  w.  j av  a2  s .co  m*/
 */
protected boolean seekTypesInType(String prefix, int firstDot, IType type, IJavaElementRequestor requestor,
        int acceptFlags) {
    IType[] types = null;
    try {
        types = type.getTypes();
    } catch (JavaModelException npe) {
        return false; // the enclosing type is not present
    }
    int length = types.length;
    if (length == 0)
        return false;

    String memberPrefix = prefix;
    boolean isMemberTypePrefix = false;
    if (firstDot != -1) {
        memberPrefix = prefix.substring(0, firstDot);
        isMemberTypePrefix = true;
    }
    for (int i = 0; i < length; i++) {
        if (requestor.isCanceled())
            return false;
        IType memberType = types[i];
        if (memberType.getElementName().toLowerCase().startsWith(memberPrefix))
            if (isMemberTypePrefix) {
                String subPrefix = prefix.substring(firstDot + 1, prefix.length());
                return seekTypesInType(subPrefix, subPrefix.indexOf('.'), memberType, requestor, acceptFlags);
            } else {
                if (acceptType(memberType, acceptFlags, true/*a source type*/)) {
                    requestor.acceptMemberType(memberType);
                    return true;
                }
            }
    }
    return false;
}

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

License:Open Source License

private boolean enclosesType(IType type, boolean recurse) {
    if (this.subTypes != null) {
        // searching subtypes
        if (this.subTypes.contains(type)) {
            return true;
        }/*from w  w  w . jav a 2  s .  c  o m*/
        // be flexible: look at original element (see bug 14106 and below)
        IType original = type.isBinary() ? null : (IType) type.getPrimaryElement();
        if (original != type && this.subTypes.contains(original)) {
            return true;
        }
    } else {
        if (this.hierarchy.contains(type)) {
            return true;
        } else {
            // be flexible: look at original element (see bug 14106 Declarations in Hierarchy does not find declarations in hierarchy)
            IType original;
            if (!type.isBinary() && (original = (IType) type.getPrimaryElement()) != null) {
                if (this.hierarchy.contains(original)) {
                    return true;
                }
            }
        }
    }
    if (recurse) {
        // queried type is enclosed in this scope if one of its members is:
        try {
            IType[] memberTypes = type.getTypes();
            for (int i = 0; i < memberTypes.length; i++) {
                if (enclosesType(memberTypes[i], recurse)) {
                    return true;
                }
            }
        } catch (JavaModelException e) {
            return false;
        }
    }
    return false;
}

From source file:com.codenvy.ide.ext.java.server.SourcesFromBytecodeGenerator.java

License:Open Source License

private void generateType(IType type, StringBuilder builder, String indent) throws JavaModelException {
    int flags = 0;

    appendAnnotationLabels(type.getAnnotations(), flags, builder, indent.substring(TAB.length()));
    builder.append(indent.substring(TAB.length()));
    builder.append(getModifiers(type.getFlags(), type.getFlags())).append(' ').append(getJavaType(type))
            .append(' ').append(type.getElementName());

    if (type.isResolved()) {
        BindingKey key = new BindingKey(type.getKey());
        if (key.isParameterizedType()) {
            String[] typeArguments = key.getTypeArguments();
            appendTypeArgumentSignaturesLabel(type, typeArguments, flags, builder);
        } else {//from w  w  w .  j a va2  s . c  o  m
            String[] typeParameters = Signature.getTypeParameters(key.toSignature());
            appendTypeParameterSignaturesLabel(typeParameters, builder);
        }
    } else {
        appendTypeParametersLabels(type.getTypeParameters(), flags, builder);
    }

    if (!"java.lang.Object".equals(type.getSuperclassName())
            && !"java.lang.Enum".equals(type.getSuperclassName())) {

        builder.append(" extends ");
        if (type.getSuperclassTypeSignature() != null) {
            //                appendTypeSignatureLabel(type, type.getSuperclassTypeSignature(), flags, builder);
            builder.append(Signature.toString(type.getSuperclassTypeSignature()));
        } else {
            builder.append(type.getSuperclassName());
        }
    }
    if (!type.isAnnotation()) {
        if (type.getSuperInterfaceNames().length != 0) {
            builder.append(" implements ");
            String[] signatures = type.getSuperInterfaceTypeSignatures();
            if (signatures.length == 0) {
                signatures = type.getSuperInterfaceNames();
            }
            for (String interfaceFqn : signatures) {
                builder.append(Signature.toString(interfaceFqn)).append(", ");
            }
            builder.delete(builder.length() - 2, builder.length());
        }
    }
    builder.append(" {\n");

    List<IField> fields = new ArrayList<>();
    if (type.isEnum()) {
        builder.append(indent);
        for (IField field : type.getFields()) {
            if (field.isEnumConstant()) {
                builder.append(field.getElementName()).append(", ");
            } else {
                fields.add(field);
            }
        }
        if (", ".equals(builder.substring(builder.length() - 2))) {
            builder.delete(builder.length() - 2, builder.length());
        }
        builder.append(";\n");

    } else {
        fields.addAll(Arrays.asList(type.getFields()));
    }

    for (IField field : fields) {
        if (Flags.isSynthetic(field.getFlags())) {
            continue;
        }
        appendAnnotationLabels(field.getAnnotations(), flags, builder, indent);
        builder.append(indent).append(getModifiers(field.getFlags(), type.getFlags()));
        if (builder.charAt(builder.length() - 1) != ' ') {
            builder.append(' ');
        }

        builder.append(Signature.toCharArray(field.getTypeSignature().toCharArray())).append(' ')
                .append(field.getElementName());
        if (field.getConstant() != null) {
            builder.append(" = ");
            if (field.getConstant() instanceof String) {
                builder.append('"').append(field.getConstant()).append('"');
            } else {
                builder.append(field.getConstant());
            }
        }
        builder.append(";\n");
    }
    builder.append('\n');

    for (IMethod method : type.getMethods()) {
        if (method.getElementName().equals("<clinit>") || Flags.isSynthetic(method.getFlags())) {
            continue;
        }
        appendAnnotationLabels(method.getAnnotations(), flags, builder, indent);
        BindingKey resolvedKey = method.isResolved() ? new BindingKey(method.getKey()) : null;
        String resolvedSig = (resolvedKey != null) ? resolvedKey.toSignature() : null;
        builder.append(indent).append(getModifiers(method.getFlags(), type.getFlags()));

        if (builder.charAt(builder.length() - 1) != ' ') {
            builder.append(' ');
        }
        if (resolvedKey != null) {
            if (resolvedKey.isParameterizedMethod()) {
                String[] typeArgRefs = resolvedKey.getTypeArguments();
                if (typeArgRefs.length > 0) {
                    appendTypeArgumentSignaturesLabel(method, typeArgRefs, flags, builder);
                    builder.append(' ');
                }
            } else {
                String[] typeParameterSigs = Signature.getTypeParameters(resolvedSig);
                if (typeParameterSigs.length > 0) {
                    appendTypeParameterSignaturesLabel(typeParameterSigs, builder);
                    builder.append(' ');
                }
            }
        } else if (method.exists()) {
            ITypeParameter[] typeParameters = method.getTypeParameters();
            if (typeParameters.length > 0) {
                appendTypeParametersLabels(typeParameters, flags, builder);
                builder.append(' ');
            }
        }

        if (!method.isConstructor()) {

            String returnTypeSig = resolvedSig != null ? Signature.getReturnType(resolvedSig)
                    : method.getReturnType();
            appendTypeSignatureLabel(method, returnTypeSig, 0, builder);
            builder.append(' ');
            //                builder.append(Signature.toCharArray(method.getReturnType().toCharArray())).append(' ');
        }
        builder.append(method.getElementName());
        builder.append('(');
        for (ILocalVariable variable : method.getParameters()) {
            builder.append(Signature.toString(variable.getTypeSignature()));
            builder.append(' ').append(variable.getElementName()).append(", ");

        }

        if (builder.charAt(builder.length() - 1) == ' ') {
            builder.delete(builder.length() - 2, builder.length());
        }
        builder.append(')');
        String[] exceptionTypes = method.getExceptionTypes();
        if (exceptionTypes != null && exceptionTypes.length != 0) {
            builder.append(' ').append("throws ");
            for (String exceptionType : exceptionTypes) {
                builder.append(Signature.toCharArray(exceptionType.toCharArray())).append(", ");
            }
            builder.delete(builder.length() - 2, builder.length());
        }
        if (type.isInterface() || type.isAnnotation()) {
            builder.append(";\n\n");
        } else {
            builder.append(" {").append(METHOD_BODY).append("}\n\n");
        }
    }
    for (IType iType : type.getTypes()) {
        generateType(iType, builder, indent + indent);
    }
    builder.append(indent.substring(TAB.length()));
    builder.append("}\n");
}

From source file:com.drgarbage.utils.ClassFileDocumentsUtils.java

License:Apache License

/**
 * Collects recursively nested classes in the compilation unit.
 * The nested class names are type-qualified. The classes are 
 * stored in the class list given as argument.
 * For example://  w w w  .j  a  v  a2  s .  c om
 * <pre>
 *      HEllo
 *      HEllo$C1
 *      HEllo$C1$C3
 * </pre>
 *  
 * @param t type representing a class
 * @param classList class list to store nested classes
 * @throws JavaModelException
 * @see IType#getTypeQualifiedName()
 */
public static void collectNestedClasses(IType t, List<String> classList) throws JavaModelException {
    classList.add(t.getTypeQualifiedName());

    /* start recursion */
    IType[] types = t.getTypes();
    for (IType e : types) {
        collectNestedClasses(e, classList);
    }
}

From source file:com.ecfeed.ui.common.JavaModelAnalyser.java

License:Open Source License

protected static IType getLocalVariableType(IMethod method, ILocalVariable var) {
    try {/* ww w.j  a  v  a 2 s . c om*/
        IType declaringType = method.getDeclaringType();
        String variableTypeName = Signature.toString(var.getTypeSignature());
        for (IType type : declaringType.getTypes()) {
            if (type.getElementName().equals(variableTypeName)) {
                return type;
            }
        }
    } catch (JavaModelException e) {
    }
    return null;
}

From source file:com.google.gdt.eclipse.designer.uibinder.model.util.StylePropertySupport.java

License:Open Source License

/**
 * @return the {@link Map} of <code>ClientBundle</code> classes into resource {@link IFile}s.
 *//*from   w ww.  ja  v a2 s.  co  m*/
private Map<Class<?>, IFile> getExternalPossibleCssFiles(final IType withType) throws Exception {
    final Map<Class<?>, IFile> styleToFile = Maps.newHashMap();
    // check every inner type
    for (final IType clientBundleType : withType.getTypes()) {
        if (isClientBundle(clientBundleType)) {
            ExecutionUtils.runIgnore(new RunnableEx() {
                public void run() throws Exception {
                    addExternalPossibleCssFiles(styleToFile, withType, clientBundleType);
                }
            });
        }
    }
    return styleToFile;
}

From source file:com.ibm.research.tours.content.url.URLTranslator.java

License:Open Source License

private static IType getType(IType[] types, String typeName) throws JavaModelException {
    for (IType type : types)
        if (type.getTypeQualifiedName('.').equals(typeName))
            return type;
        else/* w  ww .  ja v  a 2s .co m*/
            return getType(type.getTypes(), typeName);

    return null;
}