Skip to content

Commit 6a9ce00

Browse files
dschohinerm
authored andcommitted
Move CombineAnnotations into the appropriate package
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
1 parent 0b61306 commit 6a9ce00

2 files changed

Lines changed: 187 additions & 107 deletions

File tree

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
/*
2+
* #%L
3+
* SciJava Common shared library for SciJava software.
4+
* %%
5+
* Copyright (C) 2009 - 2013 Board of Regents of the University of
6+
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
7+
* Institute of Molecular Cell Biology and Genetics.
8+
* %%
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions are met:
11+
*
12+
* 1. Redistributions of source code must retain the above copyright notice,
13+
* this list of conditions and the following disclaimer.
14+
* 2. Redistributions in binary form must reproduce the above copyright notice,
15+
* this list of conditions and the following disclaimer in the documentation
16+
* and/or other materials provided with the distribution.
17+
*
18+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
* POSSIBILITY OF SUCH DAMAGE.
29+
*
30+
* The views and conclusions contained in the software and documentation are
31+
* those of the authors and should not be interpreted as representing official
32+
* policies, either expressed or implied, of any organization.
33+
* #L%
34+
*/
35+
36+
package org.scijava.annotations;
37+
38+
import java.io.File;
39+
import java.io.FileOutputStream;
40+
import java.io.IOException;
41+
import java.io.InputStream;
42+
import java.io.OutputStream;
43+
import java.lang.annotation.Annotation;
44+
import java.net.URL;
45+
import java.util.Enumeration;
46+
import java.util.HashSet;
47+
import java.util.Set;
48+
49+
import org.scijava.util.FileUtils;
50+
51+
/**
52+
* Combines SezPoz annotations from all JAR files on the classpath.
53+
*
54+
* @author Curtis Rueden
55+
*/
56+
public class CombineAnnotations extends AbstractIndexWriter {
57+
58+
private static final String PREFIX = "META-INF/json/";
59+
private static final String LEGACY_PREFIX = "META-INF/annotations/";
60+
private final String OUTPUT_DIR;
61+
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";
73+
}
74+
75+
annotationFiles = getAnnotationFiles();
76+
}
77+
78+
/** Reads in annotations from all available resources and combines them. */
79+
public void combine() throws IOException, ClassNotFoundException {
80+
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
81+
82+
log("");
83+
log("Writing annotations to " + new File(OUTPUT_DIR).getAbsolutePath());
84+
85+
new File(OUTPUT_DIR, PREFIX).mkdirs();
86+
for (final String annotationFile : annotationFiles) {
87+
final String annotationName = annotationFile.substring(PREFIX.length());
88+
@SuppressWarnings("unchecked")
89+
final Class<? extends Annotation> annotation =
90+
(Class<? extends Annotation>) loader.loadClass(annotationName);
91+
for (IndexItem<? extends Annotation> item : Index.load(annotation, loader)) {
92+
add(adapt(item.annotation()), annotationName, item.className());
93+
}
94+
}
95+
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+
});
117+
}
118+
119+
/** Scans for annotations files in every resource on the classpath. */
120+
public Set<String> getAnnotationFiles() throws IOException {
121+
final HashSet<String> files = new HashSet<String>();
122+
123+
for (final String prefix : new String[] { PREFIX, LEGACY_PREFIX }) {
124+
final Enumeration<URL> directories = Thread.currentThread()
125+
.getContextClassLoader().getResources(prefix);
126+
while (directories.hasMoreElements()) {
127+
final URL url = directories.nextElement();
128+
for (final URL annotationIndexURL : FileUtils.listContents(url)) {
129+
String string = annotationIndexURL.toString();
130+
if (string.endsWith("/")) {
131+
continue;
132+
}
133+
final int length = string.length();
134+
add(files, PREFIX + string.substring(
135+
string.lastIndexOf('/', length - 1) + 1, length));
136+
}
137+
}
138+
}
139+
return files;
140+
}
141+
142+
public static void main(final String[] args) throws Exception {
143+
new CombineAnnotations(args.length > 0 ? args[0] : null).combine();
144+
}
145+
146+
// -- Helper methods --
147+
148+
private void add(final HashSet<String> set, final String item) {
149+
log("\t" + item);
150+
set.add(item);
151+
}
152+
153+
private void log(final String msg) {
154+
System.out.println(msg);
155+
}
156+
157+
}

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

Lines changed: 30 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -35,125 +35,48 @@
3535

3636
package org.scijava.util;
3737

38-
import java.io.File;
39-
import java.io.FileOutputStream;
4038
import java.io.IOException;
41-
import java.io.InputStream;
42-
import java.io.OutputStream;
43-
import java.lang.annotation.Annotation;
44-
import java.net.URL;
45-
import java.util.Enumeration;
46-
import java.util.HashSet;
4739
import java.util.Set;
4840

49-
import org.scijava.annotations.AbstractIndexWriter;
50-
import org.scijava.annotations.Index;
51-
import org.scijava.annotations.IndexItem;
52-
5341
/**
54-
* Combines SezPoz annotations from all JAR files on the classpath.
55-
*
56-
* @author Curtis Rueden
42+
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations} instead.
5743
*/
58-
public class CombineAnnotations extends AbstractIndexWriter {
59-
60-
private static final String PREFIX = "META-INF/json/";
61-
private static final String LEGACY_PREFIX = "META-INF/annotations/";
62-
private final String OUTPUT_DIR;
63-
64-
private final Set<String> annotationFiles;
65-
66-
public CombineAnnotations() throws IOException {
67-
this(null);
68-
}
69-
70-
public CombineAnnotations(final String outputDir) throws IOException {
71-
if (outputDir != null) {
72-
OUTPUT_DIR = outputDir;
73-
} else {
74-
OUTPUT_DIR = "src/main/assembly/all";
75-
}
76-
77-
annotationFiles = getAnnotationFiles();
78-
}
79-
80-
/** Reads in annotations from all available resources and combines them. */
44+
@Deprecated
45+
public class CombineAnnotations extends
46+
org.scijava.annotations.CombineAnnotations
47+
{
48+
49+
/**
50+
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations#CombineAnnotations()} instead.
51+
*/
52+
@Deprecated
53+
public CombineAnnotations() throws IOException {}
54+
55+
/**
56+
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations#combine()} instead.
57+
*/
58+
@Deprecated
59+
@Override
8160
public void combine() throws IOException, ClassNotFoundException {
82-
final ClassLoader loader = Thread.currentThread().getContextClassLoader();
83-
84-
log("");
85-
log("Writing annotations to " + new File(OUTPUT_DIR).getAbsolutePath());
86-
87-
new File(OUTPUT_DIR, PREFIX).mkdirs();
88-
for (final String annotationFile : annotationFiles) {
89-
final String annotationName = annotationFile.substring(PREFIX.length());
90-
@SuppressWarnings("unchecked")
91-
final Class<? extends Annotation> annotation =
92-
(Class<? extends Annotation>) loader.loadClass(annotationName);
93-
for (IndexItem<? extends Annotation> item : Index.load(annotation, loader)) {
94-
add(adapt(item.annotation()), annotationName, item.className());
95-
}
96-
}
97-
98-
write(new StreamFactory() {
99-
100-
@Override
101-
public InputStream openInput(String annotationName)
102-
throws IOException {
103-
return null;
104-
}
105-
106-
@Override
107-
public OutputStream openOutput(String annotationName)
108-
throws IOException {
109-
final File file = new File(OUTPUT_DIR, PREFIX + annotationName);
110-
return new FileOutputStream(file);
111-
}
112-
113-
@Override
114-
public boolean isClassObsolete(String className) {
115-
return false;
116-
}
117-
118-
});
61+
super.combine();
11962
}
12063

121-
/** Scans for annotations files in every resource on the classpath. */
64+
/**
65+
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations#getAnnotationFiles()} instead.
66+
*/
67+
@Deprecated
68+
@Override
12269
public Set<String> getAnnotationFiles() throws IOException {
123-
final HashSet<String> files = new HashSet<String>();
124-
125-
for (final String prefix : new String[] { PREFIX, LEGACY_PREFIX }) {
126-
final Enumeration<URL> directories = Thread.currentThread()
127-
.getContextClassLoader().getResources(prefix);
128-
while (directories.hasMoreElements()) {
129-
final URL url = directories.nextElement();
130-
for (final URL annotationIndexURL : FileUtils.listContents(url)) {
131-
String string = annotationIndexURL.toString();
132-
if (string.endsWith("/")) {
133-
continue;
134-
}
135-
final int length = string.length();
136-
add(files, PREFIX + string.substring(
137-
string.lastIndexOf('/', length - 1) + 1, length));
138-
}
139-
}
140-
}
141-
return files;
70+
return super.getAnnotationFiles();
14271
}
14372

73+
/**
74+
* @deprecated Use {@link org.scijava.annotations.CombineAnnotations#main(String[])} instead.
75+
*/
76+
@Deprecated
14477
public static void main(final String[] args) throws Exception {
145-
new CombineAnnotations(args.length > 0 ? args[0] : null).combine();
146-
}
147-
148-
// -- Helper methods --
149-
150-
private void add(final HashSet<String> set, final String item) {
151-
log("\t" + item);
152-
set.add(item);
153-
}
154-
155-
private void log(final String msg) {
156-
System.out.println(msg);
78+
new org.scijava.annotations.CombineAnnotations(args.length > 0 ? args[0]
79+
: null).combine();
15780
}
15881

15982
}

0 commit comments

Comments
 (0)