Set<String> siblings = siblings();
if (friends.isEmpty() && siblings.isEmpty()) {
throw new IllegalStateException("Must specify some friends and/or siblings");
}
ModuleInfo me = Modules.getDefault().ownerOf(getClass());
if (me == null) {
throw new IllegalStateException("No apparent module owning " + getClass());
}
try {
Object manager = me.getClass().getMethod("getManager").invoke(me);
for (String m: friends) {
if (siblings.contains(m)) {
throw new IllegalStateException("Cannot specify the same module " + m + " in both friends and siblings");
}
Object data = data(findDependency(manager, m));
Field friendNamesF = Class.forName("org.netbeans.ModuleData", true, data.getClass().getClassLoader()).getDeclaredField("friendNames");
friendNamesF.setAccessible(true);
Set<?> names = (Set<?>)friendNamesF.get(data);
Set<Object> newNames = new HashSet<>(names);
newNames.add(me.getCodeNameBase());
friendNamesF.set(data, newNames);
}
for (String m: siblings) {
ModuleInfo dep = findDependency(manager, m);
String implVersion = dep.getImplementationVersion();
if (implVersion == null) {
throw new IllegalStateException("No implementation version found in " + m);
}
Object data = data(me);
Field dependenciesF = Class.forName("org.netbeans.ModuleData", true, data.getClass().getClassLoader()).getDeclaredField("dependencies");
dependenciesF.setAccessible(true);
Dependency[] dependencies = (Dependency[])dependenciesF.get(data);
boolean found = false;
for (int i = 0; i < dependencies.length; i++) {
if (dependencies[i].getName().replaceFirst("/.+$", "").equals(m)) {
Set<Dependency> nue = Dependency.create(Dependency.TYPE_MODULE, dependencies[i].getName() + " = " + implVersion);
if (nue.size() != 1) {
throw new IllegalStateException("Could not recreate dependency from " + dependencies[i] + " based on " + implVersion);
}
dependencies[i] = nue.iterator().next();
found = true;
}
}
if (!found) {
throw new IllegalStateException("Did not find dependency on " + m);
}
// StandardModule.classLoaderUp skips adding a parent if the dep seemed to offer us nothing, and this has already been called.
Object[] publicPackages = (Object[])dep.getClass().getMethod("getPublicPackages").invoke(dep);
if (publicPackages != null && publicPackages.length == 0) {
me.getClassLoader().getClass().getMethod("append", ClassLoader[].class).invoke(me.getClassLoader(), (Object)new ClassLoader[]{dep.getClassLoader()});
}
}
} catch (IllegalStateException x) {
throw x;
} catch (Exception x) {