-
Notifications
You must be signed in to change notification settings - Fork 27
Modernize Java code: remove legacy APIs and improve type safety #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
+84
−87
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,8 +4,8 @@ | |
| // Distributed under the terms of the GNU General Public License (GPL): http://www.gnu.org/licenses/ | ||
| package graphtea.platform.extension; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import graphtea.platform.StaticUtils; | ||
| import graphtea.platform.core.exception.ExceptionHandler; | ||
|
|
||
|
|
@@ -22,7 +22,7 @@ | |
| */ | ||
| public class ExtensionClassLoader extends ClassLoader { | ||
| public Map<String, byte[]> classesData = new HashMap<>(); | ||
| Map<String, Class> classes = new HashMap<>(); | ||
| Map<String, Class<?>> classes = new HashMap<>(); | ||
| List<File> unknownFiles = new ArrayList<>(); | ||
| public static URLClassLoader classLoader; | ||
| public static ClassLoader cl; | ||
|
|
@@ -31,7 +31,7 @@ public ExtensionClassLoader(String dirPath) { | |
| loadClasses(dirPath); | ||
| } | ||
|
|
||
| public Collection<Class> getLoadedClasses() { | ||
| public Collection<Class<?>> getLoadedClasses() { | ||
| return classes.values(); | ||
| } | ||
|
|
||
|
|
@@ -60,14 +60,16 @@ private void loadClassFiles(String dir, String pack) { | |
| else if (file1.getName().endsWith(".class")) | ||
| try { | ||
| String name; | ||
| if (pack.length() > 0) | ||
| if (pack.length() > 0) { | ||
| name = pack.substring(1) + "."; | ||
| else | ||
| } else { | ||
| name = ""; | ||
| } | ||
| name = name + file1.getName().substring(0, file1.getName().length() - 6); | ||
| byte[] data = new byte[(int) file1.length()]; | ||
| FileInputStream fis = new FileInputStream(file1); | ||
| fis.read(data); | ||
| try (FileInputStream fis = new FileInputStream(file1)) { | ||
| fis.read(data); | ||
| } | ||
| classesData.put(name, data); | ||
| urls.add(file1.toURI().toURL()); | ||
| } catch (IOException e) { | ||
|
|
@@ -85,26 +87,28 @@ private void loadClasses(String dirPath) { | |
| // defineClasses(); | ||
| } | ||
|
|
||
| public Class findClass(String name) throws ClassNotFoundException { | ||
| Class ret = null; | ||
| if (!classesData.containsKey(name)) | ||
| public Class<?> findClass(String name) throws ClassNotFoundException { | ||
| Class<?> ret = null; | ||
| if (!classesData.containsKey(name)) { | ||
| ret = getParent().loadClass(name); | ||
| } | ||
| if (ret == null && !classes.containsKey(name)) { | ||
| byte[] data = classesData.get(name); | ||
| Class c = defineClass(name, data, 0, data.length); | ||
| Class<?> c = defineClass(name, data, 0, data.length); | ||
| classes.put(name, c); | ||
| } | ||
| ret = classes.get(name); | ||
| if (ret == null) | ||
| if (ret == null) { | ||
| return classLoader.loadClass(name); | ||
| else | ||
| } else { | ||
| return ret; | ||
| } | ||
| } | ||
|
|
||
| public Collection<Class> getClassesImplementing(Class cl) { | ||
| Collection<Class> col = new ArrayList<>(); | ||
| for (Map.Entry<String, Class> entry1 : classes.entrySet()) { | ||
| Class c = entry1.getValue(); | ||
| public Collection<Class<?>> getClassesImplementing(Class<?> cl) { | ||
| Collection<Class<?>> col = new ArrayList<>(); | ||
| for (Map.Entry<String, Class<?>> entry1 : classes.entrySet()) { | ||
| Class<?> c = entry1.getValue(); | ||
| if (StaticUtils.isImplementing(c, cl)) | ||
| col.add(c); | ||
| } | ||
|
|
@@ -131,36 +135,23 @@ public static void copyInputStream(InputStream in, OutputStream out) | |
| * @param zipFileName The given zipped file name | ||
| */ | ||
| public static void unZip(String zipFileName, String destDir) { | ||
| System.out.println(zipFileName); | ||
| Enumeration entries; | ||
| ZipFile zipFile; | ||
|
|
||
| try { | ||
| zipFile = new ZipFile(zipFileName); | ||
|
|
||
| entries = zipFile.entries(); | ||
|
|
||
| while(entries.hasMoreElements()) { | ||
| ZipEntry entry = (ZipEntry)entries.nextElement(); | ||
|
|
||
| if(entry.isDirectory()) { | ||
| // Assume directories are stored parents first then children. | ||
| // System.err.println("Extracting directory: " + entry.getName()); | ||
| // This is not robust, just for demonstration purposes. | ||
| (new File(destDir+File.separator+entry.getName())).mkdirs(); | ||
| continue; | ||
| } | ||
|
|
||
| System.out.println("Extracting file: " + destDir+entry.getName()); | ||
| copyInputStream(zipFile.getInputStream(entry), | ||
| new BufferedOutputStream(new FileOutputStream(destDir+File.separator+entry.getName()))); | ||
| try (ZipFile zipFile = new ZipFile(zipFileName)) { | ||
| Enumeration<? extends ZipEntry> entries = zipFile.entries(); | ||
| while (entries.hasMoreElements()) { | ||
| ZipEntry entry = entries.nextElement(); | ||
| if (entry.isDirectory()) { | ||
| new File(destDir + File.separator + entry.getName()).mkdirs(); | ||
| continue; | ||
| } | ||
| try (InputStream in = zipFile.getInputStream(entry); | ||
| OutputStream out = new BufferedOutputStream( | ||
| new FileOutputStream(destDir + File.separator + entry.getName()))) { | ||
| copyInputStream(in, out); | ||
|
Comment on lines
+146
to
+149
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2. Zip path traversal extraction ExtensionClassLoader.unZip writes zip entries to destDir + File.separator + entry.getName() without normalizing/validating the entry path, allowing a crafted extension zip to write outside destDir via ../ (Zip Slip). This is particularly risky because loadClassFiles automatically unzips every *.zip in the extensions directory. Agent Prompt
|
||
| } | ||
| } | ||
| } catch (IOException ioe) { | ||
| ExceptionHandler.catchException(ioe); | ||
| } | ||
|
|
||
| zipFile.close(); | ||
| } catch (IOException ioe) { | ||
| System.err.println("Unhandled exception:"); | ||
| ExceptionHandler.catchException(ioe); | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1. Old .tea files unreadable
🐞 Bug✓ CorrectnessAgent Prompt
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools