Skip to content

Commit

Permalink
Miscellaneous code cleanup (#20)
Browse files Browse the repository at this point in the history
* Use `StandardCharset` where possible
* Remove raw use of parameterized class
* Use diamond operator where possible
* Remove unused imports
* Collapse identical catch blocks
* Use try-with-resources where possible
* Use `Collections.singletonList` where possible
* Arrays of wisdom of the ancients
* Add missing `@Override` annotations
* Remove star imports
  • Loading branch information
basil authored Nov 11, 2021
1 parent 7b28cc8 commit 11de84a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
import java.nio.charset.StandardCharsets;
import java.nio.file.NoSuchFileException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -50,11 +52,11 @@ private final class Use {
/**
* Strings that designate FQCNs where annotations are used, either on a class or its members.
*/
final Set<String> classes = new TreeSet<String>();
final Set<String> classes = new TreeSet<>();
/**
* Keeps track of elements that has the annotation.
*/
final Set<Element> originatingElements = new HashSet<Element>();
final Set<Element> originatingElements = new HashSet<>();

private Use(String annotationName) {
this.annotationName = annotationName;
Expand Down Expand Up @@ -100,21 +102,16 @@ String getIndexFileName() {
* Loads existing index, if it exists.
*/
List<String> loadExisting() throws IOException {
List<String> elements = new ArrayList<String>();
List<String> elements = new ArrayList<>();
try {
FileObject in = processingEnv.getFiler().getResource(CLASS_OUTPUT, "", getIndexFileName());
// Read existing annotations, for incremental compilation.
BufferedReader is = new BufferedReader(new InputStreamReader(in.openInputStream(),"UTF-8"));
try {
try (BufferedReader is = new BufferedReader(new InputStreamReader(in.openInputStream(), StandardCharsets.UTF_8))) {
String line;
while ((line=is.readLine())!=null)
elements.add(line);
} finally {
is.close();
}
} catch (FileNotFoundException x) {
// OK, created for the first time
} catch (java.nio.file.NoSuchFileException x) {
} catch (FileNotFoundException | NoSuchFileException x) {
// OK, created for the first time
}
return elements;
Expand All @@ -124,14 +121,11 @@ void write() {
try {
FileObject out = processingEnv.getFiler().createResource(CLASS_OUTPUT,
"", getIndexFileName(),
originatingElements.toArray(new Element[originatingElements.size()]));
originatingElements.toArray(new Element[0]));

PrintWriter w = new PrintWriter(new OutputStreamWriter(out.openOutputStream(),"UTF-8"));
try {
try (PrintWriter w = new PrintWriter(new OutputStreamWriter(out.openOutputStream(), StandardCharsets.UTF_8))) {
for (String el : classes)
w.println(el);
} finally {
w.close();
}
} catch (IOException x) {
processingEnv.getMessager().printMessage(Kind.ERROR, x.toString());
Expand Down Expand Up @@ -160,7 +154,7 @@ public SourceVersion getSupportedSourceVersion() {

protected void execute(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
// map from indexable annotation names, to actual uses
Map<String,Use> output = new HashMap<String,Use>();
Map<String,Use> output = new HashMap<>();
scan(annotations, roundEnv, output);
for (Use u : output.values())
u.write();
Expand Down
16 changes: 10 additions & 6 deletions src/main/java/org/jvnet/hudson/annotation_indexer/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedElement;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Iterator;
Expand Down Expand Up @@ -35,8 +36,9 @@ public class Index {
public static <T extends AnnotatedElement> Iterable<T> list(Class<? extends Annotation> type, ClassLoader cl, final Class<T> subType) throws IOException {
final Iterable<AnnotatedElement> base = list(type,cl);
return new Iterable<T>() {
@Override
public Iterator<T> iterator() {
return new SubtypeIterator<AnnotatedElement,T>(base.iterator(), subType);
return new SubtypeIterator<>(base.iterator(), subType);
}
};
}
Expand All @@ -59,7 +61,7 @@ public static Set<String> listClassNames(Class<? extends Annotation> type, Class
URL url = res.nextElement();

try (InputStream is = url.openStream();
BufferedReader r = new BufferedReader(new InputStreamReader(is, "UTF-8"))) {
BufferedReader r = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
String line;
while ((line = r.readLine()) != null) {
ids.add(line);
Expand All @@ -77,6 +79,7 @@ public static Set<String> listClassNames(Class<? extends Annotation> type, Class
public static Iterable<AnnotatedElement> list(final Class<? extends Annotation> type, final ClassLoader cl) throws IOException {
Set<String> ids = listClassNames(type, cl);
return new Iterable<AnnotatedElement>() {
@Override
public Iterator<AnnotatedElement> iterator() {
return new Iterator<AnnotatedElement>() {
/**
Expand All @@ -86,20 +89,23 @@ public Iterator<AnnotatedElement> iterator() {

private final Iterator<String> iditr = ids.iterator();

private final List<AnnotatedElement> lookaheads = new LinkedList<AnnotatedElement>();
private final List<AnnotatedElement> lookaheads = new LinkedList<>();

@Override
public boolean hasNext() {
fetch();
return next!=null;
}

@Override
public AnnotatedElement next() {
fetch();
AnnotatedElement r = next;
next = null;
return r;
}

@Override
public void remove() {
throw new UnsupportedOperationException();
}
Expand Down Expand Up @@ -129,9 +135,7 @@ private void fetch() {
listAnnotatedElements(c.getDeclaredConstructors());
} catch (ClassNotFoundException e) {
LOGGER.log(Level.FINE, "Failed to load: "+name,e);
} catch (LinkageError x) {
LOGGER.log(Level.WARNING, "Failed to load " + name, x);
} catch (RuntimeException x) {
} catch (LinkageError | RuntimeException x) {
LOGGER.log(Level.WARNING, "Failed to load " + name, x);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,21 @@ private void fetch() {
}
}

@Override
public boolean hasNext() {
fetch();
return fetched;
}

@Override
public U next() {
fetch();
if(!fetched) throw new NoSuchElementException();
fetched = false;
return type.cast(next);
}

@Override
public void remove() {
core.remove();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
import java.util.Iterator;
import net.java.dev.hickory.testing.Compilation;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class AnnotationProcessorImplTest {

Expand Down Expand Up @@ -105,7 +107,7 @@ public static class Stuff {@OnConst public Stuff() {}}
@Test public void constructors() throws Exception {
Iterator<AnnotatedElement> it = Index.list(OnConst.class, Stuff.class.getClassLoader()).iterator();
assertTrue(it.hasNext());
Constructor<?> c = (Constructor) it.next();
Constructor<?> c = (Constructor<?>) it.next();
assertEquals(Stuff.class, c.getDeclaringClass());
assertFalse(it.hasNext());
}
Expand Down
10 changes: 3 additions & 7 deletions src/test/java/org/jvnet/hudson/annotation_indexer/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Properties;
import java.util.TreeMap;
import javax.annotation.processing.SupportedSourceVersion;
import javax.tools.Diagnostic;
import javax.tools.FileObject;
import javax.tools.JavaFileManager;
Expand All @@ -23,12 +19,12 @@ class Utils {

// Filter out warnings about source 1.6 is obsolete in java 9
// This usually appears with other warnings
public static final List<String> IGNORE = Arrays.asList(
public static final List<String> IGNORE = Collections.singletonList(
"RELEASE_6" // Filter out warnings about source 1.6 is obsolete in java 9+
);

public static List<Diagnostic<? extends JavaFileObject>> filterObsoleteSourceVersionWarnings(List<Diagnostic<? extends JavaFileObject>> diagnostics) {
List<Diagnostic<? extends JavaFileObject>> r = new ArrayList<Diagnostic<? extends JavaFileObject>>();
List<Diagnostic<? extends JavaFileObject>> r = new ArrayList<>();
for (Diagnostic<? extends JavaFileObject> d : diagnostics) {
if (!isIgnored(d.getMessage(Locale.ENGLISH))) {
r.add(d);
Expand Down

0 comments on commit 11de84a

Please sign in to comment.