use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class StubParser method getAnnotation.
/**
* Convert {@code annotation} into an AnnotationMirror. Returns null if the annotation isn't
* supported by the checker or if some error occurred while converting it.
*/
private AnnotationMirror getAnnotation(AnnotationExpr annotation, Map<String, AnnotationMirror> allStubAnnotations) {
AnnotationMirror annoMirror;
if (annotation instanceof MarkerAnnotationExpr) {
String annoName = ((MarkerAnnotationExpr) annotation).getNameAsString();
annoMirror = allStubAnnotations.get(annoName);
} else if (annotation instanceof NormalAnnotationExpr) {
NormalAnnotationExpr nrmanno = (NormalAnnotationExpr) annotation;
String annoName = nrmanno.getNameAsString();
annoMirror = allStubAnnotations.get(annoName);
if (annoMirror == null) {
// Not a supported qualifier -> ignore
return null;
}
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
List<MemberValuePair> pairs = nrmanno.getPairs();
if (pairs != null) {
for (MemberValuePair mvp : pairs) {
String member = mvp.getNameAsString();
Expression exp = mvp.getValue();
boolean success = handleExpr(builder, member, exp);
if (!success) {
stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", exp, annotation);
return null;
}
}
}
return builder.build();
} else if (annotation instanceof SingleMemberAnnotationExpr) {
SingleMemberAnnotationExpr sglanno = (SingleMemberAnnotationExpr) annotation;
String annoName = sglanno.getNameAsString();
annoMirror = allStubAnnotations.get(annoName);
if (annoMirror == null) {
// Not a supported qualifier -> ignore
return null;
}
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, annoMirror);
Expression valexpr = sglanno.getMemberValue();
boolean success = handleExpr(builder, "value", valexpr);
if (!success) {
stubWarn("Annotation expression, %s, could not be processed for annotation: %s. ", valexpr, annotation);
return null;
}
return builder.build();
} else {
ErrorReporter.errorAbort("StubParser: unknown annotation type: " + annotation);
// dead code
annoMirror = null;
}
return annoMirror;
}
use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class AnnotatedTypeFactory method aliasedAnnotation.
/**
* Returns the canonical annotation for the passed annotation if it is an alias of a canonical
* one in the framework. If it is not an alias, the method returns null.
*
* <p>A canonical annotation is the internal annotation that will be used by the Checker
* Framework in the aliased annotation's place.
*
* @param a the qualifier to check for an alias
* @return the canonical annotation or null if none exists
*/
@Nullable
public AnnotationMirror aliasedAnnotation(AnnotationMirror a) {
TypeElement elem = (TypeElement) a.getAnnotationType().asElement();
String qualName = elem.getQualifiedName().toString();
AnnotationMirror canonicalAnno = aliases.get(qualName);
if (canonicalAnno != null && a.getElementValues().size() > 0) {
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, canonicalAnno);
if (aliasesCopyElements.contains(qualName)) {
builder.copyElementValuesFromAnnotation(a, aliasesIgnorableElements.get(qualName));
}
return builder.build();
} else {
return canonicalAnno;
}
}
use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class ValueAnnotatedTypeFactory method createIntValAnnotation.
/**
* Returns a {@link IntVal} or {@link IntRange} annotation using the values. If {@code values}
* is null, then UnknownVal is returned; if {@code values} is empty, then bottom is returned. If
* the number of {@code values} is greater than MAX_VALUES, return an {@link IntRange}. In other
* cases, the values are sorted and duplicates are removed before an {@link IntVal} is created.
*
* @param values list of longs; duplicates are allowed and the values may be in any order
* @return an annotation depends on the values
*/
public AnnotationMirror createIntValAnnotation(List<Long> values) {
if (values == null) {
return UNKNOWNVAL;
}
if (values.isEmpty()) {
return BOTTOMVAL;
}
values = ValueCheckerUtils.removeDuplicates(values);
if (values.size() > MAX_VALUES) {
long valMin = Collections.min(values);
long valMax = Collections.max(values);
return createIntRangeAnnotation(valMin, valMax);
} else {
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, IntVal.class);
builder.setValue("value", values);
return builder.build();
}
}
use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class ValueAnnotatedTypeFactory method createDoubleValAnnotation.
/**
* Returns a {@link DoubleVal} annotation using the values. If {@code values} is null, then
* UnknownVal is returned; if {@code values} is empty, then bottom is returned. The values are
* sorted and duplicates are removed before the annotation is created.
*
* @param values list of doubles; duplicates are allowed and the values may be in any order
* @return a {@link DoubleVal} annotation using the values
*/
public AnnotationMirror createDoubleValAnnotation(List<Double> values) {
if (values == null) {
return UNKNOWNVAL;
}
if (values.isEmpty()) {
return BOTTOMVAL;
}
values = ValueCheckerUtils.removeDuplicates(values);
if (values.size() > MAX_VALUES) {
return UNKNOWNVAL;
} else {
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, DoubleVal.class);
builder.setValue("value", values);
return builder.build();
}
}
use of org.checkerframework.javacutil.AnnotationBuilder in project checker-framework by typetools.
the class ValueAnnotatedTypeFactory method createBooleanAnnotation.
/**
* Returns a {@link BoolVal} annotation using the values. If {@code values} is null, then
* UnknownVal is returned; if {@code values} is empty, then bottom is returned. The values are
* sorted and duplicates are removed before the annotation is created.
*
* @param values list of booleans; duplicates are allowed and the values may be in any order
* @return a {@link BoolVal} annotation using the values
*/
public AnnotationMirror createBooleanAnnotation(List<Boolean> values) {
if (values == null) {
return UNKNOWNVAL;
}
if (values.isEmpty()) {
return BOTTOMVAL;
}
values = ValueCheckerUtils.removeDuplicates(values);
if (values.size() > MAX_VALUES) {
return UNKNOWNVAL;
} else {
AnnotationBuilder builder = new AnnotationBuilder(processingEnv, BoolVal.class);
builder.setValue("value", values);
return builder.build();
}
}
Aggregations