Skip to content

Commit

Permalink
Pending #6, write out a *.txt side file with a summary, and make the …
Browse files Browse the repository at this point in the history
…JAR executable to dump binary contents readably.
  • Loading branch information
jglick committed Aug 29, 2016
1 parent b283462 commit adf2093
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
11 changes: 11 additions & 0 deletions sezpoz/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,17 @@
<forkMode>pertest</forkMode>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>net.java.sezpoz.impl.Inspector</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<reporting>
Expand Down
3 changes: 2 additions & 1 deletion sezpoz/src/main/java/net/java/sezpoz/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.java.sezpoz.impl.Indexer6;
import net.java.sezpoz.impl.SerAnnotatedElement;

/**
Expand Down Expand Up @@ -139,7 +140,7 @@ private void peek() throws IndexError {
}
if (ois == null) {
if (resources == null) {
resources = loader.getResources("META-INF/annotations/" + annotation.getName());
resources = loader.getResources(Indexer6.METAINF_ANNOTATIONS + annotation.getName());
}
if (!resources.hasMoreElements()) {
// Exhausted all streams.
Expand Down
21 changes: 18 additions & 3 deletions sezpoz/src/main/java/net/java/sezpoz/impl/Indexer6.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Writer;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
Expand Down Expand Up @@ -77,6 +78,8 @@
@SupportedOptions("sezpoz.quiet")
public class Indexer6 extends AbstractProcessor {

public static final String METAINF_ANNOTATIONS = "META-INF/annotations/";

/** public for ServiceLoader */
public Indexer6() {}

Expand All @@ -98,7 +101,7 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}
}
// map from indexable annotation names, to actual uses
Map<String,Map<String,SerAnnotatedElement>> output = new HashMap<String,Map<String,SerAnnotatedElement>>();
Map<String,Map<String,SerAnnotatedElement>> output = new TreeMap<String,Map<String,SerAnnotatedElement>>();
Map<String,Collection<Element>> originatingElementsByAnn = new HashMap<String,Collection<Element>>();
scan(annotations, originatingElementsByAnn, roundEnv, output);
write(output, originatingElementsByAnn);
Expand Down Expand Up @@ -169,7 +172,7 @@ private void write(Map<String,Map<String,SerAnnotatedElement>> output, Map<Strin
try {
Map<String,SerAnnotatedElement> elements = outputEntry.getValue();
try {
FileObject in = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", "META-INF/annotations/" + annName);
FileObject in = processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", METAINF_ANNOTATIONS + annName);
// Read existing annotations, for incremental compilation.
InputStream is = in.openInputStream();
try {
Expand All @@ -195,7 +198,7 @@ private void write(Map<String,Map<String,SerAnnotatedElement>> output, Map<Strin
// OK, created for the first time
}
FileObject out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
"", "META-INF/annotations/" + annName,
"", METAINF_ANNOTATIONS + annName,
originatingElementsByAnn.get(annName).toArray(new Element[0]));
OutputStream os = out.openOutputStream();
try {
Expand All @@ -208,6 +211,18 @@ private void write(Map<String,Map<String,SerAnnotatedElement>> output, Map<Strin
} finally {
os.close();
}
out = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
"", METAINF_ANNOTATIONS + annName + ".txt",
originatingElementsByAnn.get(annName).toArray(new Element[0]));
Writer w = out.openWriter();
try {
for (SerAnnotatedElement el : elements.values()) {
w.write(el.toString());
w.write('\n');
}
} finally {
w.close();
}
} catch (IOException x) {
processingEnv.getMessager().printMessage(Kind.ERROR, x.toString());
}
Expand Down
79 changes: 79 additions & 0 deletions sezpoz/src/main/java/net/java/sezpoz/impl/Inspector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package net.java.sezpoz.impl;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/** CLI utility for inspecting binary SezPoz metadata. */
public class Inspector {

private static final byte[] ZIP_MAGIC = {0x50, 0x4b, 0x03, 0x04};
private static final byte[] SER_MAGIC = {(byte) 0xac, (byte) 0xed, 0x00, 0x05};

public static void main(String[] args) throws Exception {
if (args.length == 0) {
System.err.println("Usage: java -jar sezpoz.jar [ something.jar | some.serialized.Annotation ]+");
}
for (String arg : args) {
System.out.println("--- " + arg);
byte[] magic = new byte[4];
InputStream is = new FileInputStream(arg);
try {
is.read(magic, 0, 4);
} finally {
is.close();
}
if (Arrays.equals(magic, ZIP_MAGIC)) {
JarFile jf = new JarFile(arg, false);
try {
Enumeration<JarEntry> entries = jf.entries();
while (entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
String name = entry.getName();
if (name.startsWith(Indexer6.METAINF_ANNOTATIONS)) {
String annotation = name.substring(Indexer6.METAINF_ANNOTATIONS.length());
if (annotation.isEmpty() || annotation.endsWith(".txt")) {
continue;
}
System.out.println("# " + annotation);
is = jf.getInputStream(entry);
try {
dump(is);
} finally {
is.close();
}
}
}
} finally {
jf.close();
}
} else if (Arrays.equals(magic, SER_MAGIC)) {
is = new FileInputStream(arg);
try {
dump(is);
} finally {
is.close();
}
} else {
System.err.println("does not look like either a JAR file or a Java serialized file");
}
}
}

private static void dump(InputStream is) throws Exception {
ObjectInput oi = new ObjectInputStream(is);
while (true) {
SerAnnotatedElement el = (SerAnnotatedElement) oi.readObject();
if (el == null) {
break;
}
System.out.println(el);
}
}

}
3 changes: 3 additions & 0 deletions sezpoz/src/test/java/net/java/sezpoz/impl/TestUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ public static Map<String,List<String>> findMetadata(File dest) throws Exception
}
Map<String,List<String>> metadata = new HashMap<String,List<String>>();
for (String kid : dir.list()) {
if (kid.endsWith(".txt")) {
continue;
}
File f = new File(dir, kid);
InputStream is = new FileInputStream(f);
try {
Expand Down

0 comments on commit adf2093

Please sign in to comment.