use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class PropagationTypeAnnotator method applyAnnosFromBound.
/**
* Take the primary annotations from typeParamBound and place them as primary annotations on
* wildcard bound.
*/
private void applyAnnosFromBound(final AnnotatedTypeMirror wildcardBound, final AnnotatedTypeMirror typeParamBound, final Set<? extends AnnotationMirror> tops) {
// bounds via its declaration or defaulting rules
if (wildcardBound.getKind() == TypeKind.TYPEVAR || typeParamBound.getKind() == TypeKind.TYPEVAR) {
return;
}
for (final AnnotationMirror top : tops) {
if (wildcardBound.getAnnotationInHierarchy(top) == null) {
final AnnotationMirror typeParamAnno = typeParamBound.getAnnotationInHierarchy(top);
if (typeParamAnno == null) {
throw new BugInCF(StringsPlume.joinLines("Missing annotation on type parameter", "top=" + top, "wildcardBound=" + wildcardBound, "typeParamBound=" + typeParamBound));
}
// else
wildcardBound.addAnnotation(typeParamAnno);
}
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class TypeFromTypeTreeVisitor method getTypeVariableFromDeclaration.
/**
* If a tree is can be found for the declaration of the type variable {@code type}, then a {@link
* AnnotatedTypeVariable} is returned with explicit annotations from the type variables declared
* bounds. If a tree cannot be found, then {@code type}, converted to a use, is returned.
*
* @param type type variable used to find declaration tree
* @param f annotated type factory
* @return the AnnotatedTypeVariable from the declaration of {@code type} or {@code type} if no
* tree is found.
*/
private AnnotatedTypeVariable getTypeVariableFromDeclaration(AnnotatedTypeVariable type, AnnotatedTypeFactory f) {
TypeVariable typeVar = type.getUnderlyingType();
TypeParameterElement tpe = (TypeParameterElement) typeVar.asElement();
Element elt = tpe.getGenericElement();
if (elt instanceof TypeElement) {
TypeElement typeElt = (TypeElement) elt;
int idx = typeElt.getTypeParameters().indexOf(tpe);
ClassTree cls = (ClassTree) f.declarationFromElement(typeElt);
if (cls == null || cls.getTypeParameters().isEmpty()) {
// contains all necessary information and we can return that.
return type.asUse();
}
// will return a declaration ATV. So change it to a use.
return visitTypeParameter(cls.getTypeParameters().get(idx), f).asUse();
} else if (elt instanceof ExecutableElement) {
ExecutableElement exElt = (ExecutableElement) elt;
int idx = exElt.getTypeParameters().indexOf(tpe);
MethodTree meth = (MethodTree) f.declarationFromElement(exElt);
if (meth == null) {
// + elt);
return type.asUse();
}
// This works the same as the case above. Even though `meth` itself is not a
// type declaration tree, the elements of `meth.getTypeParameters()` still are.
AnnotatedTypeVariable result = visitTypeParameter(meth.getTypeParameters().get(idx), f).shallowCopy();
result.setDeclaration(false);
return result;
} else if (TypesUtils.isCapturedTypeVariable(typeVar)) {
// not an element at all, namely Symtab.noSymbol.
return type.asUse();
} else {
throw new BugInCF("TypeFromTree.forTypeVariable: not a supported element: " + elt);
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class CheckerMain method findPathTo.
/**
* Find the jar file or directory containing the .class file from which cls was loaded.
*
* @param cls the class whose .class file we wish to locate; if null, CheckerMain.class
* @param errIfFromDirectory if false, throw an exception if the file was loaded from a directory
*/
public static String findPathTo(Class<?> cls, boolean errIfFromDirectory) throws IllegalStateException {
if (cls == null) {
cls = CheckerMain.class;
}
String name = cls.getName();
String classFileName;
/* name is something like pakkage.name.ContainingClass$ClassName. We need to turn this into ContainingClass$ClassName.class. */
{
int idx = name.lastIndexOf('.');
classFileName = (idx == -1 ? name : name.substring(idx + 1)) + ".class";
}
String uri = cls.getResource(classFileName).toString();
if (uri.startsWith("file:")) {
if (errIfFromDirectory) {
return uri;
} else {
throw new IllegalStateException("This class has been loaded from a directory and not from a jar file.");
}
}
if (!uri.startsWith("jar:file:")) {
int idx = uri.indexOf(':');
String protocol = idx == -1 ? "(unknown)" : uri.substring(0, idx);
throw new IllegalStateException("This class has been loaded remotely via the " + protocol + " protocol. Only loading from a jar on the local file system is supported.");
}
int idx = uri.indexOf('!');
// Sanity check
if (idx == -1) {
throw new IllegalStateException("You appear to have loaded this class from a local jar file, but URI has no \"!\": " + uri);
}
try {
String fileName = URLDecoder.decode(uri.substring("jar:file:".length(), idx), Charset.defaultCharset().name());
return new File(fileName).getAbsolutePath();
} catch (UnsupportedEncodingException e) {
throw new BugInCF("Default charset doesn't exist. Your VM is borked.");
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class JavaExpression method fromNode.
/**
* We ignore operations such as widening and narrowing when computing the internal representation.
*
* @param receiverNode a node to convert to a JavaExpression
* @return the internal representation of the given node. Might contain {@link Unknown}.
*/
public static JavaExpression fromNode(Node receiverNode) {
JavaExpression result = null;
if (receiverNode instanceof FieldAccessNode) {
result = fromNodeFieldAccess((FieldAccessNode) receiverNode);
} else if (receiverNode instanceof ExplicitThisNode) {
result = new ThisReference(receiverNode.getType());
} else if (receiverNode instanceof ThisNode) {
result = new ThisReference(receiverNode.getType());
} else if (receiverNode instanceof SuperNode) {
result = new ThisReference(receiverNode.getType());
} else if (receiverNode instanceof LocalVariableNode) {
LocalVariableNode lv = (LocalVariableNode) receiverNode;
result = new LocalVariable(lv);
} else if (receiverNode instanceof ArrayAccessNode) {
ArrayAccessNode a = (ArrayAccessNode) receiverNode;
result = fromArrayAccess(a);
} else if (receiverNode instanceof StringConversionNode) {
// ignore string conversion
return fromNode(((StringConversionNode) receiverNode).getOperand());
} else if (receiverNode instanceof WideningConversionNode) {
// ignore widening
return fromNode(((WideningConversionNode) receiverNode).getOperand());
} else if (receiverNode instanceof NarrowingConversionNode) {
// ignore narrowing
return fromNode(((NarrowingConversionNode) receiverNode).getOperand());
} else if (receiverNode instanceof UnaryOperationNode) {
UnaryOperationNode uopn = (UnaryOperationNode) receiverNode;
return new UnaryOperation(uopn, fromNode(uopn.getOperand()));
} else if (receiverNode instanceof BinaryOperationNode) {
BinaryOperationNode bopn = (BinaryOperationNode) receiverNode;
return new BinaryOperation(bopn, fromNode(bopn.getLeftOperand()), fromNode(bopn.getRightOperand()));
} else if (receiverNode instanceof ClassNameNode) {
ClassNameNode cn = (ClassNameNode) receiverNode;
result = new ClassName(cn.getType());
} else if (receiverNode instanceof ValueLiteralNode) {
ValueLiteralNode vn = (ValueLiteralNode) receiverNode;
result = new ValueLiteral(vn.getType(), vn);
} else if (receiverNode instanceof ArrayCreationNode) {
ArrayCreationNode an = (ArrayCreationNode) receiverNode;
List<@Nullable JavaExpression> dimensions = CollectionsPlume.mapList(JavaExpression::fromNode, an.getDimensions());
List<JavaExpression> initializers = CollectionsPlume.mapList(JavaExpression::fromNode, an.getInitializers());
result = new ArrayCreation(an.getType(), dimensions, initializers);
} else if (receiverNode instanceof MethodInvocationNode) {
MethodInvocationNode mn = (MethodInvocationNode) receiverNode;
MethodInvocationTree t = mn.getTree();
if (t == null) {
throw new BugInCF("Unexpected null tree for node: " + mn);
}
assert TreeUtils.isUseOfElement(t) : "@AssumeAssertion(nullness): tree kind";
ExecutableElement invokedMethod = TreeUtils.elementFromUse(t);
// Note that the method might be nondeterministic.
List<JavaExpression> parameters = CollectionsPlume.mapList(JavaExpression::fromNode, mn.getArguments());
JavaExpression methodReceiver;
if (ElementUtils.isStatic(invokedMethod)) {
methodReceiver = new ClassName(mn.getTarget().getReceiver().getType());
} else {
methodReceiver = fromNode(mn.getTarget().getReceiver());
}
result = new MethodCall(mn.getType(), invokedMethod, methodReceiver, parameters);
}
if (result == null) {
result = new Unknown(receiverNode);
}
return result;
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class JavaExpression method getImplicitReceiver.
/**
* Returns the implicit receiver of ele.
*
* <p>Returns either a new ClassName or a new ThisReference depending on whether ele is static or
* not. The passed element must be a field, method, or class.
*
* @param ele a field, method, or class
* @return either a new ClassName or a new ThisReference depending on whether ele is static or not
*/
public static JavaExpression getImplicitReceiver(Element ele) {
TypeElement enclosingTypeElement = ElementUtils.enclosingTypeElement(ele);
if (enclosingTypeElement == null) {
throw new BugInCF("getImplicitReceiver's arg has no enclosing type: " + ele);
}
TypeMirror enclosingType = enclosingTypeElement.asType();
if (ElementUtils.isStatic(ele)) {
return new ClassName(enclosingType);
} else {
return new ThisReference(enclosingType);
}
}
Aggregations