use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class DependentTypesHelper method atVariableDeclaration.
/**
* Standardize the Java expressions in annotations in a variable declaration. Converts the
* parameter syntax, e.g "#1", to the parameter name.
*
* @param type the type of the variable declaration; is side-effected by this method
* @param declarationTree the variable declaration
* @param variableElt the element of the variable declaration
*/
public void atVariableDeclaration(AnnotatedTypeMirror type, Tree declarationTree, VariableElement variableElt) {
if (!hasDependentType(type)) {
return;
}
TreePath pathToVariableDecl = factory.getPath(declarationTree);
if (pathToVariableDecl == null) {
// If this is a synthetic created by dataflow, the path will be null.
return;
}
ElementKind variableKind = variableElt.getKind();
if (ElementUtils.isBindingVariable(variableElt)) {
// Treat binding variables the same as local variables.
variableKind = ElementKind.LOCAL_VARIABLE;
}
switch(variableKind) {
case PARAMETER:
TreePath pathTillEnclTree = TreePathUtil.pathTillOfKind(pathToVariableDecl, METHOD_OR_LAMBDA);
if (pathTillEnclTree == null) {
throw new BugInCF("no enclosing method or lambda found for " + variableElt);
}
Tree enclTree = pathTillEnclTree.getLeaf();
if (enclTree.getKind() == Tree.Kind.METHOD) {
MethodTree methodDeclTree = (MethodTree) enclTree;
StringToJavaExpression stringToJavaExpr = stringExpr -> StringToJavaExpression.atMethodBody(stringExpr, methodDeclTree, factory.getChecker());
if (debugStringToJavaExpression) {
System.out.printf("atVariableDeclaration(%s, %s, %s) 1 created %s%n", type, TreeUtils.toStringTruncated(declarationTree, 65), variableElt, stringToJavaExpr);
}
convertAnnotatedTypeMirror(stringToJavaExpr, type);
} else {
// Lambdas can use local variables defined in the enclosing method, so allow
// identifiers to be locals in scope at the location of the lambda.
StringToJavaExpression stringToJavaExpr = stringExpr -> StringToJavaExpression.atLambdaParameter(stringExpr, (LambdaExpressionTree) enclTree, pathToVariableDecl.getParentPath(), factory.getChecker());
if (debugStringToJavaExpression) {
System.out.printf("atVariableDeclaration(%s, %s, %s) 2 created %s%n", type, TreeUtils.toStringTruncated(declarationTree, 65), variableElt, stringToJavaExpr);
}
convertAnnotatedTypeMirror(stringToJavaExpr, type);
}
break;
case LOCAL_VARIABLE:
case RESOURCE_VARIABLE:
case EXCEPTION_PARAMETER:
StringToJavaExpression stringToJavaExprVar = stringExpr -> StringToJavaExpression.atPath(stringExpr, pathToVariableDecl, factory.getChecker());
if (debugStringToJavaExpression) {
System.out.printf("atVariableDeclaration(%s, %s, %s) 3 created %s%n", type, TreeUtils.toStringTruncated(declarationTree, 65), variableElt, stringToJavaExprVar);
}
convertAnnotatedTypeMirror(stringToJavaExprVar, type);
break;
case FIELD:
case ENUM_CONSTANT:
StringToJavaExpression stringToJavaExprField = stringExpr -> StringToJavaExpression.atFieldDecl(stringExpr, variableElt, factory.getChecker());
if (debugStringToJavaExpression) {
System.out.printf("atVariableDeclaration(%s, %s, %s) 4 created %s%n", type, TreeUtils.toStringTruncated(declarationTree, 65), variableElt, stringToJavaExprField);
}
convertAnnotatedTypeMirror(stringToJavaExprField, type);
break;
default:
throw new BugInCF("unexpected element kind " + variableElt.getKind() + " for " + variableElt);
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class TargetedElementAnnotationApplier method handleInvalid.
/**
* This implementation reports all invalid annotations as errors.
*
* @param invalid the list of annotations that were returned by getRawTypeAttributes and were not
* handled by handleTargeted or handleValid
*/
protected void handleInvalid(List<Attribute.TypeCompound> invalid) {
List<Attribute.TypeCompound> remaining = new ArrayList<>(invalid.size());
for (Attribute.TypeCompound tc : invalid) {
if (tc.getAnnotationType().getKind() != TypeKind.ERROR) {
// Filter out annotations that have an error type. javac will
// already have raised an error for them.
remaining.add(tc);
}
}
if (!remaining.isEmpty()) {
StringJoiner msg = new StringJoiner(System.lineSeparator());
msg.add("handleInvalid(this=" + this.getClass().getName() + "):");
msg.add("Invalid variable and element passed to extractAndApply; type: " + type);
String elementInfoPrefix = " element: " + element + " (kind: " + element.getKind() + "), invalid annotations: ";
StringJoiner remainingInfo = new StringJoiner(", ", elementInfoPrefix, "");
for (Attribute.TypeCompound r : remaining) {
remainingInfo.add(r.toString() + " (" + r.position + ")");
}
msg.add(remainingInfo.toString());
msg.add("Targeted annotations: " + StringsPlume.join(", ", annotatedTargets()));
msg.add("Valid annotations: " + StringsPlume.join(", ", validTargets()));
throw new BugInCF(msg.toString());
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class TypeParamElementAnnotationApplier method applyComponentAnnotation.
private void applyComponentAnnotation(final TypeCompound anno) throws UnexpectedAnnotationLocationException {
final AnnotatedTypeMirror upperBoundType = typeParam.getUpperBound();
Map<AnnotatedTypeMirror, List<TypeCompound>> typeToAnnotations = new HashMap<>();
if (anno.position.type == upperBoundTarget()) {
if (upperBoundType.getKind() == TypeKind.INTERSECTION) {
final List<AnnotatedTypeMirror> bounds = ((AnnotatedIntersectionType) upperBoundType).getBounds();
final int boundIndex = anno.position.bound_index + ElementAnnotationUtil.getBoundIndexOffset(bounds);
if (boundIndex < 0 || boundIndex > bounds.size()) {
throw new BugInCF("Invalid bound index on element annotation ( " + anno + " ) " + "for type ( " + typeParam + " ) with upper bound ( " + typeParam.getUpperBound() + " )");
}
addAnnotationToMap(bounds.get(boundIndex), anno, typeToAnnotations);
} else {
addAnnotationToMap(upperBoundType, anno, typeToAnnotations);
}
} else {
addAnnotationToMap(typeParam.getLowerBound(), anno, typeToAnnotations);
}
for (Map.Entry<AnnotatedTypeMirror, List<TypeCompound>> typeToAnno : typeToAnnotations.entrySet()) {
ElementAnnotationUtil.annotateViaTypeAnnoPosition(typeToAnno.getKey(), typeToAnno.getValue());
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class EqualitiesSolver method updateTargetsWithPartiallyInferredType.
// If we determined that this target T1 is equal to a type ATM in hierarchies @A,@B,@C
// for each of those hierarchies, if a target is equal to T1 in that hierarchy it is also equal
// to ATM.
// e.g.
// if : T1 == @A @B @C ATM in only the A,B hierarchies
// and T1 == T2 only in @A hierarchy
//
// then T2 == @A @B @C only in the @A hierarchy
//
public boolean updateTargetsWithPartiallyInferredType(final Equalities equalities, ConstraintMap constraintMap, AnnotatedTypeFactory typeFactory) {
boolean updated = false;
if (!equalities.types.isEmpty()) {
if (equalities.types.size() != 1) {
throw new BugInCF("Equalities should have at most 1 constraint.");
}
Map.Entry<AnnotatedTypeMirror, AnnotationMirrorSet> remainingTypeEquality;
remainingTypeEquality = equalities.types.entrySet().iterator().next();
final AnnotatedTypeMirror remainingType = remainingTypeEquality.getKey();
final AnnotationMirrorSet remainingHierarchies = remainingTypeEquality.getValue();
// update targets
for (Map.Entry<TypeVariable, AnnotationMirrorSet> targetToHierarchies : equalities.targets.entrySet()) {
final TypeVariable equalTarget = targetToHierarchies.getKey();
final AnnotationMirrorSet hierarchies = targetToHierarchies.getValue();
final AnnotationMirrorSet equalTypeHierarchies = new AnnotationMirrorSet(remainingHierarchies);
equalTypeHierarchies.retainAll(hierarchies);
final Map<AnnotatedTypeMirror, AnnotationMirrorSet> otherTargetsEqualTypes = constraintMap.getConstraints(equalTarget).equalities.types;
AnnotationMirrorSet equalHierarchies = otherTargetsEqualTypes.get(remainingType);
if (equalHierarchies == null) {
equalHierarchies = new AnnotationMirrorSet(equalTypeHierarchies);
otherTargetsEqualTypes.put(remainingType, equalHierarchies);
updated = true;
} else {
final int size = equalHierarchies.size();
equalHierarchies.addAll(equalTypeHierarchies);
updated = size == equalHierarchies.size();
}
}
}
return updated;
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class SupertypesSolver method leastUpperBound.
/**
* Successively calls least upper bound on the elements of types. Unlike
* AnnotatedTypes.leastUpperBound, this method will box primitives if necessary
*/
public static AnnotatedTypeMirror leastUpperBound(final TypeVariable target, final AnnotatedTypeFactory typeFactory, final Map<AnnotatedTypeMirror, AnnotationMirrorSet> types) {
QualifierHierarchy qualifierHierarchy = typeFactory.getQualifierHierarchy();
AnnotatedTypeVariable targetsDeclaredType = (AnnotatedTypeVariable) typeFactory.getAnnotatedType(target.asElement());
final AnnotationMirrorMap<AnnotationMirror> lowerBoundAnnos = TypeArgInferenceUtil.createHierarchyMap(new AnnotationMirrorSet(targetsDeclaredType.getLowerBound().getEffectiveAnnotations()), qualifierHierarchy);
final Iterator<Map.Entry<AnnotatedTypeMirror, AnnotationMirrorSet>> typesIter = types.entrySet().iterator();
if (!typesIter.hasNext()) {
throw new BugInCF("Calling LUB on empty list.");
}
/**
* If a constraint implies that a type parameter Ti is a supertype of an annotated type mirror
* Ai but only in a subset of all qualifier hierarchies then for all other qualifier hierarchies
* replace the primary annotation on Ai with the lowest possible annotation (ensuring that it
* won't be the LUB unless there are no other constraints, or all other constraints imply the
* bottom annotation is the LUB). Note: Even if we choose bottom as the lub here, the assignment
* context may raise this annotation.
*/
final Map.Entry<AnnotatedTypeMirror, AnnotationMirrorSet> head = typesIter.next();
AnnotatedTypeMirror lubType = groundMissingHierarchies(head, lowerBoundAnnos);
AnnotatedTypeMirror nextType = null;
while (typesIter.hasNext()) {
nextType = groundMissingHierarchies(typesIter.next(), lowerBoundAnnos);
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;
}
Aggregations