Java Code Examples for javax.lang.model.util.Types#directSupertypes()

The following examples show how to use javax.lang.model.util.Types#directSupertypes() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TypeSimplifier.java    From auto with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all types that are declared with non private visibility by the given {@code TypeMirror},
 * any class in its superclass chain, or any interface it implements.
 */
private static Set<TypeMirror> nonPrivateDeclaredTypes(Types typeUtils, TypeMirror type) {
  if (type == null) {
    return new TypeMirrorSet();
  } else {
    Set<TypeMirror> declared = new TypeMirrorSet();
    declared.add(type);
    List<TypeElement> nestedTypes =
        ElementFilter.typesIn(typeUtils.asElement(type).getEnclosedElements());
    for (TypeElement nestedType : nestedTypes) {
      if (!nestedType.getModifiers().contains(PRIVATE)) {
        declared.add(nestedType.asType());
      }
    }
    for (TypeMirror supertype : typeUtils.directSupertypes(type)) {
      declared.addAll(nonPrivateDeclaredTypes(typeUtils, supertype));
    }
    return declared;
  }
}
 
Example 2
Source File: Helper.java    From vertx-codegen with Apache License 2.0 6 votes vote down vote up
/**
 * Return the type of a type parameter element of a given type element when that type parameter
 * element is parameterized by a sub type, directly or indirectly. When the type parameter cannot
 * be resolved, null is returned.
 *
 * @param typeUtils the type utils
 * @param subType the sub type for which the type parameter is parameterized
 * @param typeParam the type parameter to resolve
 * @return the type parameterizing the type parameter
 */
public static TypeMirror resolveTypeParameter(Types typeUtils, DeclaredType subType, TypeParameterElement typeParam) {
  TypeMirror erased = typeUtils.erasure(typeParam.getGenericElement().asType());
  TypeMirror erasedSubType = typeUtils.erasure(subType);
  if (typeUtils.isSameType(erased, erasedSubType)) {
    return typeUtils.asMemberOf(subType, ((TypeVariable) typeParam.asType()).asElement());
  } else if (typeUtils.isSubtype(erasedSubType, erased)) {
    for (TypeMirror superType : typeUtils.directSupertypes(subType)) {
      TypeMirror resolved = resolveTypeParameter(typeUtils, (DeclaredType) superType, typeParam);
      if (resolved != null) {
        return resolved;
      }
    }
  }
  return null;
}
 
Example 3
Source File: UseSuperTypeRefactoringPlugin.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void replaceWithSuperType(TreePath path, VariableTree oldVarTree, VariableElement varElement, Element superTypeElement) {
    Types types = workingCopy.getTypes();
    TypeMirror supTypeErasure = types.erasure(superTypeElement.asType());
    DeclaredType varType = (DeclaredType) varElement.asType();
    TypeMirror theType = null;
    List<TypeMirror> supertypes = new LinkedList(types.directSupertypes(varType));
    while(!supertypes.isEmpty()) {
        TypeMirror supertype = supertypes.remove(0);
        if(types.isSameType(types.erasure(supertype), supTypeErasure)) {
            theType = supertype;
            break;
        }
        supertypes.addAll(types.directSupertypes(supertype));
    }
    
    if(theType == null) {
        theType = supTypeErasure;
    }
    Tree superTypeTree = make.Type(theType);
  
    ExpressionTree oldInitTree = oldVarTree.getInitializer();
    ModifiersTree oldModifiers = oldVarTree.getModifiers();
    Tree newTree = make.Variable(oldModifiers, oldVarTree.getName(), 
            superTypeTree, oldInitTree);
    rewrite(oldVarTree, newTree);
}
 
Example 4
Source File: PullUpTransformer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private boolean deepSearchTypes(DeclaredType currentElement, TypeMirror orig, TypeMirror something, Map<TypeMirror, TypeParameterElement> mappings) {
    Types types = workingCopy.getTypes();
    List<? extends TypeMirror> directSupertypes = types.directSupertypes(currentElement);
    for (TypeMirror superType : directSupertypes) {
        DeclaredType type = (DeclaredType) superType;
        List<? extends TypeMirror> typeArguments = type.getTypeArguments();
        for (int i = 0; i < typeArguments.size(); i++) {
            TypeMirror typeArgument = typeArguments.get(i);
            if (something.equals(typeArgument)) {
                TypeElement asElement = (TypeElement) type.asElement();
                mappings.put(orig, asElement.getTypeParameters().get(i));
                if (types.erasure(targetType.asType()).equals(types.erasure(superType))) {
                    return true;
                }
                if(deepSearchTypes(type, orig, typeArgument, mappings)) {
                    break;
                }
            }
        }
        if (types.erasure(targetType.asType()).equals(types.erasure(superType))) {
            mappings.remove(orig);
            return true;
        }
    }
    return false;
}
 
Example 5
Source File: Util.java    From revapi with Apache License 2.0 6 votes vote down vote up
/**
 * Similar to {@link #getAllSuperTypes(javax.lang.model.util.Types, javax.lang.model.type.TypeMirror)} but avoids
 * instantiation of a new list.
 *
 * @param types  the Types instance of the compilation environment from which the type comes from
 * @param type   the type
 * @param result the list to add the results to.
 */
public static void fillAllSuperTypes(@Nonnull Types types, @Nonnull TypeMirror type,
    @Nonnull List<TypeMirror> result) {

    try {
        List<? extends TypeMirror> superTypes = types.directSupertypes(type);

        for (TypeMirror t : superTypes) {
            result.add(t);
            fillAllSuperTypes(types, t, result);
        }
    } catch (RuntimeException e) {
        LOG.debug("Failed to find all super types of type '" + toHumanReadableString(type) + ". Possibly " +
            "missing classes?", e);
    }
}
 
Example 6
Source File: Util.java    From revapi with Apache License 2.0 6 votes vote down vote up
/**
 * Returns all the super classes of given type. I.e. the returned list does NOT contain any interfaces
 * the class or tis superclasses implement.
 *
 * @param types the Types instance of the compilation environment from which the type comes from
 * @param type  the type
 *
 * @return the list of super classes
 */
@Nonnull
public static List<TypeMirror> getAllSuperClasses(@Nonnull Types types, @Nonnull TypeMirror type) {
    List<TypeMirror> ret = new ArrayList<>();

    try {
        List<? extends TypeMirror> superTypes = types.directSupertypes(type);
        while (superTypes != null && !superTypes.isEmpty()) {
            TypeMirror superClass = superTypes.get(0);
            ret.add(superClass);
            superTypes = types.directSupertypes(superClass);
        }
    } catch (RuntimeException e) {
        LOG.debug("Failed to find all super classes of type '" + toHumanReadableString(type) + ". Possibly " +
            "missing classes?", e);
    }

    return ret;
}
 
Example 7
Source File: TypeSimplifier.java    From SimpleWeibo with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all types that are declared with non private visibility by the given {@code TypeMirror},
 * any class in its superclass chain, or any interface it implements.
 */
private static Set<TypeMirror> nonPrivateDeclaredTypes(Types typeUtils, TypeMirror type) {
  if (type == null) {
    return new TypeMirrorSet();
  } else {
    Set<TypeMirror> declared = new TypeMirrorSet();
    declared.add(type);
    List<TypeElement> nestedTypes =
        ElementFilter.typesIn(typeUtils.asElement(type).getEnclosedElements());
    for (TypeElement nestedType : nestedTypes) {
      if (!nestedType.getModifiers().contains(PRIVATE)) {
        declared.add(nestedType.asType());
      }
    }
    for (TypeMirror supertype : typeUtils.directSupertypes(type)) {
      declared.addAll(nonPrivateDeclaredTypes(typeUtils, supertype));
    }
    return declared;
  }
}
 
Example 8
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all types that are declared with non private visibility by the given {@code TypeMirror},
 * any class in its superclass chain, or any interface it implements.
 */
private static Set<TypeMirror> nonPrivateDeclaredTypes(Types typeUtils, TypeMirror type) {
  if (type == null) {
    return new TypeMirrorSet();
  } else {
    Set<TypeMirror> declared = new TypeMirrorSet();
    declared.add(type);
    List<TypeElement> nestedTypes =
        ElementFilter.typesIn(typeUtils.asElement(type).getEnclosedElements());
    for (TypeElement nestedType : nestedTypes) {
      if (!nestedType.getModifiers().contains(PRIVATE)) {
        declared.add(nestedType.asType());
      }
    }
    for (TypeMirror supertype : typeUtils.directSupertypes(type)) {
      declared.addAll(nonPrivateDeclaredTypes(typeUtils, supertype));
    }
    return declared;
  }
}
 
Example 9
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 6 votes vote down vote up
/**
 * Finds all types that are declared with non private visibility by the given {@code TypeMirror},
 * any class in its superclass chain, or any interface it implements.
 */
private static Set<TypeMirror> nonPrivateDeclaredTypes(Types typeUtils, TypeMirror type) {
  if (type == null) {
    return new TypeMirrorSet();
  } else {
    Set<TypeMirror> declared = new TypeMirrorSet();
    declared.add(type);
    List<TypeElement> nestedTypes =
        ElementFilter.typesIn(typeUtils.asElement(type).getEnclosedElements());
    for (TypeElement nestedType : nestedTypes) {
      if (!nestedType.getModifiers().contains(PRIVATE)) {
        declared.add(nestedType.asType());
      }
    }
    for (TypeMirror supertype : typeUtils.directSupertypes(type)) {
      declared.addAll(nonPrivateDeclaredTypes(typeUtils, supertype));
    }
    return declared;
  }
}
 
Example 10
Source File: JavahTask.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitDeclared(DeclaredType t, Types types) {
    t.asElement().getKind(); // ensure class exists
    for (TypeMirror st: types.directSupertypes(t))
        visit(st, types);
    return null;
}
 
Example 11
Source File: Utils.java    From paperparcel with Apache License 2.0 5 votes vote down vote up
@Nullable
private static TypeMirror paramAsMemberOfImpl(
    Types types, DeclaredType type, TypeParameterElement param) {
  TypeElement paramEnclosingElement = (TypeElement) param.getEnclosingElement();
  TypeElement typeAsElement = (TypeElement) type.asElement();
  if (paramEnclosingElement.equals(typeAsElement)) {
    List<? extends TypeParameterElement> typeParamElements = typeAsElement.getTypeParameters();
    for (int i = 0; i < typeParamElements.size(); i++) {
      TypeParameterElement typeParamElement = typeParamElements.get(i);
      if (typeParamElement.equals(param)) {
        List<? extends TypeMirror> typeArguments = type.getTypeArguments();
        if (typeArguments.isEmpty()) {
          return types.erasure(param.asType());
        } else {
          return type.getTypeArguments().get(i);
        }
      }
    }
  }
  List<? extends TypeMirror> superTypes = types.directSupertypes(type);
  for (TypeMirror superType : superTypes) {
    if (superType.getKind() == TypeKind.DECLARED) {
      TypeMirror result = paramAsMemberOfImpl(types, (DeclaredType) superType, param);
      if (result != null) {
        return result;
      }
    }
  }
  return null;
}
 
Example 12
Source File: JavahTask.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitDeclared(DeclaredType t, Types types) {
    t.asElement().getKind(); // ensure class exists
    for (TypeMirror st: types.directSupertypes(t))
        visit(st, types);
    return null;
}
 
Example 13
Source File: JavahTask.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitDeclared(DeclaredType t, Types types) {
    t.asElement().getKind(); // ensure class exists
    for (TypeMirror st: types.directSupertypes(t))
        visit(st, types);
    return null;
}
 
Example 14
Source File: JavahTask.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitDeclared(DeclaredType t, Types types) {
    t.asElement().getKind(); // ensure class exists
    for (TypeMirror st: types.directSupertypes(t))
        visit(st, types);
    return null;
}
 
Example 15
Source File: JavahTask.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override @DefinedBy(Api.LANGUAGE_MODEL)
public Void visitDeclared(DeclaredType t, Types types) {
    t.asElement().getKind(); // ensure class exists
    for (TypeMirror st: types.directSupertypes(t))
        visit(st, types);
    return null;
}
 
Example 16
Source File: JavahTask.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitDeclared(DeclaredType t, Types types) {
    t.asElement().getKind(); // ensure class exists
    for (TypeMirror st: types.directSupertypes(t))
        visit(st, types);
    return null;
}
 
Example 17
Source File: AbstractAssignabilityChecker.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private TypeMirror getAncestor( TypeMirror subject , TypeMirror rawType ,
        Types types)
{
    List<? extends TypeMirror> directSupertypes = types.directSupertypes(subject);
    for (TypeMirror typeMirror : directSupertypes) {
        if ( types.isSameType(types.erasure( typeMirror), rawType)){
            return typeMirror;
        }
        TypeMirror found = getAncestor(typeMirror, rawType, types);
        if ( found != null ){
            return found;
        }
    }
    return null;
}
 
Example 18
Source File: ToolTipAnnotation.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Test if typeElement is a parent of currentElement.
 */
private static boolean testParentOf(Types types, TypeMirror currentElement, TypeMirror typeMirror) {
    List<? extends TypeMirror> directSupertypes = types.directSupertypes(currentElement);
    for (TypeMirror superType : directSupertypes) {
        if (superType.equals(typeMirror)) {
            return true;
        } else {
            boolean isParent = testParentOf(types, superType, typeMirror);
            if (isParent) {
                return true;
            }
        }
    }
    return false;
}
 
Example 19
Source File: JavahTask.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitDeclared(DeclaredType t, Types types) {
    t.asElement().getKind(); // ensure class exists
    for (TypeMirror st: types.directSupertypes(t))
        visit(st, types);
    return null;
}
 
Example 20
Source File: JavahTask.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Void visitDeclared(DeclaredType t, Types types) {
    t.asElement().getKind(); // ensure class exists
    for (TypeMirror st: types.directSupertypes(t))
        visit(st, types);
    return null;
}