-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52b7e74
commit 354559d
Showing
6 changed files
with
314 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
.classpath | ||
.project | ||
.settings | ||
.idea | ||
jandex.iml | ||
target | ||
*~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2010, Red Hat, Inc., and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.jboss.jandex; | ||
|
||
import org.apache.tools.ant.BuildException; | ||
import org.apache.tools.ant.Task; | ||
import org.apache.tools.ant.types.FileSet; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Ant Task that indexes jars, and saves the resulting index | ||
* @author Stuart Douglas | ||
*/ | ||
public class JandexAntTask extends Task { | ||
|
||
private final List<FileSet> filesets = new ArrayList<FileSet>(); | ||
|
||
private boolean modify = false; | ||
|
||
private boolean newJar = false; | ||
|
||
private boolean verbose = false; | ||
|
||
private boolean run = true; | ||
|
||
@Override | ||
public void execute() throws BuildException { | ||
if(!run) { | ||
return; | ||
} | ||
if(modify && newJar) { | ||
throw new BuildException("Specifying both modify and newJar does not make sense."); | ||
} | ||
Indexer indexer = new Indexer(); | ||
for(FileSet fileset : filesets) { | ||
String[] files = fileset.getDirectoryScanner(getProject()).getIncludedFiles(); | ||
for(String file : files) { | ||
if(file.endsWith(".jar")) { | ||
try { | ||
JarIndexer.createJarIndex(new File(fileset.getDir().getAbsolutePath() + "/" +file), indexer, modify, newJar,verbose); | ||
} catch (IOException e) { | ||
throw new BuildException(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
} | ||
|
||
public void addFileset(FileSet fileset) { | ||
filesets.add(fileset); | ||
} | ||
|
||
public boolean isModify() { | ||
return modify; | ||
} | ||
|
||
public void setModify(boolean modify) { | ||
this.modify = modify; | ||
} | ||
|
||
public boolean isVerbose() { | ||
return verbose; | ||
} | ||
|
||
public void setVerbose(boolean verbose) { | ||
this.verbose = verbose; | ||
} | ||
|
||
public boolean isRun() { | ||
return run; | ||
} | ||
|
||
public void setRun(boolean run) { | ||
this.run = run; | ||
} | ||
|
||
public boolean isNewJar() { | ||
return newJar; | ||
} | ||
|
||
public void setNewJar(boolean newJar) { | ||
this.newJar = newJar; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package org.jboss.jandex; | ||
|
||
import java.io.*; | ||
import java.util.Enumeration; | ||
import java.util.jar.JarEntry; | ||
import java.util.jar.JarFile; | ||
import java.util.zip.ZipEntry; | ||
import java.util.zip.ZipOutputStream; | ||
|
||
/** | ||
* | ||
* Class which contains utility methods to create an index for a jar file | ||
* | ||
* @author Stuart Douglas | ||
* | ||
*/ | ||
public class JarIndexer { | ||
|
||
/** | ||
* Indexes a jar file and saves the result. If the modify flag is try it is saved META-INF/jandex.idx. | ||
* Otherwies an external file is created with a similar name to the original file, however the | ||
* <code>.jar</code> extension is replaced with <code>-jar.idx</code> | ||
* | ||
* @param jarFile The file to index | ||
* @param indexer The indexer to use | ||
* @param modify If the original jar should be modified | ||
* @param verbose If we should print what we are doing to standard out | ||
*/ | ||
public static Result createJarIndex(File jarFile, Indexer indexer, boolean modify, boolean newJar, boolean verbose) throws IOException { | ||
File tmpCopy = null; | ||
ZipOutputStream zo = null; | ||
OutputStream out = null; | ||
File outputFile = null; | ||
|
||
JarFile jar = new JarFile(jarFile); | ||
|
||
if (modify) { | ||
tmpCopy = File.createTempFile(jarFile.getName().substring(0, jarFile.getName().lastIndexOf('.')), "jmp"); | ||
out = zo = new ZipOutputStream(new FileOutputStream(tmpCopy)); | ||
} else if (newJar) { | ||
outputFile = new File(jarFile.getAbsolutePath().replace(".jar", "-jandex.jar")); | ||
out = zo = new ZipOutputStream(new FileOutputStream(outputFile)); | ||
} else | ||
{ | ||
outputFile = new File(jarFile.getAbsolutePath().replace(".jar", "-jar") + ".idx"); | ||
out = new FileOutputStream( outputFile); | ||
} | ||
|
||
try { | ||
Enumeration<JarEntry> entries = jar.entries(); | ||
while (entries.hasMoreElements()) { | ||
JarEntry entry = entries.nextElement(); | ||
if (modify) { | ||
zo.putNextEntry(entry); | ||
copy(jar.getInputStream(entry), zo); | ||
} | ||
|
||
if (entry.getName().endsWith(".class")) { | ||
ClassInfo info = indexer.index(jar.getInputStream(entry)); | ||
if (verbose && info != null) | ||
printIndexEntryInfo(info); | ||
} | ||
} | ||
|
||
if (modify || newJar) { | ||
zo.putNextEntry(new ZipEntry("META-INF/jandex.idx")); | ||
} | ||
|
||
IndexWriter writer = new IndexWriter(out); | ||
Index index = indexer.complete(); | ||
int bytes = writer.write(index); | ||
|
||
|
||
if (modify) { | ||
jarFile.delete(); | ||
tmpCopy.renameTo(jarFile); | ||
tmpCopy = null; | ||
} | ||
return new Result(index, modify ? "META-INF/jandex.idx" : outputFile.getPath(), bytes); | ||
} finally { | ||
out.flush(); | ||
out.close(); | ||
if (tmpCopy != null) | ||
tmpCopy.delete(); | ||
} | ||
} | ||
|
||
private static void printIndexEntryInfo(ClassInfo info) { | ||
System.out.println("Indexed " + info.name() + " (" + info.annotations().size() + " annotations)"); | ||
} | ||
private static void copy(InputStream in, OutputStream out) throws IOException { | ||
byte[] buf = new byte[8192]; | ||
int len; | ||
while ((len = in.read(buf)) > 0) { | ||
out.write(buf, 0, len); | ||
} | ||
out.flush(); | ||
} | ||
private JarIndexer() { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.