Skip to content

Commit

Permalink
Merge pull request #249 from apache/feature/226-Provide-SPI-interface…
Browse files Browse the repository at this point in the history
…s-to-locate-descriptors-and-JCas-classes

Issue #226: Provide SPI interfaces to locate descriptors and JCas classes
  • Loading branch information
reckart committed Aug 31, 2022
2 parents 27a8186 + 647793d commit 7deca1e
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Parameter;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
Expand Down Expand Up @@ -198,6 +199,8 @@ public abstract class FSClassRegistry { // abstract to prevent instantiating; th
private static final WeakIdentityMap<ClassLoader, StackTraceElement[]> cl_to_type2JCasStacks;
private static final WeakIdentityMap<ClassLoader, Map<String, Class<? extends TOP>>> cl_to_spiJCas = WeakIdentityMap
.newHashMap();
private static final WeakIdentityMap<ClassLoader, UIMAClassLoader> cl_to_uimaCl = WeakIdentityMap
.newHashMap();

// private static final Map<ClassLoader, Map<String, JCasClassInfo>> cl_4pears_to_type2JCas =
// Collections.synchronizedMap(new IdentityHashMap<>()); // identity: key is classloader
Expand Down Expand Up @@ -1749,10 +1752,28 @@ static Map<String, JCasClassInfo> get_className_to_jcci(ClassLoader cl, boolean
}
}

private static final URL[] NO_URLS = new URL[0];

static Lookup getLookup(ClassLoader cl) {
Lookup lookup = null;
try {
Class<?> clazz = Class.forName(UIMAClassLoader.MHLC, true, cl);
// The UIMAClassLoader has special handling for the MHLC, so we must make sure that the CL
// we are using is actually a UIMAClassLoader, otherwise the MHCL will look up in the wrong
// CL. This is in particular an issue for classes loaded through the SPI mechanism.
UIMAClassLoader ucl;
if (!(cl instanceof UIMAClassLoader)) {
synchronized (cl_to_uimaCl) {
ucl = cl_to_uimaCl.get(cl);
if (ucl == null) {
ucl = new UIMAClassLoader(NO_URLS, cl);
cl_to_uimaCl.put(cl, ucl);
}
}
} else {
ucl = (UIMAClassLoader) cl;
}

Class<?> clazz = Class.forName(UIMAClassLoader.MHLC, true, ucl);
Method m = clazz.getMethod("getMethodHandlesLookup");
lookup = (Lookup) m.invoke(null);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException
Expand Down

0 comments on commit 7deca1e

Please sign in to comment.