Skip to content

Commit eb01de0

Browse files
committed
Refactor CombineAnnotations to AnnotationCombiner
The org.scijava.annotations CombineAnnotations class now implements the Combiner interface. It was refactored to remove state, allowing its combine method to now take any output directory (instead of requiring constructing a new instance for each output dir). The class was renamed to AnnotationCombiner to fit the naming scheme of MetaInfCombiner and ServiceCombiner. The org.scijava.util.CombineAnnotations class was updated to use the refactored AnnotationCombiner API.
1 parent 02627eb commit eb01de0

2 files changed

Lines changed: 82 additions & 63 deletions

File tree

src/main/java/org/scijava/annotations/CombineAnnotations.java renamed to src/main/java/org/scijava/annotations/AnnotationCombiner.java

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -46,83 +46,56 @@
4646
import java.util.HashSet;
4747
import java.util.Set;
4848

49+
import org.scijava.util.Combiner;
4950
import org.scijava.util.FileUtils;
5051

5152
/**
5253
* Combines SezPoz annotations from all JAR files on the classpath.
5354
*
5455
* @author Curtis Rueden
5556
*/
56-
public class CombineAnnotations extends AbstractIndexWriter {
57+
public class AnnotationCombiner extends AbstractIndexWriter implements Combiner
58+
{
5759

5860
private static final String PREFIX = "META-INF/json/";
5961
private static final String LEGACY_PREFIX = "META-INF/annotations/";
60-
private final String OUTPUT_DIR;
6162

62-
private final Set<String> annotationFiles;
63-
64-
public CombineAnnotations() throws IOException {
65-
this(null);
66-
}
67-
68-
public CombineAnnotations(final String outputDir) throws IOException {
69-
if (outputDir != null) {
70-
OUTPUT_DIR = outputDir;
71-
} else {
72-
OUTPUT_DIR = "src/main/assembly/all";
63+
/** Reads in annotations from all available resources and combines them. */
64+
@Override
65+
public void combine(File outputDirectory) throws Exception {
66+
if (outputDirectory == null) {
67+
outputDirectory = new File("src/main/assembly/all");
7368
}
69+
final Set<String> annotationFiles = getAnnotationFiles();
7470

75-
annotationFiles = getAnnotationFiles();
76-
}
77-
78-
/** Reads in annotations from all available resources and combines them. */
79-
public void combine() throws IOException, ClassNotFoundException {
8071
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
8172

8273
log("");
83-
log("Writing annotations to " + new File(OUTPUT_DIR).getAbsolutePath());
74+
log("Writing annotations to " + outputDirectory.getAbsolutePath());
8475

85-
new File(OUTPUT_DIR, PREFIX).mkdirs();
76+
new File(outputDirectory, PREFIX).mkdirs();
8677
for (final String annotationFile : annotationFiles) {
8778
final String annotationName = annotationFile.substring(PREFIX.length());
8879
@SuppressWarnings("unchecked")
8980
final Class<? extends Annotation> annotation =
9081
(Class<? extends Annotation>) loader.loadClass(annotationName);
91-
for (IndexItem<? extends Annotation> item : Index.load(annotation, loader)) {
82+
for (IndexItem<? extends Annotation> item : Index
83+
.load(annotation, loader))
84+
{
9285
add(adapt(item.annotation()), annotationName, item.className());
9386
}
9487
}
9588

96-
write(new StreamFactory() {
97-
98-
@Override
99-
public InputStream openInput(String annotationName)
100-
throws IOException {
101-
return null;
102-
}
103-
104-
@Override
105-
public OutputStream openOutput(String annotationName)
106-
throws IOException {
107-
final File file = new File(OUTPUT_DIR, PREFIX + annotationName);
108-
return new FileOutputStream(file);
109-
}
110-
111-
@Override
112-
public boolean isClassObsolete(String className) {
113-
return false;
114-
}
115-
116-
});
89+
write(new AnnotationStreamFactory(outputDirectory));
11790
}
11891

11992
/** Scans for annotations files in every resource on the classpath. */
12093
public Set<String> getAnnotationFiles() throws IOException {
12194
final HashSet<String> files = new HashSet<String>();
12295

12396
for (final String prefix : new String[] { PREFIX, LEGACY_PREFIX }) {
124-
final Enumeration<URL> directories = Thread.currentThread()
125-
.getContextClassLoader().getResources(prefix);
97+
final Enumeration<URL> directories =
98+
Thread.currentThread().getContextClassLoader().getResources(prefix);
12699
while (directories.hasMoreElements()) {
127100
final URL url = directories.nextElement();
128101
for (final URL annotationIndexURL : FileUtils.listContents(url)) {
@@ -131,18 +104,14 @@ public Set<String> getAnnotationFiles() throws IOException {
131104
continue;
132105
}
133106
final int length = string.length();
134-
add(files, PREFIX + string.substring(
135-
string.lastIndexOf('/', length - 1) + 1, length));
107+
add(files, PREFIX +
108+
string.substring(string.lastIndexOf('/', length - 1) + 1, length));
136109
}
137110
}
138111
}
139112
return files;
140113
}
141114

142-
public static void main(final String[] args) throws Exception {
143-
new CombineAnnotations(args.length > 0 ? args[0] : null).combine();
144-
}
145-
146115
// -- Helper methods --
147116

148117
private void add(final HashSet<String> set, final String item) {
@@ -154,4 +123,41 @@ private void log(final String msg) {
154123
System.out.println(msg);
155124
}
156125

126+
// -- Helper Class --
127+
128+
/**
129+
* {@link StreamFactory} implementation for writing an annotation.
130+
*
131+
*/
132+
private static class AnnotationStreamFactory implements StreamFactory {
133+
134+
private final File outputDirectory;
135+
136+
public AnnotationStreamFactory(final File outputDirectory) {
137+
this.outputDirectory = outputDirectory;
138+
}
139+
140+
@Override
141+
public InputStream openInput(String annotationName) throws IOException {
142+
return null;
143+
}
144+
145+
@Override
146+
public OutputStream openOutput(String annotationName) throws IOException {
147+
final File file = new File(outputDirectory, PREFIX + annotationName);
148+
return new FileOutputStream(file);
149+
}
150+
151+
@Override
152+
public boolean isClassObsolete(String className) {
153+
return false;
154+
}
155+
156+
}
157+
158+
// -- Main method --
159+
160+
public static void main(final String[] args) throws Exception {
161+
new AnnotationCombiner().combine(args.length > 0 ? new File(args[0]) : null);
162+
}
157163
}

src/main/java/org/scijava/util/CombineAnnotations.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,48 +35,61 @@
3535

3636
package org.scijava.util;
3737

38+
import java.io.File;
3839
import java.io.IOException;
3940
import java.util.Set;
4041

42+
import org.scijava.annotations.AnnotationCombiner;
43+
4144
/**
42-
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations} instead.
45+
* @deprecated Use {@link org.scijava.annotations.AnnotationCombiner} instead.
4346
*/
4447
@Deprecated
45-
public class CombineAnnotations extends
46-
org.scijava.annotations.CombineAnnotations
48+
public class CombineAnnotations
4749
{
4850

4951
/**
50-
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations#CombineAnnotations()} instead.
52+
* @deprecated Use {@link org.scijava.annotations.AnnotationCombiner#CombineAnnotations()} instead.
5153
*/
5254
@Deprecated
5355
public CombineAnnotations() throws IOException {}
5456

57+
private AnnotationCombiner combiner = new AnnotationCombiner();
5558
/**
56-
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations#combine()} instead.
59+
* @deprecated Use {@link org.scijava.annotations.AnnotationCombiner#combine()} instead.
5760
*/
5861
@Deprecated
59-
@Override
6062
public void combine() throws IOException, ClassNotFoundException {
61-
super.combine();
63+
try {
64+
combiner.combine(null);
65+
}
66+
catch (Exception e) {
67+
if (e instanceof IOException) {
68+
throw new IOException(e.getMessage());
69+
}
70+
if (e instanceof ClassNotFoundException) {
71+
throw new ClassNotFoundException(e.getMessage());
72+
}
73+
}
6274
}
6375

6476
/**
65-
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations#getAnnotationFiles()} instead.
77+
* @deprecated Use {@link org.scijava.annotations.AnnotationCombiner#getAnnotationFiles()} instead.
6678
*/
6779
@Deprecated
68-
@Override
6980
public Set<String> getAnnotationFiles() throws IOException {
70-
return super.getAnnotationFiles();
81+
return combiner.getAnnotationFiles();
7182
}
7283

7384
/**
74-
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations#main(String[])} instead.
85+
* @deprecated Use
86+
* {@link org.scijava.annotations.AnnotationCombiner#main(String[])}
87+
* instead.
7588
*/
7689
@Deprecated
7790
public static void main(final String[] args) throws Exception {
78-
new org.scijava.annotations.CombineAnnotations(args.length > 0 ? args[0]
79-
: null).combine();
91+
new org.scijava.annotations.AnnotationCombiner().combine(args.length > 0
92+
? new File(args[0]) : null);
8093
}
8194

8295
}

0 commit comments

Comments
 (0)