protected <A extends Annotation> A getAnnotation(Class<A> annoType, Symbol annotated) { if (!annoType.isAnnotation()) { throw new IllegalArgumentException("Not an annotation type: " + annoType); } String name = annoType.getName(); for (Attribute.Compound attr : annotated.getAnnotationMirrors()) { if (name.equals(attr.type.tsym.flatName().toString())) { return AnnotationProxyMaker.generateAnnotation(env, attr, annoType); } } return null; }
public void close() { if (jusl) { try { // Call java.util.ServiceLoader.reload Method reloadMethod = loaderClass.getMethod("reload"); reloadMethod.invoke(loader); } catch (Exception e) {; // Ignore problems during a call to reload. } } }
ServiceIterator(ClassLoader classLoader, Log log) { String loadMethodName; this.log = log; try { try { loaderClass = Class.forName("java.util.ServiceLoader"); loadMethodName = "load"; jusl = true; } catch (ClassNotFoundException cnfe) { try { loaderClass = Class.forName("sun.misc.Service"); loadMethodName = "providers"; jusl = false; } catch (ClassNotFoundException cnfe2) { // Fail softly if a loader is not actually needed. this.iterator = handleServiceLoaderUnavailability("proc.no.service", null); return; } } // java.util.ServiceLoader.load or sun.misc.Service.providers Method loadMethod = loaderClass.getMethod(loadMethodName, Class.class, ClassLoader.class); Object result = loadMethod.invoke(null, Processor.class, classLoader); // For java.util.ServiceLoader, we have to call another // method to get the iterator. if (jusl) { loader = result; // Store ServiceLoader to call reload later Method m = loaderClass.getMethod("iterator"); result = m.invoke(result); // serviceLoader.iterator(); } // The result should now be an iterator. this.iterator = (Iterator<?>) result; } catch (Throwable t) { log.error("proc.service.problem"); throw new Abort(t); } }
/** {@inheritDoc} Overridden here to handle @Inherited. */ public <A extends Annotation> A getAnnotation(Class<A> annoType) { boolean inherited = annoType.isAnnotationPresent(Inherited.class); for (Type t = sym.type; t.tsym != env.symtab.objectType.tsym && !t.isErroneous(); t = env.jctypes.supertype(t)) { A result = getAnnotation(annoType, t.tsym); if (result != null || !inherited) { return result; } } return null; }
protected Object callMethod(Object o, Class<?> clazz, String name) { try { Method m = clazz.getDeclaredMethod(name); boolean prev = m.isAccessible(); m.setAccessible(true); try { return m.invoke(o); } finally { m.setAccessible(prev); } } catch (ReflectiveOperationException e) { return e; } catch (SecurityException e) { return e; } }
protected Object getField(Object o, Class<?> clazz, String name) { try { Field f = clazz.getDeclaredField(name); boolean prev = f.isAccessible(); f.setAccessible(true); try { return f.get(o); } finally { f.setAccessible(prev); } } catch (ReflectiveOperationException e) { return e; } catch (SecurityException e) { return e; } }
protected String info(Class<?> clazz, Object internal, Object external) { return String.format("%s,%s,%s", clazz.getSimpleName(), internal, external); }
/** * Returns an object cast to the specified type. * * @throws NullPointerException if the object is {@code null} * @throws IllegalArgumentException if the object is of the wrong type */ private static <T> T cast(Class<T> clazz, Object o) { if (!clazz.isInstance(o)) throw new IllegalArgumentException(o.toString()); return clazz.cast(o); }