use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class PerDirectorySuite method getParametersMethod.
/**
* Returns method annotated @Parameters, typically the getTestDirs or getTestFiles method.
*/
private FrameworkMethod getParametersMethod(TestClass testClass) {
final List<FrameworkMethod> parameterMethods = testClass.getAnnotatedMethods(Parameters.class);
if (parameterMethods.size() != 1) {
// Construct error message
String methods;
if (parameterMethods.isEmpty()) {
methods = "[No methods specified]";
} else {
StringJoiner sj = new StringJoiner(", ");
for (FrameworkMethod method : parameterMethods) {
sj.add(method.getName());
}
methods = sj.toString();
}
throw new BugInCF("Exactly one of the following methods should be declared:%n%s%n" + "testClass=%s%n" + "parameterMethods=%s", requiredFormsMessage, testClass.getName(), methods);
}
FrameworkMethod method = parameterMethods.get(0);
Class<?> returnType = method.getReturnType();
String methodName = method.getName();
switch(methodName) {
case "getTestDirs":
if (!(returnType.isArray() && returnType.getComponentType() == String.class)) {
throw new RuntimeException("getTestDirs should return String[], found " + returnType);
}
break;
default:
throw new RuntimeException(requiredFormsMessage + "%n" + "testClass=" + testClass.getName() + "%n" + "parameterMethods=" + method);
}
int modifiers = method.getMethod().getModifiers();
if (!Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers)) {
throw new RuntimeException("Parameter method (" + method.getName() + ") must be public and static");
}
return method;
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class TypeArgInferenceUtil method leastUpperBound.
/**
* Successively calls least upper bound on the elements of types. Unlike leastUpperBound, this
* method will box primitives if necessary
*/
public static AnnotatedTypeMirror leastUpperBound(final AnnotatedTypeFactory typeFactory, final Iterable<AnnotatedTypeMirror> types) {
final Iterator<AnnotatedTypeMirror> typesIter = types.iterator();
if (!typesIter.hasNext()) {
throw new BugInCF("Calling LUB on empty list");
}
AnnotatedTypeMirror lubType = typesIter.next();
AnnotatedTypeMirror nextType = null;
while (typesIter.hasNext()) {
nextType = typesIter.next();
if (lubType.getKind().isPrimitive()) {
if (!nextType.getKind().isPrimitive()) {
lubType = typeFactory.getBoxedType((AnnotatedPrimitiveType) lubType);
}
} else if (nextType.getKind().isPrimitive()) {
if (!lubType.getKind().isPrimitive()) {
nextType = typeFactory.getBoxedType((AnnotatedPrimitiveType) nextType);
}
}
lubType = AnnotatedTypes.leastUpperBound(typeFactory, lubType, nextType);
}
return lubType;
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class TypeArgInferenceUtil method assignedTo.
/**
* Returns the annotated type that the leaf of path is assigned to, if it is within an assignment
* context. Returns the annotated type that the method invocation at the leaf is assigned to. If
* the result is a primitive, return the boxed version.
*
* @param atypeFactory the type factory, for looking up types
* @param path the path whole leaf to look up a type for
* @return the type of path's leaf
*/
// AST node comparisons
@SuppressWarnings("interning:not.interned")
public static AnnotatedTypeMirror assignedTo(AnnotatedTypeFactory atypeFactory, TreePath path) {
Tree assignmentContext = TreePathUtil.getAssignmentContext(path);
AnnotatedTypeMirror res;
if (assignmentContext == null) {
res = null;
} else if (assignmentContext instanceof AssignmentTree) {
ExpressionTree variable = ((AssignmentTree) assignmentContext).getVariable();
res = atypeFactory.getAnnotatedType(variable);
} else if (assignmentContext instanceof CompoundAssignmentTree) {
ExpressionTree variable = ((CompoundAssignmentTree) assignmentContext).getVariable();
res = atypeFactory.getAnnotatedType(variable);
} else if (assignmentContext instanceof MethodInvocationTree) {
MethodInvocationTree methodInvocation = (MethodInvocationTree) assignmentContext;
// TODO move to getAssignmentContext
if (methodInvocation.getMethodSelect() instanceof MemberSelectTree && ((MemberSelectTree) methodInvocation.getMethodSelect()).getExpression() == path.getLeaf()) {
return null;
}
ExecutableElement methodElt = TreeUtils.elementFromUse(methodInvocation);
AnnotatedTypeMirror receiver = atypeFactory.getReceiverType(methodInvocation);
if (TreeUtils.isSuperConstructorCall(methodInvocation)) {
receiver = atypeFactory.getSelfType(methodInvocation);
}
res = assignedToExecutable(atypeFactory, path, methodElt, receiver, methodInvocation.getArguments());
} else if (assignmentContext instanceof NewArrayTree) {
// TODO: I left the previous implementation below, it definitely caused infinite loops
// TODO: if you called it from places like the TreeAnnotator.
res = null;
// TODO: This may cause infinite loop
// AnnotatedTypeMirror type =
// atypeFactory.getAnnotatedType((NewArrayTree)assignmentContext);
// type = AnnotatedTypes.innerMostType(type);
// return type;
} else if (assignmentContext instanceof NewClassTree) {
// This need to be basically like MethodTree
NewClassTree newClassTree = (NewClassTree) assignmentContext;
if (newClassTree.getEnclosingExpression() instanceof NewClassTree && (newClassTree.getEnclosingExpression() == path.getLeaf())) {
return null;
}
ExecutableElement constructorElt = TreeUtils.constructor(newClassTree);
AnnotatedTypeMirror receiver = atypeFactory.fromNewClass(newClassTree);
res = assignedToExecutable(atypeFactory, path, constructorElt, receiver, newClassTree.getArguments());
} else if (assignmentContext instanceof ReturnTree) {
HashSet<Tree.Kind> kinds = new HashSet<>(Arrays.asList(Tree.Kind.LAMBDA_EXPRESSION, Tree.Kind.METHOD));
Tree enclosing = TreePathUtil.enclosingOfKind(path, kinds);
if (enclosing.getKind() == Tree.Kind.METHOD) {
res = atypeFactory.getAnnotatedType((MethodTree) enclosing).getReturnType();
} else {
AnnotatedExecutableType fninf = atypeFactory.getFunctionTypeFromTree((LambdaExpressionTree) enclosing);
res = fninf.getReturnType();
}
} else if (assignmentContext instanceof VariableTree) {
res = assignedToVariable(atypeFactory, assignmentContext);
} else {
throw new BugInCF("AnnotatedTypes.assignedTo: shouldn't be here");
}
if (res != null && TypesUtils.isPrimitive(res.getUnderlyingType())) {
return atypeFactory.getBoxedType((AnnotatedPrimitiveType) res);
} else {
return res;
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class QualifierDefaults method isElementAnnotatedForThisChecker.
private boolean isElementAnnotatedForThisChecker(final Element elt) {
boolean elementAnnotatedForThisChecker = false;
if (elt == null) {
throw new BugInCF("Call of QualifierDefaults.isElementAnnotatedForThisChecker with null");
}
if (elementAnnotatedFors.containsKey(elt)) {
return elementAnnotatedFors.get(elt);
}
final AnnotationMirror annotatedFor = atypeFactory.getDeclAnnotation(elt, AnnotatedFor.class);
if (annotatedFor != null) {
elementAnnotatedForThisChecker = atypeFactory.doesAnnotatedForApplyToThisChecker(annotatedFor);
}
if (!elementAnnotatedForThisChecker) {
Element parent;
if (elt.getKind() == ElementKind.PACKAGE) {
// elt.getEnclosingElement() on a package is null; therefore,
// use the dedicated method.
parent = ElementUtils.parentPackage((PackageElement) elt, elements);
} else {
parent = elt.getEnclosingElement();
}
if (parent != null && isElementAnnotatedForThisChecker(parent)) {
elementAnnotatedForThisChecker = true;
}
}
elementAnnotatedFors.put(elt, elementAnnotatedForThisChecker);
return elementAnnotatedForThisChecker;
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class DependentTypesHelper method atLocalVariable.
/**
* Standardize the Java expressions in annotations in a type. Converts the parameter syntax, e.g.
* "#2", to the parameter name.
*
* @param type the type to standardize; is side-effected by this method
* @param elt the element whose type is {@code type}
*/
public void atLocalVariable(AnnotatedTypeMirror type, Element elt) {
if (!hasDependentType(type)) {
return;
}
switch(elt.getKind()) {
case PARAMETER:
case LOCAL_VARIABLE:
case RESOURCE_VARIABLE:
case EXCEPTION_PARAMETER:
Tree declarationTree = factory.declarationFromElement(elt);
if (declarationTree == null) {
if (elt.getKind() == ElementKind.PARAMETER) {
// assignment context for a pseudo assignment of an argument to a method parameter.
return;
}
throw new BugInCF(this.getClass() + ": tree not found");
} else if (TreeUtils.typeOf(declarationTree) == null) {
// assignment context for a pseudo assignment of an argument to a method parameter.
return;
}
atVariableDeclaration(type, declarationTree, (VariableElement) elt);
return;
default:
// for example), so there is nothing to do.
break;
}
}
Aggregations