Skip to content

Commit 21c87ce

Browse files
committed
cleaning up and modernizing
1 parent 696bc82 commit 21c87ce

File tree

1 file changed

+52
-58
lines changed

1 file changed

+52
-58
lines changed

java/src/processing/mode/java/JavaBuild.java

Lines changed: 52 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public JavaBuild(Sketch sketch) {
8686
/**
8787
* Run the build inside a temporary build folder. Used for run/present.
8888
* @return null if compilation failed, main class name if not
89-
* @throws SketchException
89+
* @throws SketchException details of where the build choked
9090
*/
9191
public String build(boolean sizeWarning) throws SketchException {
9292
return build(sketch.makeTempFolder(), sketch.makeTempFolder(), sizeWarning);
@@ -150,7 +150,7 @@ public String preprocess(File srcFolder, boolean sizeWarning) throws SketchExcep
150150
* @param packageName null, or the package name that should be used as default
151151
* @param preprocessor the preprocessor object ready to do the work
152152
* @return main PApplet class name found during preprocess, or null if error
153-
* @throws SketchException
153+
* @throws SketchException details of where the preprocessing failed
154154
*/
155155
public String preprocess(File srcFolder,
156156
String packageName,
@@ -232,11 +232,8 @@ public String preprocess(File srcFolder,
232232
outputFolder.mkdirs();
233233
// Base.openFolder(outputFolder);
234234
final File java = new File(outputFolder, sketch.getName() + ".java");
235-
final PrintWriter stream = PApplet.createWriter(java);
236-
try {
235+
try (PrintWriter stream = PApplet.createWriter(java)) {
237236
result = preprocessor.write(stream, bigCode.toString(), codeFolderPackages);
238-
} finally {
239-
stream.close();
240237
}
241238
} catch (SketchException pe) {
242239
// RunnerExceptions are caught here and re-thrown, so that they don't
@@ -295,9 +292,8 @@ public String preprocess(File srcFolder,
295292
// If someone insists on unnecessarily repeating the code folder
296293
// import, don't show an error for it.
297294
if (codeFolderPackages != null) {
298-
String itemPkg = entry;
299295
for (String pkg : codeFolderPackages) {
300-
if (pkg.equals(itemPkg)) {
296+
if (pkg.equals(entry)) {
301297
found = true;
302298
break;
303299
}
@@ -866,7 +862,7 @@ protected boolean exportApplication(File destFolder,
866862
File plistFile = new File(dotAppFolder, "Contents/Info.plist");
867863
PrintWriter pw = PApplet.createWriter(plistFile);
868864

869-
String lines[] = PApplet.loadStrings(plistTemplate);
865+
String[] lines = PApplet.loadStrings(plistTemplate);
870866
for (int i = 0; i < lines.length; i++) {
871867
if (lines[i].contains("@@")) {
872868
StringBuilder sb = new StringBuilder(lines[i]);
@@ -1070,7 +1066,7 @@ static protected boolean isXcodeInstalled() {
10701066
int result = -1;
10711067
try {
10721068
result = p.waitFor();
1073-
} catch (InterruptedException e) { }
1069+
} catch (InterruptedException ignored) { }
10741070
// returns 0 if installed, 2 if not (-1 if exception)
10751071
xcodeInstalled = (result == 0);
10761072
}
@@ -1157,25 +1153,22 @@ protected void addClasses(ZipOutputStream zos, File dir) throws IOException {
11571153

11581154

11591155
protected void addClasses(ZipOutputStream zos, File dir, String rootPath) throws IOException {
1160-
File files[] = dir.listFiles(new FilenameFilter() {
1161-
public boolean accept(File dir, String name) {
1162-
return (name.charAt(0) != '.');
1163-
}
1164-
});
1165-
for (File sub : files) {
1166-
String relativePath = sub.getAbsolutePath().substring(rootPath.length());
1167-
// System.out.println("relative path is " + relativePath);
1156+
File[] files = dir.listFiles((dir1, name) -> (name.charAt(0) != '.'));
1157+
if (files != null) {
1158+
for (File sub : files) {
1159+
String relativePath = sub.getAbsolutePath().substring(rootPath.length());
11681160

1169-
if (sub.isDirectory()) {
1170-
addClasses(zos, sub, rootPath);
1161+
if (sub.isDirectory()) {
1162+
addClasses(zos, sub, rootPath);
11711163

1172-
} else if (sub.getName().endsWith(".class")) {
1164+
} else if (sub.getName().endsWith(".class")) {
11731165
// System.out.println(" adding item " + relativePath);
1174-
ZipEntry entry = new ZipEntry(relativePath);
1175-
zos.putNextEntry(entry);
1176-
//zos.write(Base.loadBytesRaw(sub));
1177-
PApplet.saveStream(zos, new FileInputStream(sub));
1178-
zos.closeEntry();
1166+
ZipEntry entry = new ZipEntry(relativePath);
1167+
zos.putNextEntry(entry);
1168+
//zos.write(Base.loadBytesRaw(sub));
1169+
PApplet.saveStream(zos, new FileInputStream(sub));
1170+
zos.closeEntry();
1171+
}
11791172
}
11801173
}
11811174
}
@@ -1217,21 +1210,18 @@ protected void packClassPathIntoZipFile(String path,
12171210
throws IOException {
12181211
String[] pieces = PApplet.split(path, File.pathSeparatorChar);
12191212

1220-
for (int i = 0; i < pieces.length; i++) {
1221-
if (pieces[i].length() == 0) continue;
1213+
for (String piece : pieces) {
1214+
if (piece.length() == 0) continue;
12221215

12231216
// is it a jar file or directory?
1224-
if (pieces[i].toLowerCase().endsWith(".jar") ||
1225-
pieces[i].toLowerCase().endsWith(".zip")) {
1217+
if (piece.toLowerCase().endsWith(".jar") ||
1218+
piece.toLowerCase().endsWith(".zip")) {
12261219
try {
1227-
ZipFile file = new ZipFile(pieces[i]);
1220+
ZipFile file = new ZipFile(piece);
12281221
Enumeration<?> entries = file.entries();
12291222
while (entries.hasMoreElements()) {
12301223
ZipEntry entry = (ZipEntry) entries.nextElement();
1231-
if (entry.isDirectory()) {
1232-
// actually 'continue's for all dir entries
1233-
1234-
} else {
1224+
if (!entry.isDirectory()) {
12351225
String entryName = entry.getName();
12361226
// ignore contents of the META-INF folders
12371227
if (entryName.indexOf("META-INF") == 0) continue;
@@ -1243,7 +1233,7 @@ protected void packClassPathIntoZipFile(String path,
12431233
ZipEntry entree = new ZipEntry(entryName);
12441234

12451235
zos.putNextEntry(entree);
1246-
byte buffer[] = new byte[(int) entry.getSize()];
1236+
byte[] buffer = new byte[(int) entry.getSize()];
12471237
InputStream is = file.getInputStream(entry);
12481238

12491239
int offset = 0;
@@ -1262,11 +1252,11 @@ protected void packClassPathIntoZipFile(String path,
12621252
file.close();
12631253

12641254
} catch (IOException e) {
1265-
System.err.println("Error in file " + pieces[i]);
1255+
System.err.println("Error in file " + piece);
12661256
e.printStackTrace();
12671257
}
12681258
} else { // not a .jar or .zip, prolly a directory
1269-
File dir = new File(pieces[i]);
1259+
File dir = new File(piece);
12701260
// but must be a dir, since it's one of several paths
12711261
// just need to check if it exists
12721262
if (dir.exists()) {
@@ -1286,31 +1276,35 @@ static protected void packClassPathIntoZipFileRecursive(File dir,
12861276
String sofar,
12871277
ZipOutputStream zos)
12881278
throws IOException {
1289-
String files[] = dir.list();
1290-
for (int i = 0; i < files.length; i++) {
1291-
// ignore . .. and .DS_Store
1292-
if (files[i].charAt(0) == '.') continue;
1279+
String[] files = dir.list();
1280+
if (files != null) {
1281+
for (String filename : files) {
1282+
// ignore . .. and .DS_Store
1283+
if (filename.charAt(0) == '.') continue;
12931284

1294-
File sub = new File(dir, files[i]);
1295-
String nowfar = (sofar == null) ?
1296-
files[i] : (sofar + "/" + files[i]);
1285+
File sub = new File(dir, filename);
1286+
String nowfar = (sofar == null) ?
1287+
filename : (sofar + "/" + filename);
12971288

1298-
if (sub.isDirectory()) {
1299-
packClassPathIntoZipFileRecursive(sub, nowfar, zos);
1289+
if (sub.isDirectory()) {
1290+
packClassPathIntoZipFileRecursive(sub, nowfar, zos);
13001291

1301-
} else {
1302-
// don't add .jar and .zip files, since they only work
1303-
// inside the root, and they're unpacked
1304-
if (!files[i].toLowerCase().endsWith(".jar") &&
1305-
!files[i].toLowerCase().endsWith(".zip") &&
1306-
files[i].charAt(0) != '.') {
1307-
ZipEntry entry = new ZipEntry(nowfar);
1308-
zos.putNextEntry(entry);
1309-
//zos.write(Base.loadBytesRaw(sub));
1310-
PApplet.saveStream(zos, new FileInputStream(sub));
1311-
zos.closeEntry();
1292+
} else {
1293+
// don't add .jar and .zip files, since they only work
1294+
// inside the root, and they're unpacked
1295+
if (!filename.toLowerCase().endsWith(".jar") &&
1296+
!filename.toLowerCase().endsWith(".zip") &&
1297+
filename.charAt(0) != '.') {
1298+
ZipEntry entry = new ZipEntry(nowfar);
1299+
zos.putNextEntry(entry);
1300+
//zos.write(Base.loadBytesRaw(sub));
1301+
PApplet.saveStream(zos, new FileInputStream(sub));
1302+
zos.closeEntry();
1303+
}
13121304
}
13131305
}
1306+
} else {
1307+
System.err.println("Could not read from " + dir);
13141308
}
13151309
}
13161310
}

0 commit comments

Comments
 (0)