本文整理汇总了Java中org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType.getTypeArguments方法的典型用法代码示例。如果您正苦于以下问题:Java AnnotatedDeclaredType.getTypeArguments方法的具体用法?Java AnnotatedDeclaredType.getTypeArguments怎么用?Java AnnotatedDeclaredType.getTypeArguments使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType
的用法示例。
在下文中一共展示了AnnotatedDeclaredType.getTypeArguments方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: containsModifierImpl
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType; //导入方法依赖的package包/类
private static boolean containsModifierImpl(AnnotatedTypeMirror type, AnnotationMirror modifier,
List<AnnotatedTypeMirror> visited) {
boolean found = type.hasAnnotation(modifier);
boolean vis = visited.contains(type);
visited.add(type);
if (!found && !vis) {
if (type.getKind() == TypeKind.DECLARED) {
AnnotatedDeclaredType declaredType = (AnnotatedDeclaredType) type;
for (AnnotatedTypeMirror typeMirror : declaredType.getTypeArguments()) {
found |= containsModifierImpl(typeMirror, modifier, visited);
if (found) {
break;
}
}
} else if (type.getKind() == TypeKind.ARRAY) {
AnnotatedArrayType arrayType = (AnnotatedArrayType) type;
found = containsModifierImpl(arrayType.getComponentType(), modifier, visited);
} else if (type.getKind() == TypeKind.TYPEVAR) {
AnnotatedTypeVariable atv = (AnnotatedTypeVariable) type;
if (atv.getUpperBound() != null) {
found = containsModifierImpl(atv.getUpperBound(), modifier, visited);
}
if (!found && atv.getLowerBound() != null) {
found = containsModifierImpl(atv.getLowerBound(), modifier, visited);
}
} else if (type.getKind() == TypeKind.WILDCARD) {
AnnotatedWildcardType awc = (AnnotatedWildcardType) type;
if (awc.getExtendsBound() != null) {
found = containsModifierImpl(awc.getExtendsBound(), modifier, visited);
}
if (!found && awc.getSuperBound() != null) {
found = containsModifierImpl(awc.getSuperBound(), modifier, visited);
}
}
}
return found;
}
示例2: isSubtypeTypeArguments
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType; //导入方法依赖的package包/类
/**
* Checks that rhs and lhs are subtypes with respect to type arguments only.
* Returns true if any of the provided types is not a parameterized type.
*
* A parameterized type, rhs, is a subtype of another, lhs, only if their
* actual type parameters are invariant.
*
* <p>
*
* As an implementation detail, this method uses
* {@link #isSubtypeAsTypeArgument(AnnotatedTypeMirror, AnnotatedTypeMirror)}
* to compare each type argument individually. Subclasses may override
* either methods to allow type argument to change covariantly.
*
* @return true iff the type arguments of lhs and rhs are invariant.
*/
protected boolean isSubtypeTypeArguments(AnnotatedDeclaredType rhs, AnnotatedDeclaredType lhs) {
if (ignoreRawTypeArguments(rhs, lhs)) {
return true;
}
List<AnnotatedTypeMirror> rhsTypeArgs = rhs.getTypeArguments();
List<AnnotatedTypeMirror> lhsTypeArgs = lhs.getTypeArguments();
if (rhsTypeArgs.isEmpty() || lhsTypeArgs.isEmpty())
return true;
if (lhsTypeArgs.size() != rhsTypeArgs.size()) {
// System.out.println("Mismatch between " + lhsTypeArgs + " and " + rhsTypeArgs
// + " in types " + lhs + " and " + rhs);
// This happened in javari/RandomTests, where we have:
// List<String> l = (List<String>) new HashMap<String, String>();
// Shouldn't rhs and lhs be first brought to the same base type, before comparing the
// type arguments?
// When compiling that line with javac, one gets an unchecked
// warning.
// TODO
return true;
}
for (int i = 0; i < lhsTypeArgs.size(); ++i) {
if (!isSubtypeAsTypeArgument(rhsTypeArgs.get(i), lhsTypeArgs.get(i)))
return false;
}
return true;
}
示例3: substituteCall
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType; //导入方法依赖的package包/类
private AnnotatedTypeMirror substituteCall(MethodInvocationTree call, AnnotatedTypeMirror inType) {
// System.out.println("input type: " + inType);
AnnotatedTypeMirror outType = inType.getCopy(true);
AnnotationMirror anno = inType.getAnnotation(KeyFor.class);
if (anno != null) {
List<String> inMaps = AnnotationUtils.getElementValueArray(anno, "value", String.class, false);
List<String> outMaps = new ArrayList<String>();
String receiver = receiver(call);
for (String inMapName : inMaps) {
if (parameterPtn.matcher(inMapName).matches()) {
int param = Integer.valueOf(inMapName.substring(1));
if (param <= 0 || param > call.getArguments().size()) {
// The failure should already have been reported, when the
// method declaration was processed.
// checker.report(Result.failure("param.index.nullness.parse.error", inMapName), call);
} else {
String res = call.getArguments().get(param-1).toString();
outMaps.add(res);
}
} else if (inMapName.equals("this")) {
outMaps.add(receiver);
} else {
// TODO: look at the code below, copied from NullnessFlow
// System.out.println("KeyFor argument unhandled: " + inMapName + " using " + receiver + "." + inMapName);
// do not always add the receiver, e.g. for local variables this creates a mess
// outMaps.add(receiver + "." + inMapName);
// just copy name for now, better than doing nothing
outMaps.add(inMapName);
}
// TODO: look at code in NullnessFlow and decide whether there
// are more cases to copy.
}
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, KeyFor.class);
builder.setValue("value", outMaps);
AnnotationMirror newAnno = builder.build();
outType.removeAnnotation(KeyFor.class);
outType.addAnnotation(newAnno);
}
if (outType.getKind() == TypeKind.DECLARED) {
AnnotatedDeclaredType declaredType = (AnnotatedDeclaredType) outType;
Map<AnnotatedTypeMirror, AnnotatedTypeMirror> mapping = new HashMap<AnnotatedTypeMirror, AnnotatedTypeMirror>();
// Get the substituted type arguments
for (AnnotatedTypeMirror typeArgument : declaredType.getTypeArguments()) {
AnnotatedTypeMirror substTypeArgument = substituteCall(call, typeArgument);
mapping.put(typeArgument, substTypeArgument);
}
outType = declaredType.substitute(mapping);
} else if (outType.getKind() == TypeKind.ARRAY) {
AnnotatedArrayType arrayType = (AnnotatedArrayType) outType;
// Get the substituted component type
AnnotatedTypeMirror elemType = arrayType.getComponentType();
AnnotatedTypeMirror substElemType = substituteCall(call, elemType);
arrayType.setComponentType(substElemType);
// outType aliases arrayType
} else if(outType.getKind().isPrimitive() ||
outType.getKind() == TypeKind.WILDCARD ||
outType.getKind() == TypeKind.TYPEVAR) {
// TODO: for which of these should we also recursively substitute?
// System.out.println("KeyForATF: Intentionally unhandled Kind: " + outType.getKind());
} else {
// System.err.println("KeyForATF: Unknown getKind(): " + outType.getKind());
// assert false;
}
// System.out.println("result type: " + outType);
return outType;
}
示例4: isSubtypeTypeArguments
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType; //导入方法依赖的package包/类
@Override
protected boolean isSubtypeTypeArguments(AnnotatedDeclaredType rhs, AnnotatedDeclaredType lhs) {
if (ignoreRawTypeArguments(rhs, lhs)) {
return true;
}
List<AnnotatedTypeMirror> rhsTypeArgs = rhs.getTypeArguments();
List<AnnotatedTypeMirror> lhsTypeArgs = lhs.getTypeArguments();
if (rhsTypeArgs.isEmpty() || lhsTypeArgs.isEmpty())
return true;
TypeElement lhsElem = (TypeElement) lhs.getUnderlyingType().asElement();
// TypeElement rhsElem = (TypeElement) lhs.getUnderlyingType().asElement();
// the following would be needed if Covariant were per type parameter
// AnnotatedDeclaredType lhsDecl = currentATF.fromElement(lhsElem);
// AnnotatedDeclaredType rhsDecl = currentATF.fromElement(rhsElem);
// List<AnnotatedTypeMirror> lhsTVs = lhsDecl.getTypeArguments();
// List<AnnotatedTypeMirror> rhsTVs = rhsDecl.getTypeArguments();
// TODO: implementation of @Covariant should be done in the standard TypeHierarchy
int[] covarVals = null;
if (lhsElem.getAnnotation(Covariant.class) != null) {
covarVals = lhsElem.getAnnotation(Covariant.class).value();
}
if (lhsTypeArgs.size() != rhsTypeArgs.size()) {
// This test fails e.g. for casts from a type with one type
// argument to a type with two type arguments.
// See test case nullness/generics/GenericsCasts
// TODO: shouldn't the type be brought to a common type before
// this?
return true;
}
for (int i = 0; i < lhsTypeArgs.size(); ++i) {
boolean covar = false;
if (covarVals != null) {
for (int cvv = 0; cvv < covarVals.length; ++cvv) {
if (covarVals[cvv] == i) {
covar = true;
}
}
}
if (covar) {
if (!isSubtype(rhsTypeArgs.get(i), lhsTypeArgs.get(i)))
// TODO: still check whether isSubtypeAsTypeArgument returns true.
// This handles wildcards better.
return isSubtypeAsTypeArgument(rhsTypeArgs.get(i), lhsTypeArgs.get(i));
} else {
if (!isSubtypeAsTypeArgument(rhsTypeArgs.get(i), lhsTypeArgs.get(i)))
return false;
}
}
return true;
}
示例5: fromTypeTree
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType; //导入方法依赖的package包/类
/**
* Determines the annotated type from a type in tree form. This method
* does not add implicit annotations.
*
* @param tree the type tree
* @return the annotated type of the type in the AST
*/
public AnnotatedTypeMirror fromTypeTree(Tree tree) {
if (fromTreeCache.containsKey(tree) && shouldReadCache) {
return AnnotatedTypes.deepCopy(fromTreeCache.get(tree));
}
AnnotatedTypeMirror result = fromTreeWithVisitor(
TypeFromTree.TypeFromTypeTreeINSTANCE, tree);
// treat Raw as generic!
// TODO: This doesn't handle recursive type parameter
// e.g. class Pair<Y extends List<Y>> { ... }
if (result.getKind() == TypeKind.DECLARED) {
AnnotatedDeclaredType dt = (AnnotatedDeclaredType)result;
if (dt.wasRaw()) {
List<AnnotatedTypeMirror> typeArgs;
Pair<Tree, AnnotatedTypeMirror> ctx = this.visitorState.getAssignmentContext();
if (ctx != null) {
if (ctx.second.getKind() == TypeKind.DECLARED &&
types.isSameType(types.erasure(ctx.second.actualType), types.erasure(dt.actualType))) {
typeArgs = ((AnnotatedDeclaredType) ctx.second).getTypeArguments();
} else {
// TODO: we want a way to go from the raw type to an instantiation of the raw type
// that is compatible with the context.
typeArgs = null;
}
} else {
// TODO: the context is null, use uninstantiated wildcards instead.
typeArgs = new ArrayList<AnnotatedTypeMirror>();
AnnotatedDeclaredType declaration = fromElement((TypeElement)dt.getUnderlyingType().asElement());
for (AnnotatedTypeMirror typeParam : declaration.getTypeArguments()) {
AnnotatedWildcardType wct = getUninferredWildcardType((AnnotatedTypeVariable) typeParam);
typeArgs.add(wct);
}
}
dt.setTypeArguments(typeArgs);
}
}
annotateInheritedFromClass(result);
if (shouldCache)
fromTreeCache.put(tree, AnnotatedTypes.deepCopy(result));
return result;
}
示例6: fromNewClassContextHelper
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType; //导入方法依赖的package包/类
private void fromNewClassContextHelper(AnnotatedDeclaredType type, AnnotatedTypeMirror ctxtype) {
switch (ctxtype.getKind()) {
case DECLARED:
AnnotatedDeclaredType adctx = (AnnotatedDeclaredType) ctxtype;
if (type.getTypeArguments().size() == adctx.getTypeArguments().size()) {
// Try to simply take the type arguments from LHS.
List<AnnotatedTypeMirror> oldArgs = type.getTypeArguments();
List<AnnotatedTypeMirror> newArgs = adctx.getTypeArguments();
for (int i = 0; i < type.getTypeArguments().size(); ++i) {
if (!types.isSameType(oldArgs.get(i).actualType, newArgs.get(i).actualType)) {
// One of the underlying types doesn't match. Give up.
return;
}
}
type.setTypeArguments(newArgs);
/* It would be nice to call isSubtype for a basic sanity check.
* However, the type might not have been completely initialized yet,
* so isSubtype might fail.
*
if (!typeHierarchy.isSubtype(type, ctxtype)) {
// Simply taking the newArgs didn't result in a valid subtype.
// Give up and simply use the inferred types.
type.setTypeArguments(oldArgs);
}
*/
} else {
// TODO: Find a way to determine annotated type arguments.
// Look at what Attr and Resolve are doing and rework this whole method.
}
break;
case TYPEVAR:
// TODO: this should NOT be necessary.
// org.checkerframework.dataflow.cfg.node.MethodAccessNode.MethodAccessNode(ExpressionTree, Node)
// Uses an ExecutableElement, which did not substitute type variables.
break;
default:
ErrorReporter.errorAbort("AnnotatedTypeFactory.fromNewClassContextHelper: unexpected context: " +
ctxtype + " (" + ctxtype.getKind() + ")");
}
}
示例7: parseType
import org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType; //导入方法依赖的package包/类
private void parseType(ClassOrInterfaceDeclaration decl, TypeElement elt, Map<Element, AnnotatedTypeMirror> atypes, Map<String, Set<AnnotationMirror>> declAnnos) {
annotateDecl(declAnnos, elt, decl.getAnnotations());
AnnotatedDeclaredType type = atypeFactory.fromElement(elt);
annotate(type, decl.getAnnotations());
{
List<? extends AnnotatedTypeMirror> typeArguments = type.getTypeArguments();
List<TypeParameter> typeParameters = decl.getTypeParameters();
/// It can be the case that args=[] and params=null.
// if ((typeParameters == null) != (typeArguments == null)) {
// throw new Error(String.format("parseType (%s, %s): inconsistent nullness for args and params%n args = %s%n params = %s%n", decl, elt, typeArguments, typeParameters));
// }
if ((typeParameters == null) && (typeArguments.size() != 0)) {
// TODO: Class EventListenerProxy in Java 6 does not have type parameters, but in Java 7 does.
// To handle both with one specification, we currently ignore the problem.
// Investigate what a cleaner solution is, e.g. having a separate Java 7 specification that overrides
// the Java 6 specification.
// System.out.printf("Dying. theCompilationUnit=%s%n", theCompilationUnit);
if (debugStubParser) {
stubDebug(String.format("parseType: mismatched sizes for params and args%n decl=%s%n typeParameters=%s%n elt=%s (%s)%n type=%s (%s)%n typeArguments (size %d)=%s%n theCompilationUnit=%s%nEnd of Message%n",
decl, typeParameters,
elt, elt.getClass(), type, type.getClass(), typeArguments.size(), typeArguments,
theCompilationUnit));
}
/*
throw new Error(String.format("parseType: mismatched sizes for params and args%n decl=%s%n typeParameters=%s%n elt=%s (%s)%n type=%s (%s)%n typeArguments (size %d)=%s%n",
decl, typeParameters,
elt, elt.getClass(), type, type.getClass(), typeArguments.size(), typeArguments));
*/
}
if ((typeParameters != null) && (typeParameters.size() != typeArguments.size())) {
// TODO: decide how severe this problem really is; see comment above.
// System.out.printf("Dying. theCompilationUnit=%s%n", theCompilationUnit);
if (debugStubParser) {
stubDebug(String.format("parseType: mismatched sizes for params and args%n decl=%s%n typeParameters (size %d)=%s%n elt=%s (%s)%n type=%s (%s)%n typeArguments (size %d)=%s%n theCompilationUnit=%s%nEnd of Message%n",
decl, typeParameters.size(), typeParameters,
elt, elt.getClass(), type, type.getClass(), typeArguments.size(), typeArguments,
theCompilationUnit));
}
/*
throw new Error(String.format("parseType: mismatched sizes for params and args%n decl=%s%n typeParameters (size %d)=%s%n elt=%s (%s)%n type=%s (%s)%n typeArguments (size %d)=%s%n",
decl, typeParameters.size(), typeParameters,
elt, elt.getClass(), type, type.getClass(), typeArguments.size(), typeArguments));
*/
}
}
annotateParameters(type.getTypeArguments(), decl.getTypeParameters());
annotateSupertypes(decl, type);
putNew(atypes, elt, type);
}