use of com.google.errorprone.annotations.Var in project java-common-lib by sosy-lab.
the class OptionPlainTextWriter method writeOption.
/**
* Write output for a single option.
*
* @param allInstances All appearances of this option with the same name.
*/
void writeOption(Iterable<AnnotationInfo> allInstances) {
@Var boolean first = true;
for (AnnotationInfo annotation : allInstances) {
String description = getOptionDescription(annotation.element());
if (!description.isEmpty() && !lastDescription.equals(description)) {
if (first) {
out.append('\n');
first = false;
}
out.append(description);
lastDescription = description;
}
}
for (OptionInfo option : from(allInstances).filter(OptionInfo.class)) {
String infoText = getOptionInfo(option);
if (!lastInfo.equals(infoText)) {
out.append(infoText);
lastInfo = infoText;
}
}
}
use of com.google.errorprone.annotations.Var in project java-common-lib by sosy-lab.
the class ParserTest method includeDepthN.
@Test
public final void includeDepthN() throws IOException {
@Var Path included = createTempFile(TEST_FILE_PREFIX, TEST_FILE_SUFFIX, "foo.bar=abc");
List<Path> allFiles = new ArrayList<>();
allFiles.add(included);
for (int i = 0; i < MAX_INCLUDE_TEST_DEPTH; i++) {
included = createTempFile(TEST_FILE_PREFIX, TEST_FILE_SUFFIX, "#include " + included.toAbsolutePath());
allFiles.add(included);
}
try {
testSingleOption("#include " + included.toAbsolutePath(), "foo.bar", "abc");
} finally {
for (Path toDelete : allFiles) {
Files.delete(toDelete);
}
}
}
use of com.google.errorprone.annotations.Var in project java-common-lib by sosy-lab.
the class OrderStatisticMapTestSuite method testEquals.
@Test
public void testEquals() {
EqualsTester mapEqualsTester = new EqualsTester();
@Var OrderStatisticMap<String, String> l1 = createMap();
@Var OrderStatisticMap<String, String> l2 = createMap();
mapEqualsTester.addEqualityGroup(l1, l2);
l1 = createMap();
l2 = createMap();
// Check that the map sorts its elements
for (int i = ELEMS.size() - 1; i >= 0; i--) {
l2.put(ELEMS.get(i).getKey(), ELEMS.get(i).getValue());
}
for (int i = 0; i < ELEMS.size(); i++) {
l1.put(ELEMS.get(i).getKey(), ELEMS.get(i).getValue());
}
// Check the map property
@Var OrderStatisticMap<String, String> l3 = createMap();
for (int i = ELEMS.size() - 1; i >= 0; i--) {
l3.put(ELEMS.get(i).getKey(), ELEMS.get(i).getValue());
}
for (int i = 0; i < ELEMS.size(); i++) {
l3.put(ELEMS.get(i).getKey(), ELEMS.get(i).getValue());
}
mapEqualsTester.addEqualityGroup(l1, l2, l3);
mapEqualsTester.testEquals();
}
use of com.google.errorprone.annotations.Var in project java-common-lib by sosy-lab.
the class OrderStatisticSetTestSuite method testEquals.
@Test
public void testEquals() {
EqualsTester setEqualsTester = new EqualsTester();
@Var OrderStatisticSet<String> l1 = createSet();
@Var OrderStatisticSet<String> l2 = createSet();
setEqualsTester.addEqualityGroup(l1, l2);
l1 = createSet();
l2 = createSet();
Collections.addAll(l1, "a", "b", "c", "d");
Collections.addAll(l2, "d", "c", "a", "b");
OrderStatisticSet<String> l3 = createSet();
Collections.addAll(l3, "d", "c", "a", "b", "a", "b", "a");
setEqualsTester.addEqualityGroup(l1, l2, l3);
setEqualsTester.testEquals();
}
use of com.google.errorprone.annotations.Var in project java-common-lib by sosy-lab.
the class OptionAnnotationProcessor method checkOptionDetailAnnotations.
/**
* This method checks constraints of additional option-detail annotations such as {@link
* IntegerOption}}. The following constraints are checked:
*
* <ul>
* <li>At most one of theese annotations may be present.
* <li>The type of the option must match the applicable types of the annotaton.
* </ul>
*/
private void checkOptionDetailAnnotations(Element elem) {
List<String> usedDetailAnnotations = new ArrayList<>(2);
for (AnnotationMirror am : elem.getAnnotationMirrors()) {
// The @SomeTypeOption annotation at the current element
Element annotation = am.getAnnotationType().asElement();
// The @OptionDetailAnnotation at the declaration of @SomeTypeOption
Optional<? extends AnnotationMirror> optionDetailAnnotation = findAnnotationMirror(OptionDetailAnnotation.class, annotation);
if (!optionDetailAnnotation.isPresent()) {
// not an option-detail annotation
continue;
}
String annotationName = "@" + annotation.getSimpleName();
usedDetailAnnotations.add(annotationName);
// Now we want to compare the type of the option against the types
// that this option-detail annotation declares as compatible
// (cf. OptionDetailAnnotation.applicableTo()).
// Determine type of option as declared in source.
@Var TypeMirror optionType;
switch(elem.getKind()) {
case FIELD:
optionType = elem.asType();
break;
case METHOD:
ExecutableElement method = (ExecutableElement) elem;
if (method.getParameters().size() != 1) {
// error, already reported above
continue;
}
optionType = method.getParameters().get(0).asType();
break;
default:
// error, prevented by compiler
continue;
}
// If this option is an array or a collection, get the component type.
@Var boolean isArray = false;
@Var boolean isCollection = false;
if (optionType.getKind() == TypeKind.ARRAY) {
isArray = true;
optionType = ((ArrayType) optionType).getComponentType();
} else {
String rawTypeName = getRawTypeName(optionType);
for (Class<?> collectionClass : Configuration.COLLECTIONS.keySet()) {
// String comparison for type equality (cf. isSubtypeOf())
if (rawTypeName.equals(collectionClass.getName())) {
List<? extends TypeMirror> params = ((DeclaredType) optionType).getTypeArguments();
if (params.size() != 1) {
// all collections have 1 type parameter, error is reported by compiler itself
continue;
}
isCollection = true;
optionType = params.get(0);
break;
}
}
}
// This is the string we will use for type matching.
// It is raw and boxed, because we have only class literals in the acceptedClasses list.
@Var String optionTypeName = getRawTypeName(optionType);
// Unwrap AnnotatedValue
if (optionTypeName.equals(AnnotatedValue.class.getName())) {
List<? extends TypeMirror> params = ((DeclaredType) optionType).getTypeArguments();
if (params.size() == 1) {
optionType = params.get(0);
optionTypeName = getRawTypeName(optionType);
}
}
// acceptedClasses is a List<AnnotationValue>
// where each AnnotationValue has a TypeMirror/DeclaredType instance as value,
// because applicableTo is defined as array of Class instances.
Iterable<?> acceptedClasses = (Iterable<?>) findAnnotationValue(OptionDetailAnnotation.class, "applicableTo", optionDetailAnnotation.get()).get().getValue();
@Var boolean foundMatchingType = false;
Set<String> acceptedTypeNames = new HashSet<>();
for (Object listEntry : acceptedClasses) {
DeclaredType acceptedType = (DeclaredType) ((AnnotationValue) listEntry).getValue();
acceptedTypeNames.add(acceptedType.toString());
// String comparison for type equality (cf. isSubtypeOf())
if (optionTypeName.equals(acceptedType.toString())) {
foundMatchingType = true;
break;
}
}
if (!Iterables.isEmpty(acceptedClasses) && !foundMatchingType) {
String msgPrefix;
if (isArray) {
msgPrefix = "Array option with incompatible element type";
} else if (isCollection) {
msgPrefix = "Option of collection type with incompatible element type";
} else {
msgPrefix = "Option with incompatible type";
}
message(ERROR, elem, am, String.format("%s %s for annotation %s, this annotation is only for types %s.", msgPrefix, optionType, annotationName, Joiner.on(", ").join(acceptedTypeNames)));
}
}
if (usedDetailAnnotations.size() > 1) {
message(ERROR, elem, "Elements annotated with @Option" + " might have at most one additional option-detail annotation," + " but this element has the following annotations: " + Joiner.on(", ").join(usedDetailAnnotations) + ".");
}
}
Aggregations