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

The following examples show how to use javax.lang.model.util.Types#erasure() . 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: UseSuperTypeRefactoringPlugin.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public Tree visitTypeCast(TypeCastTree castTree, Element elementToMatch) {
    TreePath path = getCurrentPath();
    Types types = workingCopy.getTypes();
    TypeMirror castTypeErasure = types.erasure(workingCopy.getTrees().getTypeMirror(path));
    TypeMirror elToMatchErasure = types.erasure(subTypeElement.asType());
    path = path.getParentPath();
    Element element = workingCopy.getTrees().getElement(path);
    if (element instanceof VariableElement && types.isSameType(castTypeErasure, elToMatchErasure)) {
        VariableElement varElement = (VariableElement)element;
        TypeMirror varTypeErasure = types.erasure(varElement.asType());
        if (types.isSameType(varTypeErasure, elToMatchErasure) && isReplaceCandidate(varElement)) {
            TypeCastTree newTree = make.TypeCast(
                make.Identifier(superTypeElement), castTree.getExpression());
            rewrite(castTree, newTree);
        }
    }
    return super.visitTypeCast(castTree, elementToMatch);
}
 
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: MakeSafeTypeVisitor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror visitDeclared(DeclaredType t, Types types) {
    if (TypeModeler.isSubElement((TypeElement) t.asElement(), collectionType)
            || TypeModeler.isSubElement((TypeElement) t.asElement(), mapType)) {
        Collection<? extends TypeMirror> args = t.getTypeArguments();
        TypeMirror[] safeArgs = new TypeMirror[args.size()];
        int i = 0;
        for (TypeMirror arg : args) {
            safeArgs[i++] = visit(arg, types);
        }
        return types.getDeclaredType((TypeElement) t.asElement(), safeArgs);
    }
    return types.erasure(t);
}
 
Example 4
Source File: JavaOperationsImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
@NonNull
public Collection<? extends String> getFieldNames(
        @NonNull final String clz,
        final boolean rt,
        @NullAllowed final String type) throws QueryException {
    final TypeElement te = findClass(clz);
    if (te == null) {
        return Collections.<String>emptyList();
    }
    final Types types = control.getTypes();
    TypeMirror tm = null;
    if (type != null) {
        final List<? extends TypeElement> topLevels = control.getTopLevelElements();
        tm = topLevels.isEmpty() ?
            null :
            control.getTreeUtilities().parseType(type, topLevels.get(0));
        if (tm == null) {
            return Collections.<String>emptyList();
        } else if (rt) {
            tm = types.erasure(tm);
        }
    }
    final Collection<String> result = new ArrayList<String>();
    for (VariableElement ve : ElementFilter.fieldsIn(te.getEnclosedElements())) {
        if (isSameType(types,tm,ve.asType(),rt)) {
            result.add(ve.getSimpleName().toString());
        }
    }
    return Collections.unmodifiableCollection(result);
}
 
Example 5
Source File: CastVSInstanceOf.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@TriggerPatterns({
    @TriggerPattern(value="if ($var instanceof $instanceofClass) {" +
                          "$type $name = ($castClass) $var;" +
                          "$stmts$;" +
                          "}"),
    @TriggerPattern(value="if ($var instanceof $instanceofClass) {" +
                          "final $type $name = ($castClass) $var;" +
                          "$stmts$;" +
                          "}"),
    @TriggerPattern(value="if ($var instanceof $instanceofClass) {" +
                          "$name = ($castClass) $var;" +
                          "$stmts$;" +
                          "}")
})
public static ErrorDescription hint(HintContext ctx) {
    TypeMirror tm1 = ctx.getInfo().getTrees().getTypeMirror(ctx.getVariables().get("$instanceofClass"));
    TreePath castClass = ctx.getVariables().get("$castClass");
    TypeMirror tm2 = ctx.getInfo().getTrees().getTypeMirror(castClass);
    Types t = ctx.getInfo().getTypes();

    if (!(Utilities.isValidType(tm1) && Utilities.isValidType(tm2))) {
        return null;
    }

    tm1 = t.erasure(tm1);
    tm2 = t.erasure(tm2);

    if (t.isSubtype(tm1, tm2)) {
        return null;
    }

    if (t.isSubtype(tm2, tm1)) {
        return null;
    }

    return ErrorDescriptionFactory.forTree(ctx, castClass, NbBundle.getMessage(CastVSInstanceOf.class,"ERR_CastVSInstanceOf"));
}
 
Example 6
Source File: ApNavigator.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public TypeMirror erasure(TypeMirror t) {
    Types tu = env.getTypeUtils();
    t = tu.erasure(t);
    if (t.getKind().equals(TypeKind.DECLARED)) {
        DeclaredType dt = (DeclaredType)t;
        if (!dt.getTypeArguments().isEmpty())
            return tu.getDeclaredType((TypeElement) dt.asElement());
    }
    return t;
}
 
Example 7
Source File: MakeSafeTypeVisitor.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror visitDeclared(DeclaredType t, Types types) {
    if (TypeModeler.isSubElement((TypeElement) t.asElement(), collectionType)
            || TypeModeler.isSubElement((TypeElement) t.asElement(), mapType)) {
        Collection<? extends TypeMirror> args = t.getTypeArguments();
        TypeMirror[] safeArgs = new TypeMirror[args.size()];
        int i = 0;
        for (TypeMirror arg : args) {
            safeArgs[i++] = visit(arg, types);
        }
        return types.getDeclaredType((TypeElement) t.asElement(), safeArgs);
    }
    return types.erasure(t);
}
 
Example 8
Source File: TypeMirrors.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
private static TypeMirror rawType(
    Types typeUtils,
    Elements elementUtils,
    String typeSnippet) {
  TypeElement typeElement = elementUtils.getTypeElement(typeSnippet);
  if (typeElement == null && !typeSnippet.contains(".")) {
    typeElement = elementUtils.getTypeElement("java.lang." + typeSnippet);
  }
  Preconditions.checkArgument(typeElement != null, "Unrecognised type '%s'", typeSnippet);
  return typeUtils.erasure(typeElement.asType());
}
 
Example 9
Source File: ElementUtilities.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**Find all methods in given type and its supertypes, which are overridable.
 * 
 * @param type to inspect
 * @return list of all overridable methods
 *
 * @since 0.136
 */
public List<? extends ExecutableElement> findOverridableMethods(TypeElement type) {
    List<ExecutableElement> overridable = new ArrayList<>();
    final Set<Modifier> notOverridable = EnumSet.copyOf(NOT_OVERRIDABLE);
    if (!type.getModifiers().contains(Modifier.ABSTRACT)) {
        notOverridable.add(Modifier.ABSTRACT);
    }
    DeclaredType dt = (DeclaredType)type.asType();
    Types types = JavacTypes.instance(ctx);
    Set<String> typeStrings = new HashSet<>();
    for (ExecutableElement ee : ElementFilter.methodsIn(info.getElements().getAllMembers(type))) {
        
        TypeMirror methodType = types.erasure(types.asMemberOf(dt, ee));
        String methodTypeString = ee.getSimpleName().toString() + methodType.toString();
        if (typeStrings.contains(methodTypeString)) {
            continue;
        }
        Set<Modifier> set = EnumSet.copyOf(notOverridable);                
        set.removeAll(ee.getModifiers());                
        if (set.size() == notOverridable.size()
                && !overridesPackagePrivateOutsidePackage(ee, type) //do not offer package private methods in case they're from different package
                && !isOverridden(ee, type)) {
            overridable.add(ee);
            if (ee.getModifiers().contains(Modifier.ABSTRACT)) {
                typeStrings.add(methodTypeString);
            }
        }
    }
    Collections.reverse(overridable);
    return overridable;
}
 
Example 10
Source File: ApNavigator.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public TypeMirror erasure(TypeMirror t) {
    Types tu = env.getTypeUtils();
    t = tu.erasure(t);
    if (t.getKind().equals(TypeKind.DECLARED)) {
        DeclaredType dt = (DeclaredType)t;
        if (!dt.getTypeArguments().isEmpty())
            return tu.getDeclaredType((TypeElement) dt.asElement());
    }
    return t;
}
 
Example 11
Source File: MakeSafeTypeVisitor.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror visitDeclared(DeclaredType t, Types types) {
    if (TypeModeler.isSubElement((TypeElement) t.asElement(), collectionType)
            || TypeModeler.isSubElement((TypeElement) t.asElement(), mapType)) {
        Collection<? extends TypeMirror> args = t.getTypeArguments();
        TypeMirror[] safeArgs = new TypeMirror[args.size()];
        int i = 0;
        for (TypeMirror arg : args) {
            safeArgs[i++] = visit(arg, types);
        }
        return types.getDeclaredType((TypeElement) t.asElement(), safeArgs);
    }
    return types.erasure(t);
}
 
Example 12
Source File: ApNavigator.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public TypeMirror erasure(TypeMirror t) {
    Types tu = env.getTypeUtils();
    t = tu.erasure(t);
    if (t.getKind().equals(TypeKind.DECLARED)) {
        DeclaredType dt = (DeclaredType)t;
        if (!dt.getTypeArguments().isEmpty())
            return tu.getDeclaredType((TypeElement) dt.asElement());
    }
    return t;
}
 
Example 13
Source File: MakeSafeTypeVisitor.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
@Override
public TypeMirror visitDeclared(DeclaredType t, Types types) {
    if (TypeModeler.isSubElement((TypeElement) t.asElement(), collectionType)
            || TypeModeler.isSubElement((TypeElement) t.asElement(), mapType)) {
        Collection<? extends TypeMirror> args = t.getTypeArguments();
        TypeMirror[] safeArgs = new TypeMirror[args.size()];
        int i = 0;
        for (TypeMirror arg : args) {
            safeArgs[i++] = visit(arg, types);
        }
        return types.getDeclaredType((TypeElement) t.asElement(), safeArgs);
    }
    return types.erasure(t);
}
 
Example 14
Source File: ApNavigator.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public TypeMirror erasure(TypeMirror t) {
    Types tu = env.getTypeUtils();
    t = tu.erasure(t);
    if (t.getKind().equals(TypeKind.DECLARED)) {
        DeclaredType dt = (DeclaredType)t;
        if (!dt.getTypeArguments().isEmpty())
            return tu.getDeclaredType((TypeElement) dt.asElement());
    }
    return t;
}
 
Example 15
Source File: MakeSafeTypeVisitor.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected TypeMirror defaultAction(TypeMirror e, Types types) {
    return types.erasure(e);
}
 
Example 16
Source File: MakeSafeTypeVisitor.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected TypeMirror defaultAction(TypeMirror e, Types types) {
    return types.erasure(e);
}
 
Example 17
Source File: MakeSafeTypeVisitor.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected TypeMirror defaultAction(TypeMirror e, Types types) {
    return types.erasure(e);
}
 
Example 18
Source File: AttributeAnnotationValidator.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private static TypeMirror getErasure(ProcessingEnvironment processingEnv, final String className)
{
    final Types typeUtils = processingEnv.getTypeUtils();
    final Elements elementUtils = processingEnv.getElementUtils();
    return typeUtils.erasure(elementUtils.getTypeElement(className).asType());
}
 
Example 19
Source File: MakeSafeTypeVisitor.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
@Override
protected TypeMirror defaultAction(TypeMirror e, Types types) {
    return types.erasure(e);
}
 
Example 20
Source File: CompositeTypeConstructorGenerator.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
private static TypeMirror getErasure(ProcessingEnvironment processingEnv, final String className)
{
    final Types typeUtils = processingEnv.getTypeUtils();
    final Elements elementUtils = processingEnv.getElementUtils();
    return typeUtils.erasure(elementUtils.getTypeElement(className).asType());
}