[FEATURE] Динамическая генерация маппингов для ScriptEngine

This commit is contained in:
Gravit 2019-04-13 10:01:21 +07:00
parent 5178cc4b6a
commit 43aebf9f84
No known key found for this signature in database
GPG key ID: 061981E1E85D3216

View file

@ -4,7 +4,6 @@
import java.nio.file.Path; import java.nio.file.Path;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.jar.JarInputStream;
import java.util.zip.ZipEntry; import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream; import java.util.zip.ZipInputStream;
@ -28,26 +27,29 @@ public static void zipWalk(ZipInputStream input, ZipWalkCallback callback) throw
e = input.getNextEntry(); e = input.getNextEntry();
} }
} }
public static void jarWalk(JarInputStream input, JarWalkCallback callback) throws IOException public static void jarWalk(ZipInputStream input, JarWalkCallback callback) throws IOException
{
ZipEntry e = input.getNextEntry();
while (e != null)
{ {
zipWalk(input, (in, e) -> {
String filename = e.getName(); String filename = e.getName();
if(filename.endsWith(".class")) if(filename.endsWith(".class"))
{ {
String classFull = filename.replaceAll("/", ".").substring(0, String classFull = filename.replaceAll("/", ".").substring(0,
filename.length() - ".class".length()); filename.length() - ".class".length());
String clazz = classFull.substring(classFull.lastIndexOf('.')); String clazz = classFull.substring(classFull.lastIndexOf('.')+1);
callback.process(in,e, classFull, clazz); callback.process(input,e, classFull, clazz);
} }
}); e = input.getNextEntry();
} }
public static Map<String, String> jarMap(JarInputStream input, boolean overwrite) throws IOException }
public static Map<String, String> jarMap(ZipInputStream input, boolean overwrite) throws IOException
{ {
Map<String, String> map = new HashMap<>(); Map<String, String> map = new HashMap<>();
jarMap(input, map, overwrite); jarMap(input, map, overwrite);
return map; return map;
} }
public static void jarMap(JarInputStream input, Map<String, String> map, boolean overwrite) throws IOException public static void jarMap(ZipInputStream input, Map<String, String> map, boolean overwrite) throws IOException
{ {
jarWalk(input, (in, e, classFull, clazz) -> { jarWalk(input, (in, e, classFull, clazz) -> {
if(overwrite) map.put(clazz, classFull); if(overwrite) map.put(clazz, classFull);
@ -56,14 +58,14 @@ public static void jarMap(JarInputStream input, Map<String, String> map, boolean
} }
public static Map<String, String> jarMap(Path file, boolean overwrite) throws IOException public static Map<String, String> jarMap(Path file, boolean overwrite) throws IOException
{ {
try(JarInputStream inputStream = new JarInputStream(IOHelper.newZipInput(file))) try(ZipInputStream inputStream = IOHelper.newZipInput(file))
{ {
return jarMap(inputStream,overwrite); return jarMap(inputStream,overwrite);
} }
} }
public static void jarMap(Path file, Map<String, String> map, boolean overwrite) throws IOException public static void jarMap(Path file, Map<String, String> map, boolean overwrite) throws IOException
{ {
try(JarInputStream inputStream = new JarInputStream(IOHelper.newZipInput(file))) try(ZipInputStream inputStream = IOHelper.newZipInput(file))
{ {
jarMap(inputStream, map,overwrite); jarMap(inputStream, map,overwrite);
} }