Class loading fixes

This commit is contained in:
dragonmacher
2026-05-13 18:15:56 -04:00
parent 56d5167e93
commit 5b0295e0d1
2 changed files with 25 additions and 11 deletions
@@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@@ -15,6 +15,9 @@
*/ */
package ghidra.app.nav; package ghidra.app.nav;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import ghidra.framework.options.SaveState; import ghidra.framework.options.SaveState;
import ghidra.program.model.listing.Program; import ghidra.program.model.listing.Program;
import ghidra.program.util.AddressFieldLocation; import ghidra.program.util.AddressFieldLocation;
@@ -22,9 +25,6 @@ import ghidra.program.util.ProgramLocation;
import ghidra.util.Msg; import ghidra.util.Msg;
import ghidra.util.SystemUtilities; import ghidra.util.SystemUtilities;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
public class LocationMemento { public class LocationMemento {
private static final String PROGRAM_PATH = "PROGRAM_PATH_"; private static final String PROGRAM_PATH = "PROGRAM_PATH_";
private static final String PROGRAM_ID = "PROGRAM_ID"; private static final String PROGRAM_ID = "PROGRAM_ID";
@@ -86,7 +86,8 @@ public class LocationMemento {
return false; return false;
} }
LocationMemento other = (LocationMemento) obj; LocationMemento other = (LocationMemento) obj;
return (program == other.program && compareLocations(programLocation, other.programLocation)); return (program == other.program &&
compareLocations(programLocation, other.programLocation));
} }
@Override @Override
@@ -142,7 +143,6 @@ public class LocationMemento {
programLocation.saveState(saveState); programLocation.saveState(saveState);
} }
@SuppressWarnings("unchecked")
// we saved the class, it should be the right type // we saved the class, it should be the right type
public static LocationMemento getLocationMemento(SaveState saveState, Program[] programs) { public static LocationMemento getLocationMemento(SaveState saveState, Program[] programs) {
String className = saveState.getString(MEMENTO_CLASS, null); String className = saveState.getString(MEMENTO_CLASS, null);
@@ -150,10 +150,15 @@ public class LocationMemento {
return null; return null;
} }
ClassLoader loader = LocationMemento.class.getClassLoader();
try { try {
Class<? extends LocationMemento> mementoClass = Class<?> clazz = Class.forName(className, false, loader);
(Class<? extends LocationMemento>) Class.forName(className); if (!LocationMemento.class.isAssignableFrom(clazz)) {
Msg.error(LocationMemento.class, "Class is not a LocationMemento: " + clazz);
return null;
}
Class<? extends LocationMemento> mementoClass = clazz.asSubclass(LocationMemento.class);
Constructor<? extends LocationMemento> constructor = Constructor<? extends LocationMemento> constructor =
mementoClass.getConstructor(SaveState.class, Program[].class); mementoClass.getConstructor(SaveState.class, Program[].class);
return constructor.newInstance(saveState, programs); return constructor.newInstance(saveState, programs);
@@ -182,7 +187,8 @@ public class LocationMemento {
// Cause could be null here, so protect. // Cause could be null here, so protect.
String message = cause == null ? "" : cause.getMessage(); String message = cause == null ? "" : cause.getMessage();
throw new IllegalArgumentException("Unexpected exception restoring memento: " + message); throw new IllegalArgumentException(
"Unexpected exception restoring memento: " + message);
} }
return null; return null;
@@ -278,8 +278,10 @@ public class ProgramLocation implements Cloneable, Comparable<ProgramLocation> {
return null; return null;
} }
ClassLoader loader = ProgramLocation.class.getClassLoader();
try { try {
Class<?> locationClass = Class.forName(className);
Class<?> locationClass = Class.forName(className, false, loader);
if (locationClass.isInterface()) { if (locationClass.isInterface()) {
// This check is needed due to a refactoring that has changed a class into an // This check is needed due to a refactoring that has changed a class into an
// interface. The class name may have been saved into the tool. Upon restoring we // interface. The class name may have been saved into the tool. Upon restoring we
@@ -288,6 +290,12 @@ public class ProgramLocation implements Cloneable, Comparable<ProgramLocation> {
return null; return null;
} }
if (!ProgramLocation.class.isAssignableFrom(locationClass)) {
Msg.error(ProgramLocation.class,
"Class is not a ProgramLocation: " + locationClass);
return null;
}
ProgramLocation loc = (ProgramLocation) locationClass.getConstructor().newInstance(); ProgramLocation loc = (ProgramLocation) locationClass.getConstructor().newInstance();
loc.restoreState(program, saveState); loc.restoreState(program, saveState);
if (loc.getAddress() != null) { if (loc.getAddress() != null) {