Skip to content

Commit

Permalink
Add information in the WatchingException to store the file, line, cha…
Browse files Browse the repository at this point in the history
…racter and reason of the processing issue (#26).

Also add the support of the Java Compiler.

Signed-off-by: Clement Escoffier <[email protected]>
  • Loading branch information
cescoffier committed May 31, 2014
1 parent ddda1c3 commit 85c2e2e
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ public class WatchingException extends Exception {

private final File file;

private final int line;
private final int character;

/**
* Creates a Watching Exception.
*
Expand Down Expand Up @@ -55,8 +58,14 @@ public WatchingException(String message, Throwable cause) {
* @param cause the cause of the error, if known.
*/
public WatchingException(String message, File file, Throwable cause) {
this(message, file, -1, -1, cause);
}

public WatchingException(String message, File file, int line, int character, Throwable cause) {
super(message, cause);
this.file = file;
this.line = line;
this.character = character;
}

/**
Expand All @@ -68,4 +77,23 @@ public File getFile() {
return file;
}

/**
* Gets the line having triggered the issue. It's the line from the file returned by {@link #getFile()}.
*
* @return the line number, {@value -1} is there are no line specified.
*/
public int getLine() {
return line;
}

/**
* Gets the character / row having triggered the issue. It's the character from the line returned by {@link
* #getLine()}, from the {@link #getFile()} file.
*
* @return the character number, {@value -1} is there are no character specified.
*/
public int getCharacter() {
return character;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,37 +46,77 @@ public class JavaCompilerMojo extends AbstractWisdomWatcherMojo implements Const
private File classes;
private CompilerExecutor compiler = new CompilerExecutor();

/**
* Compiles the Java sources.
*
* @throws MojoExecutionException thrown on compilation error.
*/
@Override
public void execute() throws MojoExecutionException {
classes = new File(buildDirectory, "classes");
compiler.execute(this);
}

/**
* Checks whether or not an event on the given file should trigger the Java compilation.
*
* @param file the file
* @return {@literal true} if the given file is a JAva file and is contained in the Java source directory.
*/
@Override
public boolean accept(File file) {
return WatcherUtils.isInDirectory(file, WatcherUtils.getJavaSource(basedir));
}

/**
* A new (accepted) file was created. This methods triggers the Java compilation.
*
* @param file the file
* @return {@literal true}
* @throws WatchingException thrown on compilation error. The thrown exception contains the file, line,
* character and reason of the compilation error.
*/
@Override
public boolean fileCreated(File file) throws WatchingException {
compile();
return true;
}

private void compile() throws WatchingException {
try {
execute();
} catch (MojoExecutionException e) {
if (e.getCause() != null
&& e.getCause().getClass().getName().equals("org.apache.maven.plugin.compiler" +
".CompilationFailureException")) {
throw CompilerExecutor.build(this, e.getCause());
}
throw new WatchingException("Compilation error", e);
}
return true;
}

/**
* A new (accepted) file was updated. This methods triggers the Java compilation.
*
* @param file the file
* @return {@literal true}
* @throws WatchingException thrown on compilation error. The thrown exception contains the file, line,
* character and reason of the compilation error.
*/
@Override
public boolean fileUpdated(File file) throws WatchingException {
try {
execute();
} catch (MojoExecutionException e) {
throw new WatchingException("Compilation error", e);
}
compile();
return true;
}

/**
* A new (accepted) file was deleted. This methods triggers the Java compilation.
*
* @param file the file
* @return {@literal true}
* @throws WatchingException thrown on compilation error. The thrown exception contains the file, line,
* character and reason of the compilation error.
*/
@Override
public boolean fileDeleted(final File file) throws WatchingException {
// Delete the associated class file.
Expand All @@ -96,14 +136,10 @@ public boolean accept(File dir, String name) {
}, TrueFileFilter.INSTANCE);

for (File clazz : files) {
clazz.delete();
getLog().debug("Deleting " + clazz.getAbsolutePath() + " : " + clazz.delete());
}

try {
execute();
} catch (MojoExecutionException e) {
throw new WatchingException("Compilation error", e);
}
compile();
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@

import org.apache.maven.plugin.MojoExecutionException;
import org.codehaus.plexus.util.xml.Xpp3Dom;
import org.wisdom.maven.WatchingException;
import org.wisdom.maven.mojos.AbstractWisdomMojo;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.twdata.maven.mojoexecutor.MojoExecutor.*;

Expand Down Expand Up @@ -96,4 +100,25 @@ public static String getLongMessage(AbstractWisdomMojo mojo, Object exception) {
}
return null;
}

public static Pattern JAVA_COMPILATION_ERROR = Pattern.compile("(.*):\\[(.*),(.*)\\](.*)");

public static WatchingException build(AbstractWisdomMojo mojo, Throwable exception) {
String message = getLongMessage(mojo, exception);
if (message.contains("\n")) {
message = message.substring(0, message.indexOf("\n")).trim();
}

final Matcher matcher = JAVA_COMPILATION_ERROR.matcher(message);
if (matcher.matches()) {
String path = matcher.group(1);
String line = matcher.group(2);
String character = matcher.group(3);
String reason = matcher.group(4);
File file = new File(path);
return new WatchingException(reason, file, Integer.valueOf(line), Integer.valueOf(character), null);
} else {
return new WatchingException("Java Compilation Error", exception);
}
}
}

0 comments on commit 85c2e2e

Please sign in to comment.