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 Project: netbeans   Author: apache   File: JavaSymbolProvider.java    License: 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 #2
Source Project: netbeans   Author: apache   File: TypeUtilities.java    License: 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 #3
Source Project: netbeans   Author: apache   File: TypeUtilities.java    License: 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 #4
Source Project: netbeans   Author: apache   File: SourceUtils.java    License: 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 #5
Source Project: netbeans   Author: apache   File: SpringXMLConfigCompletionItem.java    License: 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 #6
Source Project: netbeans   Author: apache   File: Utilities.java    License: 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 #7
Source Project: netbeans   Author: apache   File: Utilities.java    License: 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 #8
Source Project: netbeans   Author: apache   File: ExpectedTypeResolver.java    License: 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 #9
Source Project: netbeans   Author: apache   File: MethodModelSupport.java    License: 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 #10
Source Project: netbeans   Author: apache   File: Utilities.java    License: 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 Project: netbeans   Author: apache   File: AbstractAssignabilityChecker.java    License: 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 #12
Source Project: netbeans   Author: apache   File: BeanModelBuilder.java    License: 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 #13
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: GeneratedPlugin.java    License: 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 #14
Source Project: openjdk-jdk9   Author: AdoptOpenJDK   File: PluginGenerator.java    License: 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 #15
Source Project: netbeans   Author: apache   File: TypeMirrorHandle.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Void visitWildcardType(Type.WildcardType t, Void s) {
    if (t.type instanceof PlaceholderType)
        t.type = ((PlaceholderType)t.type).delegate;
    else if (t.type != null)
        t.type.accept(this, s);
    if (t.bound != null)
        t.bound.accept(this, s);
    return null;
}
 
Example #16
Source Project: netbeans   Author: apache   File: SourceUtils.java    License: Apache License 2.0 5 votes vote down vote up
public static TypeMirror getBound(WildcardType wildcardType) {
    Type.TypeVar bound = ((Type.WildcardType)wildcardType).bound;
    try {
        return bound != null ? bound.bound : null;
    } catch (NoSuchFieldError err) {
        return bound != null ? bound.getUpperBound() : null;
    }
}
 
Example #17
Source Project: netbeans   Author: apache   File: SourceUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @since 0.24
 */
public static WildcardType resolveCapturedType(TypeMirror type) {
    if (type instanceof Type.CapturedType) {
        return ((Type.CapturedType) type).wildcard;
    } else {
        return null;
    }
}
 
Example #18
Source Project: netbeans   Author: apache   File: SourceUtilsTest.java    License: 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 #19
Source Project: netbeans   Author: apache   File: AbstractTestGenerator.java    License: 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 #20
Source Project: netbeans   Author: apache   File: DeclaredTypeCollector.java    License: 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 #21
Source Project: netbeans   Author: apache   File: Utilities.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean containsErrorsRecursively(TypeMirror tm) {
    switch (tm.getKind()) {
        case ERROR:
            return true;
        case DECLARED:
            DeclaredType type = (DeclaredType) tm;

            for (TypeMirror t : type.getTypeArguments()) {
                if (containsErrorsRecursively(t))
                    return true;
            }

            return false;
        case ARRAY:
            return containsErrorsRecursively(((ArrayType) tm).getComponentType());
        case WILDCARD:
            if (((WildcardType) tm).getExtendsBound() != null && containsErrorsRecursively(((WildcardType) tm).getExtendsBound())) {
                return true;
            }
            if (((WildcardType) tm).getSuperBound() != null && containsErrorsRecursively(((WildcardType) tm).getSuperBound())) {
                return true;
            }
            return false;
        case OTHER:
            return true;
        default:
            return false;
    }
}
 
Example #22
Source Project: SimpleWeibo   Author: 8tory   File: TypeSimplifier.java    License: 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 Project: netbeans   Author: apache   File: EnableBeansFilter.java    License: 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 #24
Source Project: turbine   Author: google   File: TurbineTypes.java    License: 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 Project: turbine   Author: google   File: TurbineTypeMirrorTest.java    License: 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 #26
Source Project: auto-parcel   Author: aitorvs   File: MoreTypes.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Boolean visitWildcard(WildcardType a, EqualVisitorParam p) {
    if (p.type.getKind().equals(WILDCARD)) {
        WildcardType b = (WildcardType) p.type;
        return equal(a.getExtendsBound(), b.getExtendsBound(), p.visiting)
                && equal(a.getSuperBound(), b.getSuperBound(), p.visiting);
    }
    return false;
}
 
Example #27
Source Project: SimpleWeibo   Author: 8tory   File: TypeSimplifier.java    License: 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 #28
Source Project: auto-parcel   Author: aitorvs   File: MoreTypes.java    License: 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 Project: SimpleWeibo   Author: 8tory   File: TypeSimplifier.java    License: 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 #30
Source Project: RetroFacebook   Author: yongjhih   File: TypeSimplifier.java    License: 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;
}