diff options
author | Thomas Broyer <t.broyer@ltgt.net> | 2018-03-03 18:03:40 +0100 |
---|---|---|
committer | Jake Wharton <jakewharton@gmail.com> | 2018-03-03 12:03:40 -0500 |
commit | b314337267eb36462c3bf67a87494e614c9c8b40 (patch) | |
tree | 1d0e30db2c64fedb333a1fe9027e88828170a6cc | |
parent | 82e20e0ce26eee0e20cc52f976bb3e7e627dd338 (diff) | |
download | javapoet-b314337267eb36462c3bf67a87494e614c9c8b40.tar.gz |
Update the codebase now that we require Java 8 (#617)
20 files changed, 68 insertions, 113 deletions
diff --git a/src/main/java/com/squareup/javapoet/AnnotationSpec.java b/src/main/java/com/squareup/javapoet/AnnotationSpec.java index 0c96756..d1c5e53 100644 --- a/src/main/java/com/squareup/javapoet/AnnotationSpec.java +++ b/src/main/java/com/squareup/javapoet/AnnotationSpec.java @@ -34,7 +34,7 @@ import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.TypeElement; import javax.lang.model.element.VariableElement; import javax.lang.model.type.TypeMirror; -import javax.lang.model.util.SimpleAnnotationValueVisitor7; +import javax.lang.model.util.SimpleAnnotationValueVisitor8; import static com.squareup.javapoet.Util.characterLiteralWithoutSingleQuotes; import static com.squareup.javapoet.Util.checkArgument; @@ -113,12 +113,7 @@ public final class AnnotationSpec { Builder builder = builder(annotation.annotationType()); try { Method[] methods = annotation.annotationType().getDeclaredMethods(); - Arrays.sort(methods, new Comparator<Method>() { - @Override - public int compare(Method m1, Method m2) { - return m1.getName().compareTo(m2.getName()); - } - }); + Arrays.sort(methods, Comparator.comparing(Method::getName)); for (Method method : methods) { Object value = method.invoke(annotation); if (!includeDefaultValues) { @@ -210,11 +205,7 @@ public final class AnnotationSpec { public Builder addMember(String name, CodeBlock codeBlock) { checkNotNull(name, "name == null"); checkArgument(SourceVersion.isName(name), "not a valid name: %s", name); - List<CodeBlock> values = members.get(name); - if (values == null) { - values = new ArrayList<>(); - members.put(name, values); - } + List<CodeBlock> values = members.computeIfAbsent(name, k -> new ArrayList<>()); values.add(codeBlock); return this; } @@ -254,7 +245,7 @@ public final class AnnotationSpec { /** * Annotation value visitor adding members to the given builder instance. */ - private static class Visitor extends SimpleAnnotationValueVisitor7<Builder, String> { + private static class Visitor extends SimpleAnnotationValueVisitor8<Builder, String> { final Builder builder; Visitor(Builder builder) { diff --git a/src/main/java/com/squareup/javapoet/ArrayTypeName.java b/src/main/java/com/squareup/javapoet/ArrayTypeName.java index 2d02b33..7c54465 100644 --- a/src/main/java/com/squareup/javapoet/ArrayTypeName.java +++ b/src/main/java/com/squareup/javapoet/ArrayTypeName.java @@ -31,7 +31,7 @@ public final class ArrayTypeName extends TypeName { public final TypeName componentType; private ArrayTypeName(TypeName componentType) { - this(componentType, new ArrayList<AnnotationSpec>()); + this(componentType, new ArrayList<>()); } private ArrayTypeName(TypeName componentType, List<AnnotationSpec> annotations) { @@ -63,7 +63,7 @@ public final class ArrayTypeName extends TypeName { /** Returns an array type equivalent to {@code mirror}. */ public static ArrayTypeName get(ArrayType mirror) { - return get(mirror, new LinkedHashMap<TypeParameterElement, TypeVariableName>()); + return get(mirror, new LinkedHashMap<>()); } static ArrayTypeName get( @@ -73,7 +73,7 @@ public final class ArrayTypeName extends TypeName { /** Returns an array type equivalent to {@code type}. */ public static ArrayTypeName get(GenericArrayType type) { - return get(type, new LinkedHashMap<Type, TypeVariableName>()); + return get(type, new LinkedHashMap<>()); } static ArrayTypeName get(GenericArrayType type, Map<Type, TypeVariableName> map) { diff --git a/src/main/java/com/squareup/javapoet/ClassName.java b/src/main/java/com/squareup/javapoet/ClassName.java index 4161025..c5c065d 100644 --- a/src/main/java/com/squareup/javapoet/ClassName.java +++ b/src/main/java/com/squareup/javapoet/ClassName.java @@ -40,7 +40,7 @@ public final class ClassName extends TypeName implements Comparable<ClassName> { final String canonicalName; private ClassName(List<String> names) { - this(names, new ArrayList<AnnotationSpec>()); + this(names, new ArrayList<>()); } private ClassName(List<String> names, List<AnnotationSpec> annotations) { @@ -50,8 +50,8 @@ public final class ClassName extends TypeName implements Comparable<ClassName> { } this.names = Util.immutableList(names); this.canonicalName = (names.get(0).isEmpty() - ? Util.join(".", names.subList(1, names.size())) - : Util.join(".", names)); + ? String.join(".", names.subList(1, names.size())) + : String.join(".", names)); } @Override public ClassName annotated(List<AnnotationSpec> annotations) { diff --git a/src/main/java/com/squareup/javapoet/CodeBlock.java b/src/main/java/com/squareup/javapoet/CodeBlock.java index cdddc0b..33e3846 100644 --- a/src/main/java/com/squareup/javapoet/CodeBlock.java +++ b/src/main/java/com/squareup/javapoet/CodeBlock.java @@ -306,7 +306,7 @@ public final class CodeBlock { } } String s = unused.size() == 1 ? "" : "s"; - checkArgument(unused.isEmpty(), "unused argument%s: %s", s, Util.join(", ", unused)); + checkArgument(unused.isEmpty(), "unused argument%s: %s", s, String.join(", ", unused)); } return this; } diff --git a/src/main/java/com/squareup/javapoet/CodeWriter.java b/src/main/java/com/squareup/javapoet/CodeWriter.java index 4743ea9..9468a08 100644 --- a/src/main/java/com/squareup/javapoet/CodeWriter.java +++ b/src/main/java/com/squareup/javapoet/CodeWriter.java @@ -33,8 +33,8 @@ import javax.lang.model.element.Modifier; import static com.squareup.javapoet.Util.checkArgument; import static com.squareup.javapoet.Util.checkNotNull; import static com.squareup.javapoet.Util.checkState; -import static com.squareup.javapoet.Util.join; import static com.squareup.javapoet.Util.stringLiteralWithDoubleQuotes; +import static java.lang.String.join; /** * Converts a {@link JavaFile} to a string suitable to both human- and javac-consumption. This @@ -67,11 +67,11 @@ final class CodeWriter { int statementLine = -1; CodeWriter(Appendable out) { - this(out, " ", Collections.<String>emptySet()); + this(out, " ", Collections.emptySet()); } CodeWriter(Appendable out, String indent, Set<String> staticImports) { - this(out, indent, Collections.<String, ClassName>emptyMap(), staticImports); + this(out, indent, Collections.emptyMap(), staticImports); } CodeWriter(Appendable out, String indent, Map<String, ClassName> importedTypes, @@ -177,7 +177,7 @@ final class CodeWriter { } public void emitModifiers(Set<Modifier> modifiers) throws IOException { - emitModifiers(modifiers, Collections.<Modifier>emptySet()); + emitModifiers(modifiers, Collections.emptySet()); } /** @@ -339,7 +339,7 @@ final class CodeWriter { private void emitLiteral(Object o) throws IOException { if (o instanceof TypeSpec) { TypeSpec typeSpec = (TypeSpec) o; - typeSpec.emit(this, null, Collections.<Modifier>emptySet()); + typeSpec.emit(this, null, Collections.emptySet()); } else if (o instanceof AnnotationSpec) { AnnotationSpec annotationSpec = (AnnotationSpec) o; annotationSpec.emit(this, true); diff --git a/src/main/java/com/squareup/javapoet/FieldSpec.java b/src/main/java/com/squareup/javapoet/FieldSpec.java index 5e3e630..851b36d 100644 --- a/src/main/java/com/squareup/javapoet/FieldSpec.java +++ b/src/main/java/com/squareup/javapoet/FieldSpec.java @@ -79,7 +79,7 @@ public final class FieldSpec { StringBuilder out = new StringBuilder(); try { CodeWriter codeWriter = new CodeWriter(out); - emit(codeWriter, Collections.<Modifier>emptySet()); + emit(codeWriter, Collections.emptySet()); return out.toString(); } catch (IOException e) { throw new AssertionError(); diff --git a/src/main/java/com/squareup/javapoet/JavaFile.java b/src/main/java/com/squareup/javapoet/JavaFile.java index 4852f87..b6b5a75 100644 --- a/src/main/java/com/squareup/javapoet/JavaFile.java +++ b/src/main/java/com/squareup/javapoet/JavaFile.java @@ -32,7 +32,6 @@ import java.util.Set; import java.util.TreeSet; import javax.annotation.processing.Filer; import javax.lang.model.element.Element; -import javax.lang.model.element.Modifier; import javax.tools.JavaFileObject; import javax.tools.JavaFileObject.Kind; import javax.tools.SimpleJavaFileObject; @@ -154,7 +153,7 @@ public final class JavaFile { codeWriter.emit("\n"); } - typeSpec.emit(codeWriter, null, Collections.<Modifier>emptySet()); + typeSpec.emit(codeWriter, null, Collections.emptySet()); codeWriter.popPackage(); } diff --git a/src/main/java/com/squareup/javapoet/MethodSpec.java b/src/main/java/com/squareup/javapoet/MethodSpec.java index 0073445..eff5410 100644 --- a/src/main/java/com/squareup/javapoet/MethodSpec.java +++ b/src/main/java/com/squareup/javapoet/MethodSpec.java @@ -161,7 +161,7 @@ public final class MethodSpec { StringBuilder out = new StringBuilder(); try { CodeWriter codeWriter = new CodeWriter(out); - emit(codeWriter, "Constructor", Collections.<Modifier>emptySet()); + emit(codeWriter, "Constructor", Collections.emptySet()); return out.toString(); } catch (IOException e) { throw new AssertionError(); @@ -202,7 +202,7 @@ public final class MethodSpec { modifiers = new LinkedHashSet<>(modifiers); modifiers.remove(Modifier.ABSTRACT); - modifiers.remove(Util.DEFAULT); // LinkedHashSet permits null as element for Java 7 + modifiers.remove(Modifier.DEFAULT); methodBuilder.addModifiers(modifiers); for (TypeParameterElement typeParameterElement : method.getTypeParameters()) { diff --git a/src/main/java/com/squareup/javapoet/NameAllocator.java b/src/main/java/com/squareup/javapoet/NameAllocator.java index 961cba5..8269664 100644 --- a/src/main/java/com/squareup/javapoet/NameAllocator.java +++ b/src/main/java/com/squareup/javapoet/NameAllocator.java @@ -83,7 +83,7 @@ public final class NameAllocator implements Cloneable { private final Map<Object, String> tagToName; public NameAllocator() { - this(new LinkedHashSet<String>(), new LinkedHashMap<Object, String>()); + this(new LinkedHashSet<>(), new LinkedHashMap<>()); } private NameAllocator(LinkedHashSet<String> allocatedNames, diff --git a/src/main/java/com/squareup/javapoet/ParameterizedTypeName.java b/src/main/java/com/squareup/javapoet/ParameterizedTypeName.java index aadb9ae..d46aba8 100644 --- a/src/main/java/com/squareup/javapoet/ParameterizedTypeName.java +++ b/src/main/java/com/squareup/javapoet/ParameterizedTypeName.java @@ -35,7 +35,7 @@ public final class ParameterizedTypeName extends TypeName { ParameterizedTypeName(ParameterizedTypeName enclosingType, ClassName rawType, List<TypeName> typeArguments) { - this(enclosingType, rawType, typeArguments, new ArrayList<AnnotationSpec>()); + this(enclosingType, rawType, typeArguments, new ArrayList<>()); } private ParameterizedTypeName(ParameterizedTypeName enclosingType, ClassName rawType, @@ -59,8 +59,7 @@ public final class ParameterizedTypeName extends TypeName { } @Override public TypeName withoutAnnotations() { - return new ParameterizedTypeName( - enclosingType, rawType, typeArguments, new ArrayList<AnnotationSpec>()); + return new ParameterizedTypeName(enclosingType, rawType, typeArguments, new ArrayList<>()); } @Override CodeWriter emit(CodeWriter out) throws IOException { @@ -92,8 +91,8 @@ public final class ParameterizedTypeName extends TypeName { */ public ParameterizedTypeName nestedClass(String name) { checkNotNull(name, "name == null"); - return new ParameterizedTypeName(this, rawType.nestedClass(name), new ArrayList<TypeName>(), - new ArrayList<AnnotationSpec>()); + return new ParameterizedTypeName(this, rawType.nestedClass(name), new ArrayList<>(), + new ArrayList<>()); } /** @@ -103,7 +102,7 @@ public final class ParameterizedTypeName extends TypeName { public ParameterizedTypeName nestedClass(String name, List<TypeName> typeArguments) { checkNotNull(name, "name == null"); return new ParameterizedTypeName(this, rawType.nestedClass(name), typeArguments, - new ArrayList<AnnotationSpec>()); + new ArrayList<>()); } /** Returns a parameterized type, applying {@code typeArguments} to {@code rawType}. */ @@ -118,7 +117,7 @@ public final class ParameterizedTypeName extends TypeName { /** Returns a parameterized type equivalent to {@code type}. */ public static ParameterizedTypeName get(ParameterizedType type) { - return get(type, new LinkedHashMap<Type, TypeVariableName>()); + return get(type, new LinkedHashMap<>()); } /** Returns a parameterized type equivalent to {@code type}. */ diff --git a/src/main/java/com/squareup/javapoet/TypeName.java b/src/main/java/com/squareup/javapoet/TypeName.java index 401887d..e09d785 100644 --- a/src/main/java/com/squareup/javapoet/TypeName.java +++ b/src/main/java/com/squareup/javapoet/TypeName.java @@ -36,7 +36,7 @@ import javax.lang.model.type.NoType; import javax.lang.model.type.PrimitiveType; import javax.lang.model.type.TypeKind; import javax.lang.model.type.TypeMirror; -import javax.lang.model.util.SimpleTypeVisitor7; +import javax.lang.model.util.SimpleTypeVisitor8; /** * Any type in Java's type system, plus {@code void}. This class is an identifier for primitive @@ -95,7 +95,7 @@ public class TypeName { private String cachedString; private TypeName(String keyword) { - this(keyword, new ArrayList<AnnotationSpec>()); + this(keyword, new ArrayList<>()); } private TypeName(String keyword, List<AnnotationSpec> annotations) { @@ -235,12 +235,12 @@ public class TypeName { /** Returns a type name equivalent to {@code mirror}. */ public static TypeName get(TypeMirror mirror) { - return get(mirror, new LinkedHashMap<TypeParameterElement, TypeVariableName>()); + return get(mirror, new LinkedHashMap<>()); } static TypeName get(TypeMirror mirror, final Map<TypeParameterElement, TypeVariableName> typeVariables) { - return mirror.accept(new SimpleTypeVisitor7<TypeName, Void>() { + return mirror.accept(new SimpleTypeVisitor8<TypeName, Void>() { @Override public TypeName visitPrimitive(PrimitiveType t, Void p) { switch (t.getKind()) { case BOOLEAN: @@ -315,7 +315,7 @@ public class TypeName { /** Returns a type name equivalent to {@code type}. */ public static TypeName get(Type type) { - return get(type, new LinkedHashMap<Type, TypeVariableName>()); + return get(type, new LinkedHashMap<>()); } static TypeName get(Type type, Map<Type, TypeVariableName> map) { @@ -352,7 +352,7 @@ public class TypeName { /** Converts an array of types to a list of type names. */ static List<TypeName> list(Type[] types) { - return list(types, new LinkedHashMap<Type, TypeVariableName>()); + return list(types, new LinkedHashMap<>()); } static List<TypeName> list(Type[] types, Map<Type, TypeVariableName> map) { diff --git a/src/main/java/com/squareup/javapoet/TypeSpec.java b/src/main/java/com/squareup/javapoet/TypeSpec.java index 73052d0..feac8ef 100644 --- a/src/main/java/com/squareup/javapoet/TypeSpec.java +++ b/src/main/java/com/squareup/javapoet/TypeSpec.java @@ -34,7 +34,6 @@ import javax.lang.model.element.Modifier; import static com.squareup.javapoet.Util.checkArgument; import static com.squareup.javapoet.Util.checkNotNull; import static com.squareup.javapoet.Util.checkState; -import static com.squareup.javapoet.Util.hasDefaultModifier; import static com.squareup.javapoet.Util.requireExactlyOneOf; /** A generated class, interface, or enum declaration. */ @@ -215,7 +214,7 @@ public final class TypeSpec { implementsTypes = Collections.emptyList(); } else { extendsTypes = superclass.equals(ClassName.OBJECT) - ? Collections.<TypeName>emptyList() + ? Collections.emptyList() : Collections.singletonList(superclass); implementsTypes = superinterfaces; } @@ -252,8 +251,7 @@ public final class TypeSpec { i.hasNext(); ) { Map.Entry<String, TypeSpec> enumConstant = i.next(); if (!firstMember) codeWriter.emit("\n"); - enumConstant.getValue() - .emit(codeWriter, enumConstant.getKey(), Collections.<Modifier>emptySet()); + enumConstant.getValue().emit(codeWriter, enumConstant.getKey(), Collections.emptySet()); firstMember = false; if (i.hasNext()) { codeWriter.emit(",\n"); @@ -343,7 +341,7 @@ public final class TypeSpec { StringBuilder out = new StringBuilder(); try { CodeWriter codeWriter = new CodeWriter(out); - emit(codeWriter, null, Collections.<Modifier>emptySet()); + emit(codeWriter, null, Collections.emptySet()); return out.toString(); } catch (IOException e) { throw new AssertionError(); @@ -352,28 +350,28 @@ public final class TypeSpec { public enum Kind { CLASS( - Collections.<Modifier>emptySet(), - Collections.<Modifier>emptySet(), - Collections.<Modifier>emptySet(), - Collections.<Modifier>emptySet()), + Collections.emptySet(), + Collections.emptySet(), + Collections.emptySet(), + Collections.emptySet()), INTERFACE( Util.immutableSet(Arrays.asList(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)), Util.immutableSet(Arrays.asList(Modifier.PUBLIC, Modifier.ABSTRACT)), Util.immutableSet(Arrays.asList(Modifier.PUBLIC, Modifier.STATIC)), - Util.immutableSet(Arrays.asList(Modifier.STATIC))), + Util.immutableSet(Collections.singletonList(Modifier.STATIC))), ENUM( - Collections.<Modifier>emptySet(), - Collections.<Modifier>emptySet(), - Collections.<Modifier>emptySet(), + Collections.emptySet(), + Collections.emptySet(), + Collections.emptySet(), Collections.singleton(Modifier.STATIC)), ANNOTATION( Util.immutableSet(Arrays.asList(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)), Util.immutableSet(Arrays.asList(Modifier.PUBLIC, Modifier.ABSTRACT)), Util.immutableSet(Arrays.asList(Modifier.PUBLIC, Modifier.STATIC)), - Util.immutableSet(Arrays.asList(Modifier.STATIC))); + Util.immutableSet(Collections.singletonList(Modifier.STATIC))); private final Set<Modifier> implicitFieldModifiers; private final Set<Modifier> implicitMethodModifiers; @@ -571,7 +569,8 @@ public final class TypeSpec { public Builder addMethod(MethodSpec methodSpec) { if (kind == Kind.INTERFACE) { - requireExactlyOneOf(methodSpec.modifiers, Modifier.ABSTRACT, Modifier.STATIC, Util.DEFAULT); + requireExactlyOneOf(methodSpec.modifiers, Modifier.ABSTRACT, Modifier.STATIC, + Modifier.DEFAULT); requireExactlyOneOf(methodSpec.modifiers, Modifier.PUBLIC, Modifier.PRIVATE); } else if (kind == Kind.ANNOTATION) { checkState(methodSpec.modifiers.equals(kind.implicitMethodModifiers), @@ -583,7 +582,7 @@ public final class TypeSpec { kind, name, methodSpec.name); } if (kind != Kind.INTERFACE) { - checkState(!hasDefaultModifier(methodSpec.modifiers), "%s %s.%s cannot be default", + checkState(!methodSpec.hasModifier(Modifier.DEFAULT), "%s %s.%s cannot be default", kind, name, methodSpec.name); } methodSpecs.add(methodSpec); diff --git a/src/main/java/com/squareup/javapoet/TypeVariableName.java b/src/main/java/com/squareup/javapoet/TypeVariableName.java index b3e3e03..e0e54fb 100644 --- a/src/main/java/com/squareup/javapoet/TypeVariableName.java +++ b/src/main/java/com/squareup/javapoet/TypeVariableName.java @@ -36,7 +36,7 @@ public final class TypeVariableName extends TypeName { public final List<TypeName> bounds; private TypeVariableName(String name, List<TypeName> bounds) { - this(name, bounds, new ArrayList<AnnotationSpec>()); + this(name, bounds, new ArrayList<>()); } private TypeVariableName(String name, List<TypeName> bounds, List<AnnotationSpec> annotations) { @@ -85,7 +85,7 @@ public final class TypeVariableName extends TypeName { /** Returns type variable named {@code name} without bounds. */ public static TypeVariableName get(String name) { - return TypeVariableName.of(name, Collections.<TypeName>emptyList()); + return TypeVariableName.of(name, Collections.emptyList()); } /** Returns type variable named {@code name} with {@code bounds}. */ @@ -145,7 +145,7 @@ public final class TypeVariableName extends TypeName { /** Returns type variable equivalent to {@code type}. */ public static TypeVariableName get(java.lang.reflect.TypeVariable<?> type) { - return get(type, new LinkedHashMap<Type, TypeVariableName>()); + return get(type, new LinkedHashMap<>()); } /** @see #get(java.lang.reflect.TypeVariable, Map) */ diff --git a/src/main/java/com/squareup/javapoet/Util.java b/src/main/java/com/squareup/javapoet/Util.java index 15e3909..e0eabad 100644 --- a/src/main/java/com/squareup/javapoet/Util.java +++ b/src/main/java/com/squareup/javapoet/Util.java @@ -36,17 +36,6 @@ final class Util { private Util() { } - /** Modifier.DEFAULT doesn't exist until Java 8, but we want to run on earlier releases. */ - static final Modifier DEFAULT; - static { - Modifier def = null; - try { - def = Modifier.valueOf("DEFAULT"); - } catch (IllegalArgumentException ignored) { - } - DEFAULT = def; - } - static <K, V> Map<K, List<V>> immutableMultimap(Map<K, List<V>> multimap) { LinkedHashMap<K, List<V>> result = new LinkedHashMap<>(); for (Map.Entry<K, List<V>> entry : multimap.entrySet()) { @@ -81,16 +70,6 @@ final class Util { return Collections.unmodifiableSet(new LinkedHashSet<>(set)); } - static String join(String separator, List<String> parts) { - if (parts.isEmpty()) return ""; - StringBuilder result = new StringBuilder(); - result.append(parts.get(0)); - for (int i = 1; i < parts.size(); i++) { - result.append(separator).append(parts.get(i)); - } - return result.toString(); - } - static <T> Set<T> union(Set<T> a, Set<T> b) { Set<T> result = new LinkedHashSet<>(); result.addAll(a); @@ -101,17 +80,12 @@ final class Util { static void requireExactlyOneOf(Set<Modifier> modifiers, Modifier... mutuallyExclusive) { int count = 0; for (Modifier modifier : mutuallyExclusive) { - if (modifier == null && Util.DEFAULT == null) continue; // Skip 'DEFAULT' if it doesn't exist! if (modifiers.contains(modifier)) count++; } checkArgument(count == 1, "modifiers %s must contain one of %s", modifiers, Arrays.toString(mutuallyExclusive)); } - static boolean hasDefaultModifier(Collection<Modifier> modifiers) { - return DEFAULT != null && modifiers.contains(DEFAULT); - } - static String characterLiteralWithoutSingleQuotes(char c) { // see https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.6 switch (c) { diff --git a/src/main/java/com/squareup/javapoet/WildcardTypeName.java b/src/main/java/com/squareup/javapoet/WildcardTypeName.java index cf33bb8..17cb73f 100644 --- a/src/main/java/com/squareup/javapoet/WildcardTypeName.java +++ b/src/main/java/com/squareup/javapoet/WildcardTypeName.java @@ -19,7 +19,6 @@ import java.io.IOException; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; import java.util.ArrayList; -import java.util.Arrays; import java.util.Collections; import java.util.LinkedHashMap; import java.util.List; @@ -34,7 +33,7 @@ public final class WildcardTypeName extends TypeName { public final List<TypeName> lowerBounds; private WildcardTypeName(List<TypeName> upperBounds, List<TypeName> lowerBounds) { - this(upperBounds, lowerBounds, new ArrayList<AnnotationSpec>()); + this(upperBounds, lowerBounds, new ArrayList<>()); } private WildcardTypeName(List<TypeName> upperBounds, List<TypeName> lowerBounds, @@ -78,7 +77,7 @@ public final class WildcardTypeName extends TypeName { * ? extends Object}. */ public static WildcardTypeName subtypeOf(TypeName upperBound) { - return new WildcardTypeName(Arrays.asList(upperBound), Collections.<TypeName>emptyList()); + return new WildcardTypeName(Collections.singletonList(upperBound), Collections.emptyList()); } public static WildcardTypeName subtypeOf(Type upperBound) { @@ -90,7 +89,8 @@ public final class WildcardTypeName extends TypeName { * bound} is {@code String.class}, this returns {@code ? super String}. */ public static WildcardTypeName supertypeOf(TypeName lowerBound) { - return new WildcardTypeName(Arrays.<TypeName>asList(OBJECT), Arrays.asList(lowerBound)); + return new WildcardTypeName(Collections.singletonList(OBJECT), + Collections.singletonList(lowerBound)); } public static WildcardTypeName supertypeOf(Type lowerBound) { @@ -98,7 +98,7 @@ public final class WildcardTypeName extends TypeName { } public static TypeName get(javax.lang.model.type.WildcardType mirror) { - return get(mirror, new LinkedHashMap<TypeParameterElement, TypeVariableName>()); + return get(mirror, new LinkedHashMap<>()); } static TypeName get( @@ -118,7 +118,7 @@ public final class WildcardTypeName extends TypeName { } public static TypeName get(WildcardType wildcardName) { - return get(wildcardName, new LinkedHashMap<Type, TypeVariableName>()); + return get(wildcardName, new LinkedHashMap<>()); } static TypeName get(WildcardType wildcardName, Map<Type, TypeVariableName> map) { diff --git a/src/test/java/com/squareup/javapoet/AnnotatedTypeNameTest.java b/src/test/java/com/squareup/javapoet/AnnotatedTypeNameTest.java index 142af72..18b7f7e 100644 --- a/src/test/java/com/squareup/javapoet/AnnotatedTypeNameTest.java +++ b/src/test/java/com/squareup/javapoet/AnnotatedTypeNameTest.java @@ -20,6 +20,8 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; +import java.lang.annotation.ElementType; +import java.lang.annotation.Target; import java.util.List; import java.util.Map; import org.junit.Ignore; @@ -115,7 +117,7 @@ public class AnnotatedTypeNameTest { } // https://github.com/square/javapoet/issues/431 - // @Target(ElementType.TYPE_USE) requires Java 1.8 + @Target(ElementType.TYPE_USE) public @interface TypeUseAnnotation {} // https://github.com/square/javapoet/issues/431 diff --git a/src/test/java/com/squareup/javapoet/FileReadingTest.java b/src/test/java/com/squareup/javapoet/FileReadingTest.java index 27041a0..eb19de0 100644 --- a/src/test/java/com/squareup/javapoet/FileReadingTest.java +++ b/src/test/java/com/squareup/javapoet/FileReadingTest.java @@ -105,8 +105,8 @@ public class FileReadingTest { CompilationTask task = compiler.getTask(null, fileManager, diagnosticCollector, - Collections.<String>emptySet(), - Collections.<String>emptySet(), + Collections.emptySet(), + Collections.emptySet(), Collections.singleton(javaFile.toJavaFileObject())); assertThat(task.call()).isTrue(); diff --git a/src/test/java/com/squareup/javapoet/MethodSpecTest.java b/src/test/java/com/squareup/javapoet/MethodSpecTest.java index 1a7205c..96923eb 100644 --- a/src/test/java/com/squareup/javapoet/MethodSpecTest.java +++ b/src/test/java/com/squareup/javapoet/MethodSpecTest.java @@ -39,7 +39,6 @@ import org.junit.Test; import static com.google.common.collect.Iterables.getOnlyElement; import static com.google.common.truth.Truth.assertThat; -import static com.google.common.truth.TruthJUnit.assume; import static javax.lang.model.util.ElementFilter.methodsIn; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -152,7 +151,6 @@ public final class MethodSpecTest { DeclaredType classType = (DeclaredType) classElement.asType(); List<ExecutableElement> methods = methodsIn(elements.getAllMembers(classElement)); ExecutableElement exec = findFirst(methods, "iterator"); - assume().that(Util.DEFAULT).isNotNull(); exec = findFirst(methods, "spliterator"); MethodSpec method = MethodSpec.overriding(exec, classType, types).build(); assertThat(method.toString()).isEqualTo("" diff --git a/src/test/java/com/squareup/javapoet/TypeSpecTest.java b/src/test/java/com/squareup/javapoet/TypeSpecTest.java index 145d9bc..5d87582 100644 --- a/src/test/java/com/squareup/javapoet/TypeSpecTest.java +++ b/src/test/java/com/squareup/javapoet/TypeSpecTest.java @@ -44,7 +44,6 @@ import org.mockito.Mockito; import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; -import static org.junit.Assume.assumeTrue; @RunWith(JUnit4.class) public final class TypeSpecTest { @@ -57,10 +56,6 @@ public final class TypeSpecTest { return compilation.getElements().getTypeElement(clazz.getCanonicalName()); } - private boolean isJava8() { - return Util.DEFAULT != null; - } - @Test public void basic() throws Exception { TypeSpec taco = TypeSpec.classBuilder("Taco") .addMethod(MethodSpec.methodBuilder("toString") @@ -832,11 +827,10 @@ public final class TypeSpecTest { @Test public void classCannotHaveDefaultMethods() throws Exception { - assumeTrue(isJava8()); try { TypeSpec.classBuilder("Tacos") .addMethod(MethodSpec.methodBuilder("test") - .addModifiers(Modifier.PUBLIC, Modifier.valueOf("DEFAULT")) + .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .returns(int.class) .addCode(CodeBlock.builder().addStatement("return 0").build()) .build()) @@ -869,10 +863,9 @@ public final class TypeSpecTest { @Test public void interfaceDefaultMethods() throws Exception { - assumeTrue(isJava8()); TypeSpec bar = TypeSpec.interfaceBuilder("Tacos") .addMethod(MethodSpec.methodBuilder("test") - .addModifiers(Modifier.PUBLIC, Modifier.valueOf("DEFAULT")) + .addModifiers(Modifier.PUBLIC, Modifier.DEFAULT) .returns(int.class) .addCode(CodeBlock.builder().addStatement("return 0").build()) .build()) diff --git a/src/test/java/com/squareup/javapoet/TypesEclipseTest.java b/src/test/java/com/squareup/javapoet/TypesEclipseTest.java index 96b846b..2759f17 100644 --- a/src/test/java/com/squareup/javapoet/TypesEclipseTest.java +++ b/src/test/java/com/squareup/javapoet/TypesEclipseTest.java @@ -65,7 +65,7 @@ public final class TypesEclipseTest extends AbstractTypesTest { public Statement apply(final Statement base, Description description) { return new Statement() { @Override public void evaluate() throws Throwable { - final AtomicReference<Throwable> thrown = new AtomicReference<Throwable>(); + final AtomicReference<Throwable> thrown = new AtomicReference<>(); boolean successful = compile(ImmutableList.of(new AbstractProcessor() { @Override public SourceVersion getSupportedSourceVersion() { @@ -130,15 +130,15 @@ public final class TypesEclipseTest extends AbstractTypesTest { static private boolean compile(Iterable<? extends Processor> processors) { JavaCompiler compiler = new EclipseCompiler(); DiagnosticCollector<JavaFileObject> diagnosticCollector = - new DiagnosticCollector<JavaFileObject>(); + new DiagnosticCollector<>(); JavaFileManager fileManager = compiler.getStandardFileManager(diagnosticCollector, Locale.getDefault(), UTF_8); JavaCompiler.CompilationTask task = compiler.getTask( null, fileManager, diagnosticCollector, - ImmutableSet.<String>of(), + ImmutableSet.of(), ImmutableSet.of(TypesEclipseTest.class.getCanonicalName()), - ImmutableSet.<JavaFileObject>of()); + ImmutableSet.of()); task.setProcessors(processors); return task.call(); } |