Skip to content

Commit

Permalink
Only load neighbors when required
Browse files Browse the repository at this point in the history
  • Loading branch information
Revxrsal committed Jul 16, 2021
1 parent d122692 commit 0e3d4c5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
22 changes: 13 additions & 9 deletions core/src/main/java/io/tunabytes/bytecode/MixinsConfig.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.tunabytes.bytecode;

import io.tunabytes.classloader.TunaClassDefiner;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
Expand All @@ -20,15 +22,17 @@ public MixinsConfig() {
properties.forEach((key, value) -> mixinEntries.add(new MixinEntry((String) key, (String) value)));
InputStream neighborsStream = getClass().getResourceAsStream("/mixins-neighbors.properties");
requireNonNull(neighborsStream, "mixins-neighbors.properties not found. Did you add tuna-bytes as an annotation processor?");
Properties neighborsProps = new Properties();
neighborsProps.load(neighborsStream);
neighborsProps.forEach((key, value) -> {
try {
neighbors.put((String) key, Class.forName(String.valueOf(value)));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
});
if (TunaClassDefiner.requiresNeighbor()) {
Properties neighborsProps = new Properties();
neighborsProps.load(neighborsStream);
neighborsProps.forEach((key, value) -> {
try {
neighbors.put((String) key, Class.forName(String.valueOf(value)));
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
5 changes: 5 additions & 0 deletions core/src/main/java/io/tunabytes/classloader/ClassDefiner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ Class<?> defineClass(String name,
Class<?> neighbor,
ClassLoader loader,
ProtectionDomain protectionDomain) throws ClassFormatError;

default boolean requiresNeighbor() {
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ public static Class<?> defineClass(String name,
ProtectionDomain protectionDomain) throws ClassFormatError {
return classDefiner.defineClass(name, b, 0, b.length, neighbor, loader, protectionDomain);
}

public static boolean requiresNeighbor() {
return classDefiner.requiresNeighbor();
}
}
4 changes: 4 additions & 0 deletions java11/src/main/java/io/tunabytes/classloader/Java11.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,8 @@ public static Class<?> toClass(Class<?> neighbor, byte[] bcode) {
+ " has no permission to define the class");
}
}

@Override public boolean requiresNeighbor() {
return true;
}
}

0 comments on commit 0e3d4c5

Please sign in to comment.