Skip to content

Commit

Permalink
Use only java6 language level
Browse files Browse the repository at this point in the history
  • Loading branch information
acmcelwee committed Jun 15, 2022
1 parent c3ef75e commit 787b9c9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.testing.compile.CompilationSubject.assertThat;
import static com.google.testing.compile.Compiler.javac;
Expand Down Expand Up @@ -241,7 +239,7 @@ public void testInvalidFinalOptionsAndParameters() {

// For every primitive type + String type, the InvalidFinal class defines
// an invalid combination of using a final field with a declared value, for each of those types.
List<String> primitiveTypes = Arrays.asList(
List<String> types = Arrays.asList(
"boolean",
"byte",
"short",
Expand All @@ -252,21 +250,17 @@ public void testInvalidFinalOptionsAndParameters() {
"double",
"string"
);
List<String> fields = primitiveTypes.stream()
.flatMap(t -> {
String titleized = t.substring(0, 1).toUpperCase() + t.substring(1);
return Stream.of(
String.format("invalid%s", titleized),
String.format("invalid%sParam", titleized));
})
.collect(Collectors.toList());

List<String> expectedValidationErrors = fields.stream()
.map(field -> {
String annotation = field.endsWith("Param") ? "@Parameter" : "@Option";
return String.format("Constant (final) primitive and String fields like %s cannot be used as %s: compile-time constant inlining may hide new values written to it.", field, annotation);
})
.collect(Collectors.toList());

String errorFormat = "Constant (final) primitive and String fields like %s cannot be used as %s: compile-time constant inlining may hide new values written to it.";
List<String> expectedValidationErrors = new ArrayList<String>();
for (String type : types) {
String titleized = type.substring(0, 1).toUpperCase() + type.substring(1);
String invalidOptionField = String.format("invalid%s", titleized);
String invalidParamField = String.format("invalid%sParam", titleized);

expectedValidationErrors.add(String.format(errorFormat, invalidOptionField, "@Option"));
expectedValidationErrors.add(String.format(errorFormat, invalidParamField, "@Parameters"));
}

validateErrorMessages(compilation, expectedValidationErrors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private void checkOption(Element e, TypeMirror type, Parameters positional) {
if (positional.split().length() > 0 && !new CompileTimeTypeInfo(type).isMultiValue()) {
error(e, null, "%s has a split regex but is a single-value type", e.getSimpleName());
}
maybeValidateFinalFields(e, type, "@Parameter");
maybeValidateFinalFields(e, type, "@Parameters");
}
}, element.getAnnotation(Parameters.class));
}
Expand Down Expand Up @@ -226,8 +226,10 @@ private void maybeValidateFinalFields(Element e, TypeMirror type, String annotat
if (!isFinal || !shouldValidateFinal) {
return;
}
boolean isConstantValueField = ElementFilter.fieldsIn(Collections.singletonList(e)).stream()
.anyMatch(variableElement -> variableElement.getConstantValue() != null);
boolean isConstantValueField = false;
for (VariableElement variableElement : ElementFilter.fieldsIn(Collections.singletonList(e))) {
isConstantValueField = isConstantValueField || variableElement.getConstantValue() != null;
}
if ((type.getKind().isPrimitive() || typeUtils.isAssignable(type, stringType)) && isConstantValueField) {
error(e, null, "Constant (final) primitive and String fields like %s cannot be used as %s: compile-time constant inlining may hide new values written to it.", e.getSimpleName(), annotation);
}
Expand Down

0 comments on commit 787b9c9

Please sign in to comment.