2121 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2222 * POSSIBILITY OF SUCH DAMAGE.
2323 */
24-
25- /*
26- * JavaCompiler.java
27- * @author A. Sundararajan
28- */
29-
3024package com .l2jserver .script .java ;
3125
3226import java .io .IOException ;
3933
4034import javax .tools .Diagnostic ;
4135import javax .tools .DiagnosticCollector ;
36+ import javax .tools .JavaCompiler .CompilationTask ;
4237import javax .tools .JavaFileObject ;
43- import javax .tools .StandardJavaFileManager ;
4438
4539import org .eclipse .jdt .internal .compiler .tool .EclipseCompiler ;
4640
4741/**
4842 * Simple interface to Java compiler using JSR 199 Compiler API.
43+ * @author A. Sundararajan
4944 */
50- public class JavaCompiler {
51-
52- private javax .tools .JavaCompiler tool ;
53- private StandardJavaFileManager stdManager ;
54-
55- public JavaCompiler () {
45+ public class JavaCompiler
46+ {
47+
48+ private final javax .tools .JavaCompiler tool ;
49+
50+ public JavaCompiler ()
51+ {
5652 tool = new EclipseCompiler ();
57- stdManager = tool .getStandardFileManager (null , null , null );
5853 }
59-
60- public Map <String , byte []> compile (String source , String fileName ) {
54+
55+ public Map <String , byte []> compile (String source , String fileName )
56+ {
6157 PrintWriter err = new PrintWriter (System .err );
6258 return compile (source , fileName , err , null , null );
6359 }
64-
65- public Map <String , byte []> compile (String fileName , String source , Writer err ) {
60+
61+ public Map <String , byte []> compile (String fileName , String source , Writer err )
62+ {
6663 return compile (fileName , source , err , null , null );
6764 }
68-
69- public Map <String , byte []> compile (String fileName , String source , Writer err , String sourcePath ) {
65+
66+ public Map <String , byte []> compile (String fileName , String source , Writer err , String sourcePath )
67+ {
7068 return compile (fileName , source , err , sourcePath , null );
7169 }
72-
70+
7371 /**
7472 * compile given String source and return bytecodes as a Map.
75- *
7673 * @param fileName source fileName to be used for error messages etc.
7774 * @param source Java source as String
7875 * @param err error writer where diagnostic messages are written
7976 * @param sourcePath location of additional .java source files
8077 * @param classPath location of additional .class files
8178 */
82- public Map <String , byte []> compile (String fileName , String source , Writer err , String sourcePath , String classPath ) {
79+ public Map <String , byte []> compile (String fileName , String source , Writer err , String sourcePath , String classPath )
80+ {
8381 // to collect errors, warnings etc.
8482 DiagnosticCollector <JavaFileObject > diagnostics = new DiagnosticCollector <JavaFileObject >();
85-
83+
8684 // create a new memory JavaFileManager
8785 MemoryJavaFileManager manager = new MemoryJavaFileManager ();
88-
86+
8987 // prepare the compilation unit
9088 List <JavaFileObject > compUnits = new ArrayList <JavaFileObject >(1 );
9189 compUnits .add (MemoryJavaFileManager .makeStringSource (fileName , source ));
92-
90+
9391 // javac options
9492 List <String > options = new ArrayList <String >();
9593 options .add ("-Xlint:all" );
9694 options .add ("-g" );
9795 options .add ("-deprecation" );
98- options .add ("-1.6" );
99- if (sourcePath != null ) {
96+ options .add ("-1.7" );
97+ if (sourcePath != null )
98+ {
10099 options .add ("-sourcepath" );
101100 options .add (sourcePath );
102101 }
103- if (classPath != null ) {
102+ if (classPath != null )
103+ {
104104 options .add ("-classpath" );
105105 options .add (classPath );
106106 }
107-
107+
108108 // create a compilation task
109- javax .tools .JavaCompiler .CompilationTask task = tool .getTask (err , manager , diagnostics , options , null , compUnits );
110-
111- if (task .call () == false ) {
109+ CompilationTask task = tool .getTask (err , manager , diagnostics , options , null , compUnits );
110+
111+ if (task .call () == false )
112+ {
112113 PrintWriter perr = new PrintWriter (err );
113- for (Diagnostic diagnostic : diagnostics .getDiagnostics ()) {
114+ for (Diagnostic <?> diagnostic : diagnostics .getDiagnostics ())
115+ {
114116 perr .println (diagnostic .getMessage (Locale .getDefault ()));
115117 }
116118 perr .flush ();
117119 return null ;
118120 }
119-
121+
120122 Map <String , byte []> classBytes = manager .getClassBytes ();
121- try {
123+ try
124+ {
122125 manager .close ();
123126 }
124- catch (IOException exp ) {
127+ catch (IOException exp )
128+ {
129+ //
125130 }
126-
127131 return classBytes ;
128132 }
129- }
133+ }
0 commit comments