Skip to content

Commit

Permalink
merge patch from Heiko Braun
Browse files Browse the repository at this point in the history
  • Loading branch information
bsorrentino committed Apr 3, 2013
1 parent e8301f1 commit c7f9172
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 20 deletions.
9 changes: 3 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
<<<<<<< HEAD
.classpath
.project
.settings/
branch.sh
release.sh
sign-and-deploy.sh

=======
/target/
>>>>>>> bf958dd87dd0dcd79c32815f3cdbafbfc8f350a5
/issue47.txt
target/
issue47.txt
pom.xml.versionsBackup
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<groupId>org.bsc.maven</groupId>
<artifactId>maven-processor-plugin</artifactId>
<packaging>maven-plugin</packaging>
<version>2.1.2-SNAPSHOT</version>
<version>2.2.0-SNAPSHOT</version>
<name>maven-processor-plugin - ${project.version}</name>
<description>A maven plugin to process annotation for jdk6 at compile time

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import javax.tools.JavaCompiler.CompilationTask;
import javax.tools.*;
import org.apache.maven.artifact.Artifact;
Expand All @@ -48,6 +50,13 @@
public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo
{

interface ArtifactClosure {

void execute( Artifact artifact );
}

private static final String SOURCE_CLASSIFIER = "sources";

/**
*
*/
Expand Down Expand Up @@ -154,7 +163,14 @@ public abstract class AbstractAnnotationProcessorMojo extends AbstractMojo
private boolean addCompileSourceRoots = false;



/**
* append source artifacts to sources list
*
* @since 2.2.0
*/
@Parameter( defaultValue = "false")
private boolean appendSourceArtifacts = false;

/**
* for execution synchronization
*/
Expand Down Expand Up @@ -313,12 +329,13 @@ private void executeWithExceptionsHandled() throws Exception

for( File sourceDir : sourceDirs ) {

getLog().debug( String.format( "processing source directory [%s]", sourceDir.getPath()) );

if( sourceDir==null ) {
getLog().warn( "source directory is null! Processor task will be skipped!" );
continue;
}

getLog().debug( String.format( "processing source directory [%s]", sourceDir.getPath()) );

if( !sourceDir.exists() ) {
getLog().warn( String.format("source directory [%s] doesn't exist! Processor task will be skipped!", sourceDir.getPath()));
continue;
Expand All @@ -331,8 +348,7 @@ private void executeWithExceptionsHandled() throws Exception

files.addAll( FileUtils.getFiles(sourceDir, includesString, excludesString) );
}

Iterable< ? extends JavaFileObject> compilationUnits1 = null;


String compileClassPath = buildCompileClasspath();

Expand Down Expand Up @@ -404,35 +420,80 @@ public void report(Diagnostic< ? extends JavaFileObject> diagnostic)
}

}

//
// add to allSource the files coming out from source archives
//
final List<JavaFileObject> allSources = new java.util.ArrayList<JavaFileObject>();

processSourceArtifacts( new ArtifactClosure() {

public void execute(Artifact artifact) {
try {

java.io.File f = artifact.getFile();

ZipFile zipFile = new ZipFile(f);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
int sourceCount = 0;

while (entries.hasMoreElements()) {
ZipEntry entry = (ZipEntry) entries.nextElement();

if (entry.getName().endsWith(".java")) {
++sourceCount;
allSources.add(ZipFileObject.create(zipFile, entry));

}
}

getLog().debug(String.format("** Discovered %d java sources in %s", sourceCount, f.getAbsolutePath()));

} catch (Exception ex) {
getLog().warn(String.format("Problem reading source archive [%s]", artifact.getFile().getPath()));
}
}
});

//compileLock.lock();
try {
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
final JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();

if( compiler==null ) {
getLog().error("JVM is not suitable for processing annotation! ToolProvider.getSystemJavaCompiler() is null.");
return;
}

StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
final StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

if( files!=null && !files.isEmpty() ) {
compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files);


for( JavaFileObject f : fileManager.getJavaFileObjectsFromFiles(files) ) {

allSources.add(f);
};



}
else {



if( allSources.isEmpty() ) {
getLog().warn( "no source file(s) detected! Processor task will be skipped");
return;
}


final Iterable<String> classes = null;

CompilationTask task = compiler.getTask(
new PrintWriter(System.out),
fileManager,
dl,
options,
null,
compilationUnits1);
classes,
allSources);

/*
* //Create a list to hold annotation processors LinkedList<Processor> processors = new
Expand Down Expand Up @@ -527,4 +588,18 @@ private void ensureOutputDirectoryExists()
}


private void processSourceArtifacts( ArtifactClosure closure ) {
if( ! appendSourceArtifacts ) {
return;
}
for (Artifact dep : this.project.getDependencyArtifacts()) {
if ((dep.hasClassifier()) && (dep.getClassifier().equals(SOURCE_CLASSIFIER))) {

closure.execute(dep);
//getLog().debug("Append source artifact to classpath: " + dep.getGroupId() + ":" + dep.getArtifactId());
//this.sourceArtifacts.add(dep.getFile());
}
}
}

}
82 changes: 82 additions & 0 deletions src/main/java/org/bsc/maven/plugin/processor/ZipFileObject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package org.bsc.maven.plugin.processor;

import javax.tools.JavaFileObject;
import javax.tools.SimpleJavaFileObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
*
* @author Heiko Braun
*/
public class ZipFileObject extends SimpleJavaFileObject
{
private ZipEntry zipEntry;
private ZipFile zipFile;

public ZipFileObject(ZipFile zipFile, ZipEntry zipEntry, URI uri)
{
super(uri, JavaFileObject.Kind.SOURCE);
this.zipEntry = zipEntry;
this.zipFile = zipFile;
}

@Override
public InputStream openInputStream() throws IOException
{
return this.zipFile.getInputStream(this.zipEntry);
}

@Override
public String getName()
{
return this.zipEntry.getName();
}

@Override
public CharSequence getCharContent(boolean b)
throws IOException
{
InputStreamReader is = new InputStreamReader(openInputStream());
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(is);
String read = br.readLine();

while (read != null) {
sb.append(read).append("\n");
read = br.readLine();
}

return sb.toString();
}

@Override
public Reader openReader(boolean b) throws IOException
{
return new BufferedReader(new InputStreamReader(openInputStream()));
}

@Override
public long getLastModified()
{
return this.zipEntry.getTime();
}

public static ZipFileObject create(ZipFile zipFile, ZipEntry entry) {
try {
final String uri = String.format("jar://%s!%s", zipFile.getName(), entry.getName() );
return new ZipFileObject(zipFile, entry, new URI(uri));
}
catch (URISyntaxException e)
{
throw new RuntimeException("Invalid zip entry:" + e.getMessage());
}
}
}

0 comments on commit c7f9172

Please sign in to comment.