use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AnnotatedTypeCopier method visitUnion.
@Override
public AnnotatedTypeMirror visitUnion(AnnotatedUnionType original, IdentityHashMap<AnnotatedTypeMirror, AnnotatedTypeMirror> originalToCopy) {
if (originalToCopy.containsKey(original)) {
return originalToCopy.get(original);
}
final AnnotatedUnionType copy = (AnnotatedUnionType) AnnotatedTypeMirror.createType(original.getUnderlyingType(), original.atypeFactory, original.isDeclaration());
maybeCopyPrimaryAnnotations(original, copy);
originalToCopy.put(original, copy);
if (original.alternatives != null) {
final List<AnnotatedDeclaredType> copyAlternatives = new ArrayList<>();
for (final AnnotatedDeclaredType supertype : original.alternatives) {
copyAlternatives.add((AnnotatedDeclaredType) visit(supertype, originalToCopy));
}
copy.alternatives = Collections.unmodifiableList(copyAlternatives);
}
return copy;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AnnotatedTypeFactory method fromNewClassContextHelper.
// This method extracts the ugly hacky parts.
// This method should be rewritten and in particular diamonds should be
// implemented cleanly.
// See Issue 289.
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 ARRAY:
// so nothing to be done.
break;
case TYPEVAR:
// Uses an ExecutableElement, which did not substitute type variables.
break;
case WILDCARD:
// TODO: look at bounds of wildcard and see whether we can improve.
break;
default:
if (ctxtype.getKind().isPrimitive()) {
// See Issue 438. Ignore primitive types for diamond inference - a primitive
// type
// is never a suitable context anyways.
} else {
ErrorReporter.errorAbort("AnnotatedTypeFactory.fromNewClassContextHelper: unexpected context: " + ctxtype + " (" + ctxtype.getKind() + ")");
}
}
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AnnotatedTypeFactory method getSelfType.
/**
* Returns the type of {@code this} in the current location, which can be used if {@code this}
* has a special semantics (e.g. {@code this} is non-null).
*
* <p>The parameter is an arbitrary tree and does not have to mention "this", neither explicitly
* nor implicitly. This method should be overridden for type-system specific behavior.
*
* <p>TODO: in 1.8.2, handle all receiver type annotations. TODO: handle enclosing classes
* correctly.
*/
public AnnotatedDeclaredType getSelfType(Tree tree) {
TreePath path = getPath(tree);
ClassTree enclosingClass = TreeUtils.enclosingClass(path);
if (enclosingClass == null) {
// I hope this only happens when tree is a fake tree that
// we created, e.g. when desugaring enhanced-for-loops.
enclosingClass = getCurrentClassTree(tree);
}
AnnotatedDeclaredType type = getAnnotatedType(enclosingClass);
MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
if (enclosingClass.getSimpleName().length() != 0 && enclosingMethod != null) {
AnnotatedDeclaredType methodReceiver;
if (TreeUtils.isConstructor(enclosingMethod)) {
methodReceiver = (AnnotatedDeclaredType) getAnnotatedType(enclosingMethod).getReturnType();
} else {
methodReceiver = getAnnotatedType(enclosingMethod).getReceiverType();
}
if (shouldTakeFromReceiver(methodReceiver)) {
// TODO what about all annotations on the receiver?
// Code is also duplicated above.
type.clearAnnotations();
type.addAnnotations(methodReceiver.getAnnotations());
}
}
return type;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AnnotatedTypeFactory method getCurrentMethodReceiver.
/**
* Returns the receiver type of the current method being visited, and returns null if the
* visited tree is not within a method or if that method has no receiver (e.g. a static method).
*
* <p>The method uses the parameter only if the most enclosing method cannot be found directly.
*
* @return receiver type of the most enclosing method being visited
*/
@Nullable
protected final AnnotatedDeclaredType getCurrentMethodReceiver(Tree tree) {
AnnotatedDeclaredType res = visitorState.getMethodReceiver();
if (res == null) {
TreePath path = getPath(tree);
if (path != null) {
MethodTree enclosingMethod = TreeUtils.enclosingMethod(path);
ClassTree enclosingClass = TreeUtils.enclosingClass(path);
boolean found = false;
for (Tree member : enclosingClass.getMembers()) {
if (member.getKind() == Tree.Kind.METHOD) {
if (member == enclosingMethod) {
found = true;
}
}
}
if (found && enclosingMethod != null) {
AnnotatedExecutableType method = getAnnotatedType(enclosingMethod);
res = method.getReceiverType();
// TODO: three tests fail if one adds the following, which would make
// sense, or not?
// visitorState.setMethodReceiver(res);
} else {
// We are within an anonymous class or field initializer
res = this.getAnnotatedType(enclosingClass);
}
}
}
return res;
}
use of org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType in project checker-framework by typetools.
the class AnnotatedTypeFactory method getFnInterfaceFromTree.
/**
* Returns the functional interface and the function type that this lambda or member references
* targets.
*
* <p>The function type is the type of the single method declared in the functional interface
* adapted as if it were invoked using the functional interface as the receiver expression.
*
* <p>The target type of a lambda or a method reference is the type to which it is assigned or
* casted.
*
* @param tree lambda expression tree or member reference tree
* @return the functional interface and the function type that this method reference or lambda
* targets.
*/
private Pair<AnnotatedDeclaredType, AnnotatedExecutableType> getFnInterfaceFromTree(Tree tree) {
// Functional interface
AnnotatedDeclaredType functionalInterfaceType = getFunctionalInterfaceType(tree);
makeGroundTargetType(functionalInterfaceType, (DeclaredType) TreeUtils.typeOf(tree));
// Functional method
Element fnElement = TreeUtils.findFunction(tree, processingEnv);
// Function type
AnnotatedExecutableType functionType = (AnnotatedExecutableType) AnnotatedTypes.asMemberOf(types, this, functionalInterfaceType, fnElement);
return Pair.of(functionalInterfaceType, functionType);
}
Aggregations