这是一个可行的解决方案,将内部类的类文件复制到包含编织目标的第三方插件的 bin/ 目录中。如果有人知道这样做的不那么老套的方法,请发布单独的答案。
编辑:使原始解决方案更通用,发现正在编织的类的所有内部类文件,如果在那里丢失或大小不同,则将它们复制到编织目标插件。
public class Activator extends Plugin {
...
public static final String CLASS_FILE_ROOT = "bin/";
/**
* Binary weaving of a class file may result in the addition or modification of
* inner class files in this plug-in which need to be placed on the classpath
* of the plug-in containing the weave target. This method compares inner class
* files for a given class in this plug-in with inner class files in the plug-in
* that contains the weave target. Any inner class files missing from the weave
* target or with a different size than the corresponding file in this plug-in
* are copied to the appropriate package directory in the weave target plug-in.
*
* @param bundle the bundle of the class being woven
* @param className the fully qualified name of the class being woven
* @return true if no errors were encountered in copying the inner class files
*/
private boolean checkInnerClassFileSync(Bundle bundle, String className) {
className = className.replaceAll("\\.", "/");
String classDir = CLASS_FILE_ROOT + className.substring(
0, FilenameUtils.indexOfLastSeparator(className));
for (String innerClass : getInnerClassNames(classDir, FilenameUtils.getName(className))) {
try {
URL srcUrl = getBundle().getEntry(classDir + "/" + innerClass);
File srcFile = new File(FileLocator.resolve(srcUrl).toURI());
URL destUrl = bundle.getEntry(classDir);
File destDir = new File(FileLocator.resolve(destUrl).toURI());
File destFile = FileUtils.getFile(destDir, innerClass);
if (srcFile.isFile() && (!destFile.exists() || destFile.length() != srcFile.length())) {
FileUtils.copyFile(srcFile, destFile);
}
} catch (IOException | URISyntaxException e) {
logger.log(Level.ERROR, "An error occurred while trying to copy an inner class file to the weave target bundle", e);
return false;
}
}
return true;
}
/**
* Get the class files representing inner classes defined in a specified class and
* found in a specified directory.
*
* @param dir a sub-directory containing a class file for <code>className</code>
* and possibly one or more class files representing inner classes defined in
* <code>className</code>
* @param className a class whose class file is found in <code>dir</code>
* @return class files names representing every inner class defined in
* <code>className</code> and found in <code>dir</code>.
*/
private String[] getInnerClassNames(String dir, String className) {
List<String> classNames = new ArrayList<String>();
try {
File classDir = new File(FileLocator.resolve(getBundle().getEntry(dir)).toURI());
for (String fName : classDir.list()) {
Pattern p = Pattern.compile("^" + className + "\\$.+$");
Matcher m = p.matcher(fName);
if (m.matches()) {
classNames.add(fName);
}
}
return classNames.toArray(new String[0]);
} catch (URISyntaxException | IOException e) {
logger.log(Level.ERROR, "An error occured while scanning for inner class files", e);
return classNames.toArray(new String[0]);
}
}
....
}