Skip to content

Instantly share code, notes, and snippets.

@florentdupont
Created October 2, 2013 20:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save florentdupont/6799971 to your computer and use it in GitHub Desktop.
Save florentdupont/6799971 to your computer and use it in GitHub Desktop.
Classe JavaSyntax complète de l'article "Valider la syntaxe d'un source Java avec Eclipse JDT"
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.jdt.core.compiler.IProblem;
import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.ASTParser;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.util.Util;
/**
* Classe utilitaire de validation de syntaxe.
* permet de valider la syntaxe d'un fichier Java
* @author fdupont
*
*/
public class JavaSyntax {
public static void main(String[] args) {
String source = "package flo.test;\n" +
"import 1flo.truc.*\n" +
"import java.util.Date;\n" +
"\n" +
" sdf \n" +
"public class Toto {\n" +
"int toto = 123_456;\n" +
"Bla ex = new Bla();\n" +
"Truc t ; \n" +
"Titi t2; \n"+
"private class Titi {}\n" +
"}\n";
System.out.println(source);
check(source, System.err);
}
public final static boolean check(String source) {
return check(source, System.out);
}
/**
* Valide la syntaxe pour le code source passé en paramètre.
* La chaine de caractère représente l'intégralité du code source d'un fichier.
* Les erreur de compilation sont
*
* @param source fichier source sous la forme d'une chaine de caractère
* @param out le flux de sortie
* @return true si la syntaxe est correcte, false sinon.
*/
public final static boolean check(String source, PrintStream out) {
char[] sourceArray = source.toCharArray();
// char[] sourceArray = Util.getFileCharContent(file, encoding);
// option de compilation : sources en version 1.7 dans notre cas.
// nécessite un JLS 4
Map<String, String> optionsMap = new HashMap<String, String>(2);
optionsMap.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_7);
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setCompilerOptions(optionsMap);
parser.setKind(ASTParser.K_COMPILATION_UNIT);
parser.setSource(sourceArray);
CompilationUnit unit = (CompilationUnit) parser.createAST(null);
IProblem[] pbs = unit.getProblems();
if(pbs.length > 0) {
displayProblems(pbs, unit, source, out);
return false;
} else {
return true;
}
}
private static void displayProblems(IProblem[] pbs, CompilationUnit unit, String source, PrintStream out) {
for(IProblem pb : pbs) {
displayError(pb, out);
displayLine(source, pb.getSourceLineNumber(), out);
displayEmphasize(unit.getColumnNumber(pb.getSourceStart()) , unit.getColumnNumber(pb.getSourceEnd()), out);
}
}
private static void displayError(IProblem pb, PrintStream out) {
out.append("line ");
out.append(String.valueOf(pb.getSourceLineNumber()));
out.append(" : ");
out.append(pb.getMessage());
out.append('\n');
}
/**
* pour start=3, stop=4, on aurait
* " --"
* 01234
*
* @param start index de début inclusif: commence à 0
* @param stop index de fin inclusif
* @return
*/
private static void displayEmphasize(int start, int stop, PrintStream out) {
int length = stop + 1;
out.append(" ");
for(int i=0; i < length; i++) {
if(i < start || i > stop) {
out.append(' ');
} else {
out.append('-');
}
}
out.append('\n');
}
private static void displayLine(String source, int line, PrintStream out) {
out.append(" ");
String[] strlines = source.split("\n");
out.append( strlines[line - 1 ]);
out.append('\n');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment