Skip to content

Commit

Permalink
All classes add to the versioned bundle will have the versioned prope…
Browse files Browse the repository at this point in the history
…rty set on them
  • Loading branch information
Col-E committed Aug 26, 2023
1 parent c4c6efe commit cd1e726
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package software.coley.recaf.info.properties.builtin;

import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import software.coley.recaf.info.JvmClassInfo;
Expand All @@ -13,6 +14,7 @@
* @author Matt Coley
*/
public class RemoteClassloaderProperty extends BasicProperty<Integer> {
private static final Int2ObjectArrayMap<RemoteClassloaderProperty> cache = new Int2ObjectArrayMap<>();
public static final String KEY = "remote-classloader-id";

/**
Expand Down Expand Up @@ -44,7 +46,7 @@ public static Integer get(@Nonnull JvmClassInfo classInfo) {
* used as key for {@link WorkspaceRemoteVmResource#getJvmClassloaderBundles()}.
*/
public static void set(@Nonnull JvmClassInfo classInfo, int loaderId) {
classInfo.setProperty(new RemoteClassloaderProperty(loaderId));
classInfo.setProperty(cache.computeIfAbsent(loaderId, RemoteClassloaderProperty::new));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package software.coley.recaf.info.properties.builtin;

import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Nullable;
import software.coley.recaf.info.JvmClassInfo;
Expand All @@ -13,6 +14,7 @@
* @author Matt Coley
*/
public class VersionedClassProperty extends BasicProperty<Integer> {
private static final Int2ObjectArrayMap<VersionedClassProperty> cache = new Int2ObjectArrayMap<>();
public static final String KEY = "meta-inf-versioned";

/**
Expand Down Expand Up @@ -44,7 +46,7 @@ public static Integer get(@Nonnull JvmClassInfo classInfo) {
* used as key for {@link WorkspaceResource#getVersionedJvmClassBundles()}.
*/
public static void set(@Nonnull JvmClassInfo classInfo, int version) {
classInfo.setProperty(new VersionedClassProperty(version));
classInfo.setProperty(cache.computeIfAbsent(version, VersionedClassProperty::new));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package software.coley.recaf.workspace.model.bundle;

import jakarta.annotation.Nonnull;
import software.coley.recaf.info.JvmClassInfo;
import software.coley.recaf.info.properties.builtin.VersionedClassProperty;

/**
* Basic versioned JVM class bundle implementation.
*
* @author Matt Coley
*/
public class BasicVersionedClassBundle extends BasicJvmClassBundle implements VersionedClassBundle {
public class BasicVersionedClassBundle extends BasicJvmClassBundle implements VersionedClassBundle, BundleListener<JvmClassInfo> {
private final int version;

/**
Expand All @@ -14,10 +18,28 @@ public class BasicVersionedClassBundle extends BasicJvmClassBundle implements Ve
*/
public BasicVersionedClassBundle(int version) {
this.version = version;

// Register self as listener. We'll apply the versioned class property to items added to this bundle.
addBundleListener(this);
}

@Override
public int version() {
return version;
}

@Override
public void onNewItem(@Nonnull String key, @Nonnull JvmClassInfo value) {
VersionedClassProperty.set(value, version);
}

@Override
public void onUpdateItem(@Nonnull String key, @Nonnull JvmClassInfo oldValue, @Nonnull JvmClassInfo newValue) {
VersionedClassProperty.set(newValue, version);
}

@Override
public void onRemoveItem(@Nonnull String key, @Nonnull JvmClassInfo value) {
// no-op
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package software.coley.recaf.workspace.model.bundle;


import jakarta.annotation.Nonnull;

/**
* Listener for updates to contents within a {@link Bundle}.
*
Expand All @@ -15,7 +18,7 @@ public interface BundleListener<I> {
* @param value
* Item value.
*/
void onNewItem(String key, I value);
void onNewItem(@Nonnull String key, @Nonnull I value);

/**
* @param key
Expand All @@ -25,13 +28,13 @@ public interface BundleListener<I> {
* @param newValue
* New item value.
*/
void onUpdateItem(String key, I oldValue, I newValue);
void onUpdateItem(@Nonnull String key, @Nonnull I oldValue, @Nonnull I newValue);

/**
* @param key
* Item key.
* @param value
* Item value.
*/
void onRemoveItem(String key, I value);
void onRemoveItem(@Nonnull String key, @Nonnull I value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,13 +237,13 @@ private RemoteJvmClassBundle(@Nonnull ClassLoaderInfo loaderInfo) {

addBundleListener(new BundleListener<>() {
@Override
public void onNewItem(String key, JvmClassInfo value) {
public void onNewItem(@Nonnull String key, @Nonnull JvmClassInfo value) {
// Should occur when we get data from the client.
// No action needed.
}

@Override
public void onUpdateItem(String key, JvmClassInfo oldValue, JvmClassInfo newValue) {
public void onUpdateItem(@Nonnull String key, @Nonnull JvmClassInfo oldValue, @Nonnull JvmClassInfo newValue) {
// Should occur when the user makes changes to a class from recaf.
// We need to send this definition to the remote server.

Expand All @@ -262,7 +262,7 @@ public void onUpdateItem(String key, JvmClassInfo oldValue, JvmClassInfo newValu
}

@Override
public void onRemoveItem(String key, JvmClassInfo value) {
public void onRemoveItem(@Nonnull String key, @Nonnull JvmClassInfo value) {
// Should not occur
throw new IllegalStateException("Remove operations should not occur for remote VM resource!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void setupListenerDelegation() {
protected void delegateJvmClassBundle(@Nonnull WorkspaceResource resource, @Nonnull JvmClassBundle bundle) {
bundle.addBundleListener(new BundleListener<>() {
@Override
public void onNewItem(String key, JvmClassInfo cls) {
public void onNewItem(@Nonnull String key, @Nonnull JvmClassInfo cls) {
for (ResourceJvmClassListener listener : jvmClassListeners) {
try {
listener.onNewClass(resource, bundle, cls);
Expand All @@ -116,7 +116,7 @@ public void onNewItem(String key, JvmClassInfo cls) {
}

@Override
public void onUpdateItem(String key, JvmClassInfo oldCls, JvmClassInfo newCls) {
public void onUpdateItem(@Nonnull String key, @Nonnull JvmClassInfo oldCls, @Nonnull JvmClassInfo newCls) {
for (ResourceJvmClassListener listener : jvmClassListeners) {
try {
listener.onUpdateClass(resource, bundle, oldCls, newCls);
Expand All @@ -127,7 +127,7 @@ public void onUpdateItem(String key, JvmClassInfo oldCls, JvmClassInfo newCls) {
}

@Override
public void onRemoveItem(String key, JvmClassInfo cls) {
public void onRemoveItem(@Nonnull String key, @Nonnull JvmClassInfo cls) {
for (ResourceJvmClassListener listener : jvmClassListeners) {
try {
listener.onRemoveClass(resource, bundle, cls);
Expand All @@ -150,7 +150,7 @@ public void onRemoveItem(String key, JvmClassInfo cls) {
protected void delegateAndroidClassBundle(@Nonnull WorkspaceResource resource, @Nonnull AndroidClassBundle bundle) {
bundle.addBundleListener(new BundleListener<>() {
@Override
public void onNewItem(String key, AndroidClassInfo cls) {
public void onNewItem(@Nonnull String key, @Nonnull AndroidClassInfo cls) {
for (ResourceAndroidClassListener listener : androidClassListeners) {
try {
listener.onNewClass(resource, bundle, cls);
Expand All @@ -161,7 +161,7 @@ public void onNewItem(String key, AndroidClassInfo cls) {
}

@Override
public void onUpdateItem(String key, AndroidClassInfo oldCls, AndroidClassInfo newCls) {
public void onUpdateItem(@Nonnull String key, @Nonnull AndroidClassInfo oldCls, @Nonnull AndroidClassInfo newCls) {
for (ResourceAndroidClassListener listener : androidClassListeners) {
try {
listener.onUpdateClass(resource, bundle, oldCls, newCls);
Expand All @@ -172,7 +172,7 @@ public void onUpdateItem(String key, AndroidClassInfo oldCls, AndroidClassInfo n
}

@Override
public void onRemoveItem(String key, AndroidClassInfo cls) {
public void onRemoveItem(@Nonnull String key, @Nonnull AndroidClassInfo cls) {
for (ResourceAndroidClassListener listener : androidClassListeners) {
try {
listener.onRemoveClass(resource, bundle, cls);
Expand All @@ -195,7 +195,7 @@ public void onRemoveItem(String key, AndroidClassInfo cls) {
protected void delegateFileBundle(@Nonnull WorkspaceResource resource, @Nonnull FileBundle bundle) {
bundle.addBundleListener(new BundleListener<>() {
@Override
public void onNewItem(String key, FileInfo file) {
public void onNewItem(@Nonnull String key, @Nonnull FileInfo file) {
for (ResourceFileListener listener : fileListeners) {
try {
listener.onNewFile(resource, bundle, file);
Expand All @@ -206,7 +206,7 @@ public void onNewItem(String key, FileInfo file) {
}

@Override
public void onUpdateItem(String key, FileInfo oldFile, FileInfo newFile) {
public void onUpdateItem(@Nonnull String key, @Nonnull FileInfo oldFile, @Nonnull FileInfo newFile) {
for (ResourceFileListener listener : fileListeners) {
try {
listener.onUpdateFile(resource, bundle, oldFile, newFile);
Expand All @@ -217,7 +217,7 @@ public void onUpdateItem(String key, FileInfo oldFile, FileInfo newFile) {
}

@Override
public void onRemoveItem(String key, FileInfo file) {
public void onRemoveItem(@Nonnull String key, @Nonnull FileInfo file) {
for (ResourceFileListener listener : fileListeners) {
try {
listener.onRemoveFile(resource, bundle, file);
Expand Down

0 comments on commit cd1e726

Please sign in to comment.