File: WsCompileExample.java

package info (click to toggle)
libnb-javaparser-java 7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 35,940 kB
  • ctags: 55,696
  • sloc: java: 264,137; xml: 2,781; sh: 1,135; makefile: 425
file content (189 lines) | stat: -rw-r--r-- 7,402 bytes parent folder | download | duplicates (24)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (c) 2007, 2008, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code 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 General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

import java.util.List;
import java.util.ArrayList;
import java.io.File;
//for CompilerHelper
import java.io.OutputStream;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;



public class WsCompileExample {
    File destDir;
    File srcDir;
    protected boolean compilerDebug = false;
    protected boolean compilerOptimize = false;
    protected String userClasspath = null;

    public static void main(String[] args) {
        new WsCompileExample().do_main(args);
    }

    public void do_main(String[] args) {
        if(!args[0].equals("-s")) {
            throw new RuntimeException("specify -s for src");
        }

        //run it once
        srcDir =  new File(args[1]);
        if(!args[2].equals("-d")) {
            throw new RuntimeException("specify -d for dest");
        }
        destDir =  new File(args[3]);
        if(!destDir.exists())
            destDir.mkdirs();
        System.out.println("----test compile 1-----");
        compileGeneratedClasses();

        //run it twice
         srcDir = new File(args[1]+"1");
         destDir =  new File(args[3]+"1");
        if(!destDir.exists())
            destDir.mkdirs();
        System.out.println("----test compile 2-----");
        compileGeneratedClasses();

    }
    protected void compileGeneratedClasses() {
        List sourceFiles = new ArrayList();

        for (File f: srcDir.listFiles()) {
            if (f.getName().endsWith(".java")) {
                sourceFiles.add(f.getAbsolutePath());
            }
        }

        if (sourceFiles.size() > 0) {
                                String classDir = destDir.getAbsolutePath();
            String classpathString = createClasspathString();
            System.out.println("classpathString: " + classpathString);

                                String[] args = new String[4 + (compilerDebug == true ? 1 : 0) +
                (compilerOptimize == true ? 1 : 0) +
                sourceFiles.size()];
            args[0] = "-d";
            args[1] = classDir;
            args[2] = "-classpath";
            args[3] = classpathString;
//                              args[4]="-DnonBatchMode";
            int baseIndex = 4;
            if (compilerDebug) {
                args[baseIndex++] = "-g";
            }
            if (compilerOptimize) {
                args[baseIndex++] = "-O";
            }
            for (int i = 0; i < sourceFiles.size(); ++i) {
                args[baseIndex + i] = (String)sourceFiles.get(i);
            }

            // ByteArrayOutputStream javacOutput = new ByteArrayOutputStream();
            JavaCompilerHelper compilerHelper = new JavaCompilerHelper(System.out);
            boolean result = compilerHelper.compile(args);
            if (!result) {
                System.out.println("wscompile.compilation Failed");
            }
        }
    }

    protected String createClasspathString() {
        if (userClasspath == null) {
            userClasspath = "";
        }
                          String jcp = userClasspath + File.pathSeparator + System.getProperty("java.class.path");
                  return jcp;
    }
}
///////////////////////////////////////////////////////////////////
class JavaCompilerHelper {
    public JavaCompilerHelper(OutputStream out) {
                this.out = out;
        }

        public boolean compile(String[] args) {
                return internalCompile(args);
        }

        protected boolean internalCompile(String[] args) {

                System.out.println("Args: ");
                for(String arg : args){
                        System.out.print(arg+" ");
                }
        System.out.println();
                ClassLoader cl = Thread.currentThread().getContextClassLoader();
                Class comSunToolsJavacMainClass = null;
                try {
                        /* try to use the new compiler */
                        comSunToolsJavacMainClass =
                                cl.loadClass("com.sun.tools.javac.Main");
                        try {
                                Method compileMethod =
                                        comSunToolsJavacMainClass.getMethod(
                                                "compile",
                                                compile141MethodSignature);
                                try {
                                        Object result =
                                                compileMethod.invoke(
                                                        null,
                                                        new Object[] { args, new PrintWriter(out)});
                                        if (!(result instanceof Integer)) {
                                                return false;
                                        }
                                        return ((Integer) result).intValue() == 0;
                                } catch (IllegalAccessException e3) {
                                        return false;
                                } catch (IllegalArgumentException e3) {
                                        return false;
                                } catch (InvocationTargetException e3) {
                                        return false;
                                }
                        } catch (NoSuchMethodException e2) {
              System.out.println("ERROR: Compile failed with error:" + e2.toString() );
                        }
                } catch (ClassNotFoundException e) {
                        e.printStackTrace();
                        return false;
                } catch (SecurityException e) {
                        return false;
                }
                return true;
        }

        protected String getGenericErrorMessage() {return "javacompiler.error"; }
        protected void run() {  }
        protected boolean parseArguments(String[] args) {return false;}
        protected OutputStream out;

        protected static final Class[] compile141MethodSignature;
        static
        {
                compile141MethodSignature = new Class[2];
                compile141MethodSignature[0] = (new String[0]).getClass();
                compile141MethodSignature[1] = PrintWriter.class;
        }
}