javax.lang.model.type.WildcardType Java Examples

The following examples show how to use javax.lang.model.type.WildcardType. 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: PluginGenerator.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void appendSimpleTypeName(StringBuilder ret, TypeMirror type) {
    switch (type.getKind()) {
        case DECLARED:
            DeclaredType declared = (DeclaredType) type;
            TypeElement element = (TypeElement) declared.asElement();
            ret.append(element.getSimpleName());
            break;
        case TYPEVAR:
            appendSimpleTypeName(ret, ((TypeVariable) type).getUpperBound());
            break;
        case WILDCARD:
            appendSimpleTypeName(ret, ((WildcardType) type).getExtendsBound());
            break;
        case ARRAY:
            appendSimpleTypeName(ret, ((ArrayType) type).getComponentType());
            ret.append("Array");
            break;
        default:
            ret.append(type);
    }
}
 
Example #2
Source File: JavaSymbolProvider.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder visitWildcard(WildcardType t, Boolean p) {
    int len = DEFAULT_VALUE.length();
    DEFAULT_VALUE.append("?"); //NOI18N
    TypeMirror bound = t.getSuperBound();
    if (bound == null) {
        bound = t.getExtendsBound();
        if (bound != null) {
            DEFAULT_VALUE.append(" extends "); //NOI18N
            if (bound.getKind() == TypeKind.WILDCARD) {
                bound = ((WildcardType)bound).getSuperBound();
            }
            visit(bound, p);
        } else if (len == 0) {
            bound = SourceUtils.getBound(t);
            if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N
                DEFAULT_VALUE.append(" extends "); //NOI18N
                visit(bound, p);
            }
        }
    } else {
        DEFAULT_VALUE.append(" super "); //NOI18N
        visit(bound, p);
    }
    return DEFAULT_VALUE;
}
 
Example #3
Source File: TypeUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm) {
    if (tm == null) {
        return tm;
    }
    if (tm.getKind() == TypeKind.ERROR) {
        tm = info.getTrees().getOriginalType((ErrorType) tm);
    }
    TypeMirror type = resolveCapturedTypeInt(info, tm);
    if (type == null) {
        return tm;
    }
    if (type.getKind() == TypeKind.WILDCARD) {
        TypeMirror tmirr = ((WildcardType) type).getExtendsBound();
        if (tmirr != null)
            return tmirr;
        else { //no extends, just '?'
            TypeElement te = info.getElements().getTypeElement("java.lang.Object"); // NOI18N
            return te == null ? null : te.asType();
        }
            
    }
    
    return type;
}
 
Example #4
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm) {
    if (tm == null) {
        return tm;
    }
    if (tm.getKind() == TypeKind.ERROR) {
        tm = info.getTrees().getOriginalType((ErrorType) tm);
    }
    TypeMirror type = resolveCapturedTypeInt(info, tm);
    if (type == null) {
        return tm;
    }
    if (type.getKind() == TypeKind.WILDCARD) {
        TypeMirror tmirr = ((WildcardType) type).getExtendsBound();
        if (tmirr != null)
            return tmirr;
        else { //no extends, just '?'
            TypeElement te = info.getElements().getTypeElement("java.lang.Object"); // NOI18N
            return te == null ? null : te.asType();
        }
            
    }
    
    return type;
}
 
Example #5
Source File: TypeUtilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder visitWildcard(WildcardType t, Boolean p) {
    int len = DEFAULT_VALUE.length();
    DEFAULT_VALUE.append("?"); //NOI18N
    TypeMirror bound = t.getSuperBound();
    if (bound == null) {
        bound = t.getExtendsBound();
        if (bound != null) {
            DEFAULT_VALUE.append(" extends "); //NOI18N
            if (bound.getKind() == TypeKind.WILDCARD)
                bound = ((WildcardType)bound).getSuperBound();
            visit(bound, p);
        } else if (len == 0) {
            bound = SourceUtils.getBound(t);
            if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N
                DEFAULT_VALUE.append(" extends "); //NOI18N
                visit(bound, p);
            }
        }
    } else {
        DEFAULT_VALUE.append(" super "); //NOI18N
        visit(bound, p);
    }
    return DEFAULT_VALUE;
}
 
Example #6
Source File: SourceUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Resolves all captured type variables to their respective wildcards in the given type.
 * @param info CompilationInfo over which the method should work
 * @param tm type to resolve
 * @return resolved type
 * 
 * @since 0.136
 */
public static TypeMirror resolveCapturedType(CompilationInfo info, TypeMirror tm) {
    TypeMirror type = resolveCapturedTypeInt(info, tm);
    
    if (type.getKind() == TypeKind.WILDCARD) {
        TypeMirror tmirr = ((WildcardType) type).getExtendsBound();
        tmirr = tmirr != null ? tmirr : ((WildcardType) type).getSuperBound();
        if (tmirr != null) {
            return tmirr;
        } else { //no extends, just '?
            TypeElement tel = info.getElements().getTypeElement("java.lang.Object"); // NOI18N
            return tel == null ? null : tel.asType();
        }
            
    }
    
    return type;
}
 
Example #7
Source File: SpringXMLConfigCompletionItem.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder visitWildcard(WildcardType t, Boolean p) {
    DEFAULT_VALUE.append("?"); //NOI18N
    TypeMirror bound = t.getSuperBound();
    if (bound == null) {
        bound = t.getExtendsBound();
        if (bound != null) {
            DEFAULT_VALUE.append(" extends "); //NOI18N
            if (bound.getKind() == TypeKind.WILDCARD)
                bound = ((WildcardType)bound).getSuperBound();
            visit(bound, p);
        } else {
            bound = SourceUtils.getBound(t);
            if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N
                DEFAULT_VALUE.append(" extends "); //NOI18N
                visit(bound, p);
            }
        }
    } else {
        DEFAULT_VALUE.append(" super "); //NOI18N
        visit(bound, p);
    }
    return DEFAULT_VALUE;
}
 
Example #8
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static void containedTypevarsRecursively(@NonNull TypeMirror tm, @NonNull Collection<TypeVariable> typeVars) {
    switch (tm.getKind()) {
        case TYPEVAR:
            typeVars.add((TypeVariable) tm);
            break;
        case DECLARED:
            DeclaredType type = (DeclaredType) tm;
            for (TypeMirror t : type.getTypeArguments()) {
                containedTypevarsRecursively(t, typeVars);
            }

            break;
        case ARRAY:
            containedTypevarsRecursively(((ArrayType) tm).getComponentType(), typeVars);
            break;
        case WILDCARD:
            if (((WildcardType) tm).getExtendsBound() != null) {
                containedTypevarsRecursively(((WildcardType) tm).getExtendsBound(), typeVars);
            }
            if (((WildcardType) tm).getSuperBound() != null) {
                containedTypevarsRecursively(((WildcardType) tm).getSuperBound(), typeVars);
            }
            break;
    }
}
 
Example #9
Source File: ExpectedTypeResolver.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private TypeMirror decapture(TypeMirror argm) {
    if (argm instanceof CapturedType) {
        argm = ((CapturedType)argm).wildcard;
    }
    if (argm.getKind() == TypeKind.WILDCARD) {
        WildcardType wctype = (WildcardType)argm;
        TypeMirror bound = wctype.getExtendsBound();
        if (bound != null) {
            return bound;
        } 
        bound = wctype.getSuperBound();
        if (bound != null) {
            return bound;
        }
        return null;
    } 
    return argm;
}
 
Example #10
Source File: MethodModelSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder visitWildcard(WildcardType t, Boolean p) {
    int len = DEFAULT_VALUE.length();
    DEFAULT_VALUE.append("?"); //NOI18N
    TypeMirror bound = t.getSuperBound();
    if (bound == null) {
        bound = t.getExtendsBound();
        if (bound != null) {
            DEFAULT_VALUE.append(" extends "); //NOI18N
            if (bound.getKind() == TypeKind.WILDCARD)
                bound = ((WildcardType)bound).getSuperBound();
            visit(bound, p);
        } else if (len == 0) {
            bound = SourceUtils.getBound(t);
            if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N
                DEFAULT_VALUE.append(" extends "); //NOI18N
                visit(bound, p);
            }
        }
    } else {
        DEFAULT_VALUE.append(" super "); //NOI18N
        visit(bound, p);
    }
    return DEFAULT_VALUE;
}
 
Example #11
Source File: Utilities.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
public StringBuilder visitWildcard(WildcardType t, Boolean p) {
    int len = DEFAULT_VALUE.length();
    DEFAULT_VALUE.append("?"); //NOI18N
    TypeMirror bound = t.getSuperBound();
    if (bound == null) {
        bound = t.getExtendsBound();
        if (bound != null) {
            DEFAULT_VALUE.append(" extends "); //NOI18N
            if (bound.getKind() == TypeKind.WILDCARD)
                bound = ((WildcardType)bound).getSuperBound();
            visit(bound, p);
        } else if (len == 0) {
            bound = SourceUtils.getBound(t);
            if (bound != null && (bound.getKind() != TypeKind.DECLARED || !((TypeElement)((DeclaredType)bound).asElement()).getQualifiedName().contentEquals("java.lang.Object"))) { //NOI18N
                DEFAULT_VALUE.append(" extends "); //NOI18N
                visit(bound, p);
            }
        }
    } else {
        DEFAULT_VALUE.append(" super "); //NOI18N
        visit(bound, p);
    }
    return DEFAULT_VALUE;
}
 
Example #12
Source File: AbstractAssignabilityChecker.java    From netbeans with Apache License 2.0 6 votes vote down vote up
protected boolean handleWildCard( TypeMirror argType, WildcardType varTypeArg,
        Types types )
{
    TypeMirror upperBound = varTypeArg.getExtendsBound();
    TypeMirror lowerBound = varTypeArg.getSuperBound();

    if ( argType instanceof ReferenceType && 
            argType.getKind()!=TypeKind.TYPEVAR)
    {
        return handleWildCardActualType(argType, types, upperBound,
                lowerBound);
    }            
    
    if ( argType.getKind() == TypeKind.TYPEVAR ){
        return handleWildCardTypeVar(argType, types, upperBound, lowerBound);
    }
    
    return false;
}
 
Example #13
Source File: BeanModelBuilder.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void addDependency(TypeMirror tm) {
    if (tm.getKind() == TypeKind.ARRAY) {
        addDependency(((ArrayType)tm).getComponentType());
    } else if (tm.getKind() == TypeKind.WILDCARD) {
        WildcardType wt = (WildcardType)tm;
        TypeMirror bound = wt.getSuperBound();
        if (bound == null) {
            bound = wt.getExtendsBound();
        }
        addDependency(bound);
    } else if (tm.getKind() == TypeKind.DECLARED) {
        addDependency(
            ((TypeElement)compilationInfo.getTypes().asElement(tm)).getQualifiedName().toString()
        );
    }
}
 
Example #14
Source File: GeneratedPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
static String getErasedType(TypeMirror type) {
    switch (type.getKind()) {
        case DECLARED:
            DeclaredType declared = (DeclaredType) type;
            TypeElement element = (TypeElement) declared.asElement();
            return element.getQualifiedName().toString();
        case TYPEVAR:
            return getErasedType(((TypeVariable) type).getUpperBound());
        case WILDCARD:
            return getErasedType(((WildcardType) type).getExtendsBound());
        case ARRAY:
            return getErasedType(((ArrayType) type).getComponentType()) + "[]";
        default:
            return type.toString();
    }
}
 
Example #15
Source File: AbstractTestGenerator.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private TypeMirror getTypeMirror(WorkingCopy workingCopy, TypeMirror retType) {
if (retType.getKind() == TypeKind.DECLARED) {
    List<? extends TypeMirror> typeArguments = ((DeclaredType) retType).getTypeArguments();
    for (TypeMirror argument : typeArguments) {
	if (argument.getKind() == TypeKind.WILDCARD) {
	    TypeMirror extendsBound = ((WildcardType) argument).getExtendsBound();
	    TypeMirror superBound = ((WildcardType) argument).getSuperBound();
	    if(extendsBound == null && superBound == null) {
		return workingCopy.getTypes().erasure(retType);
	    }
	    if (extendsBound != null) {
		if (shouldApplyErasure(extendsBound)) {
		    return workingCopy.getTypes().erasure(retType);
		}
	    }
	    if (superBound != null) {
		if (shouldApplyErasure(superBound)) {
		    return workingCopy.getTypes().erasure(retType);
		}
	    }
	} else if (argument.getKind() == TypeKind.DECLARED) {
	    if (((DeclaredType) argument).asElement().getModifiers().contains(Modifier.PRIVATE)) {
		return workingCopy.getTypes().erasure(retType);
	    }
	} else {
	    return workingCopy.getTypes().erasure(retType);
	}
    }
} else {
    return workingCopy.getTypes().erasure(retType);
}
return retType;
   }
 
Example #16
Source File: DeclaredTypeCollector.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitWildcard(WildcardType t, Collection<DeclaredType> p) {
    if (t.getExtendsBound() != null) {
        visit(t.getExtendsBound(), p);
    }
    if (t.getSuperBound() != null) {
        visit(t.getSuperBound(), p);
    }
    return DEFAULT_VALUE;
}
 
Example #17
Source File: TypeMirrorAppender.java    From FreeBuilder with Apache License 2.0 5 votes vote down vote up
@Override
public Void visitWildcard(WildcardType t, QualifiedNameAppendable a) {
  a.append("?");
  if (t.getSuperBound() != null) {
    a.append(" super ");
    t.getSuperBound().accept(this, a);
  }
  if (t.getExtendsBound() != null) {
    a.append(" extends ");
    t.getExtendsBound().accept(this, a);
  }
  return null;
}
 
Example #18
Source File: TypeSimplifier.java    From SimpleWeibo with Apache License 2.0 5 votes vote down vote up
@Override public StringBuilder visitWildcard(WildcardType type, StringBuilder sb) {
  sb.append("?");
  TypeMirror extendsBound = type.getExtendsBound();
  TypeMirror superBound = type.getSuperBound();
  if (superBound != null) {
    sb.append(" super ");
    visit(superBound, sb);
  } else if (extendsBound != null) {
    sb.append(" extends ");
    visit(extendsBound, sb);
  }
  return sb;
}
 
Example #19
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
@Override public Void visitWildcard(WildcardType t, Void p) {
  for (TypeMirror bound : new TypeMirror[] {t.getSuperBound(), t.getExtendsBound()}) {
    if (bound != null) {
      visit(bound, p);
    }
  }
  return null;
}
 
Example #20
Source File: SourceUtilsTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testGetBound() throws Exception {
    //only a scatch of the test, add testcases as needed:
    prepareTest();

    TypeElement test = info.getElements().getTypeElement("sourceutils.TestGetBound");

    assertNotNull(test);

    TypeParameterElement typeParam = test.getTypeParameters().get(0);
    TypeMirror outerBound = ((TypeVariable) typeParam.asType()).getUpperBound();

    TypeMirror bound = SourceUtils.getBound((WildcardType) ((DeclaredType) outerBound).getTypeArguments().get(0));
    assertEquals("java.lang.CharSequence", String.valueOf(bound));
}
 
Example #21
Source File: EnableBeansFilter.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private DeclaredType getDeclaredType( TypeMirror type ){
    if ( type instanceof DeclaredType && type.getKind()!= TypeKind.ERROR){
        return (DeclaredType)type;
    }
    if ( type instanceof TypeVariable ){
        TypeMirror upperBound = ((TypeVariable)type).getUpperBound();
        return getDeclaredType( upperBound );
    }
    else if ( type instanceof WildcardType ){
        TypeMirror extendsBound = ((WildcardType)type).getExtendsBound();
        return getDeclaredType( extendsBound );
    }
    return null;
}
 
Example #22
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
@Override public StringBuilder visitWildcard(WildcardType type, StringBuilder sb) {
  sb.append("?");
  TypeMirror extendsBound = type.getExtendsBound();
  TypeMirror superBound = type.getSuperBound();
  if (superBound != null) {
    sb.append(" super ");
    visit(superBound, sb);
  } else if (extendsBound != null) {
    sb.append(" extends ");
    visit(extendsBound, sb);
  }
  return sb;
}
 
Example #23
Source File: TurbineTypeMirrorTest.java    From turbine with Apache License 2.0 5 votes vote down vote up
@Test
public void wildTy() {
  WildcardType lower =
      (WildcardType)
          factory.asTypeMirror(
              Type.WildLowerBoundedTy.create(
                  Type.ClassTy.asNonParametricClassTy(new ClassSymbol("java/lang/Integer")),
                  ImmutableList.of()));
  WildcardType upper =
      (WildcardType)
          factory.asTypeMirror(
              Type.WildUpperBoundedTy.create(
                  Type.ClassTy.asNonParametricClassTy(new ClassSymbol("java/lang/Long")),
                  ImmutableList.of()));
  WildcardType unbound =
      (WildcardType) factory.asTypeMirror(Type.WildUnboundedTy.create(ImmutableList.of()));

  assertThat(lower.getKind()).isEqualTo(TypeKind.WILDCARD);
  assertThat(lower.getExtendsBound()).isNull();
  assertThat(lower.getSuperBound().getKind()).isEqualTo(TypeKind.DECLARED);

  assertThat(upper.getKind()).isEqualTo(TypeKind.WILDCARD);
  assertThat(upper.getExtendsBound().getKind()).isEqualTo(TypeKind.DECLARED);
  assertThat(upper.getSuperBound()).isNull();

  assertThat(unbound.getKind()).isEqualTo(TypeKind.WILDCARD);
  assertThat(unbound.getExtendsBound()).isNull();
  assertThat(unbound.getSuperBound()).isNull();
}
 
Example #24
Source File: TurbineTypes.java    From turbine with Apache License 2.0 5 votes vote down vote up
@Override
public WildcardType getWildcardType(TypeMirror extendsBound, TypeMirror superBound) {
  WildTy type;
  if (extendsBound != null) {
    type = WildTy.WildUpperBoundedTy.create(asTurbineType(extendsBound), ImmutableList.of());
  } else if (superBound != null) {
    type = WildTy.WildLowerBoundedTy.create(asTurbineType(superBound), ImmutableList.of());
  } else {
    type = WildUnboundedTy.create(ImmutableList.of());
  }
  return (WildcardType) factory.asTypeMirror(type);
}
 
Example #25
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
@Override public boolean apply(TypeMirror arg) {
  if (arg.getKind() == TypeKind.WILDCARD) {
    WildcardType wildcard = (WildcardType) arg;
    if (wildcard.getExtendsBound() == null || isJavaLangObject(wildcard.getExtendsBound())) {
      // This is <?>, unless there's a super bound, in which case it is <? super Foo> and
      // is erased.
      return (wildcard.getSuperBound() != null);
    }
  }
  return true;
}
 
Example #26
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
@Override public Void visitWildcard(WildcardType t, Void p) {
  for (TypeMirror bound : new TypeMirror[] {t.getSuperBound(), t.getExtendsBound()}) {
    if (bound != null) {
      visit(bound, p);
    }
  }
  return null;
}
 
Example #27
Source File: TypeSimplifier.java    From SimpleWeibo with Apache License 2.0 5 votes vote down vote up
@Override public boolean apply(TypeMirror arg) {
  if (arg.getKind() == TypeKind.WILDCARD) {
    WildcardType wildcard = (WildcardType) arg;
    if (wildcard.getExtendsBound() == null || isJavaLangObject(wildcard.getExtendsBound())) {
      // This is <?>, unless there's a super bound, in which case it is <? super Foo> and
      // is erased.
      return (wildcard.getSuperBound() != null);
    }
  }
  return true;
}
 
Example #28
Source File: MoreTypes.java    From auto-parcel with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a {@link WildcardType} if the {@link TypeMirror} represents a wildcard type
 * or throws an {@link IllegalArgumentException}.
 */
public static WildcardType asWildcard(WildcardType maybeWildcardType) {
    return maybeWildcardType.accept(new CastingTypeVisitor<WildcardType>() {
        @Override
        public WildcardType visitWildcard(WildcardType type, String p) {
            return type;
        }
    }, "wildcard type");
}
 
Example #29
Source File: TypeSimplifier.java    From SimpleWeibo with Apache License 2.0 5 votes vote down vote up
@Override public Void visitWildcard(WildcardType t, Void p) {
  for (TypeMirror bound : new TypeMirror[] {t.getSuperBound(), t.getExtendsBound()}) {
    if (bound != null) {
      visit(bound, p);
    }
  }
  return null;
}
 
Example #30
Source File: TypeSimplifier.java    From RetroFacebook with Apache License 2.0 5 votes vote down vote up
@Override public boolean apply(TypeMirror arg) {
  if (arg.getKind() == TypeKind.WILDCARD) {
    WildcardType wildcard = (WildcardType) arg;
    if (wildcard.getExtendsBound() == null || isJavaLangObject(wildcard.getExtendsBound())) {
      // This is <?>, unless there's a super bound, in which case it is <? super Foo> and
      // is erased.
      return (wildcard.getSuperBound() != null);
    }
  }
  return true;
}