Skip to content

Commit

Permalink
Add jar path of the class belongs to
Browse files Browse the repository at this point in the history
  • Loading branch information
Kane committed Feb 10, 2015
1 parent 1230f5e commit abbb2a4
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions src/gz/jythonhelper/EasyJython.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@ abstract class ClassGenerator {
public static final String INIT_TEMPLATE = "# encoding: utf-8\n" +
"# module %s\n" +
"# from (built-in)\n" +
"# by generator 999.999\n";
"# by generator 999.999\n" +
"# source:%s\n";

protected ClassGenerator(String outputDir) {
this.outputDir = outputDir;
Expand Down Expand Up @@ -225,9 +226,13 @@ String indents(String line) {
return sb.toString();
}

void writePyHeader(FileWriter fw, String name) {
void writePyHeader(FileWriter fw, String name, URL resourceURL) {
String source = "";
if (resourceURL != null) {
source = resourceURL.toString();
}
try {
fw.write(String.format(INIT_TEMPLATE, name));
fw.write(String.format(INIT_TEMPLATE, name, source));
} catch (IOException e) {
e.printStackTrace();
}
Expand All @@ -243,7 +248,7 @@ void ensureInitPy(File dir) {
boolean newFile = initFile.createNewFile();
if (newFile) {
FileWriter fw = new FileWriter(initFile);
writePyHeader(fw, initFile.getName());
writePyHeader(fw, initFile.getName(), null);
fw.close();
}
} catch (IOException e) {
Expand Down Expand Up @@ -305,7 +310,13 @@ public void createPyForClass(Class clazz) throws IOException {
File initPy = new File(directory, INIT_PY);
Map<String, StringBuilder> classStringMap = readClassesFromInitPy(initPy);
classStringMap.put(clazz.getSimpleName(), new StringBuilder(classContent));
writeClassesFromInitPy(classStringMap, initPy);
String name = clazz.getName().replace('.', '/') + ".class";
ClassLoader classLoader = clazz.getClassLoader();
URL resourceURL = null;
if (classLoader != null) {
resourceURL = classLoader.getResource(name);
}
writeClassesFromInitPy(classStringMap, initPy, resourceURL);
}

public void createPyForClass0(Class clazz) throws IOException {
Expand Down Expand Up @@ -341,9 +352,9 @@ private Map<String, StringBuilder> readClassesFromInitPy(File initFile) throws I
return maps;
}

private void writeClassesFromInitPy(Map<String, StringBuilder> classStringMap, File initPy) throws IOException {
private void writeClassesFromInitPy(Map<String, StringBuilder> classStringMap, File initPy, URL resourceURL) throws IOException {
FileWriter fileWriter = new FileWriter(initPy);
writePyHeader(fileWriter, initPy.getName());
writePyHeader(fileWriter, initPy.getName(), resourceURL);
for (StringBuilder classContent : classStringMap.values()) {
fileWriter.write(classContent.toString());
}
Expand Down

0 comments on commit abbb2a4

Please sign in to comment.