protected void maybeAddAncestors(Gwtc gwtc, Class<?> c) { addGwtcClass(gwtc, c); for (AncestorMode mode : gwtc.inheritanceMode()) { switch (mode) { case INHERIT_ONE_PARENT: Package pkg = c.getPackage(); gwtc = pkg.getAnnotation(Gwtc.class); if (gwtc != null && addPackage(pkg)) { addGwtcPackage(gwtc, pkg, false); } break; case INHERIT_ALL_PARENTS: addAllPackages(c.getPackage()); break; case INHERIT_ENCLOSING_CLASSES: addEnclosingClasses(c); break; case INHERIT_SUPER_CLASSES: addSuperclasses(c); break; default: X_Log.warn("Unsupported mode type", mode, "for class", c); } } }
protected void addAllPackages(Package pkg) { Gwtc gwtc = pkg.getAnnotation(Gwtc.class); if (gwtc != null && addPackage(pkg)) { addGwtcPackage(gwtc, pkg, false); } String parentName = pkg.getName(); int ind = parentName.lastIndexOf('.'); while (ind > -1) { parentName = parentName.substring(0, ind); ind = parentName.lastIndexOf('.'); pkg = GwtReflect.getPackage(parentName); X_Log.debug(getClass(), "Checking parent package", "'" + parentName + "'", pkg != null); if (pkg != null) { gwtc = pkg.getAnnotation(Gwtc.class); if (gwtc != null && addPackage(pkg)) { addGwtcPackage(gwtc, pkg, false); } } } pkg = GwtReflect.getPackage(""); if (pkg != null) { gwtc = pkg.getAnnotation(Gwtc.class); if (gwtc != null && addPackage(pkg)) { addGwtcPackage(gwtc, pkg, false); } } }
private Object findAncestor(Class<?> clazz) { // First check enclosing classes Class<?> c = clazz.getDeclaringClass(); while (c != null) { if (c.isAnnotationPresent(Gwtc.class)) { return c; } c = c.getDeclaringClass(); } Package p = clazz.getPackage(); if (p.getAnnotation(Gwtc.class) != null) { return p; } Object o = findAncestor(p); if (o == null) { c = clazz.getSuperclass(); while (c != null) { if (c.isAnnotationPresent(Gwtc.class)) { return c; } c = c.getSuperclass(); } } return o; }
public ClassMeta getClassInfo(Class<?> cls) { final Package pkg = cls.getPackage(); URL loadedFrom = null; try { loadedFrom = cls.getProtectionDomain().getCodeSource().getLocation(); } catch (Throwable t) { Log.warn(t, "Failed to get source for %s", cls); } final API apiAnnotation = pkg.getAnnotation(API.class); final ApiInfo apiInfo = apiAnnotation != null ? new ApiInfo(apiAnnotation) : null; Map<File, Set<String>> mods = Maps.newHashMap(); for (ModCandidate candidate : table.getCandidatesFor(pkg.getName())) { if (!candidate.getClassList().contains(cls.getName().replace('.', '/'))) continue; final File candidateFile = candidate.getModContainer(); Set<String> modIds = Sets.newHashSet(); mods.put(candidateFile, modIds); for (ModContainer mod : candidate.getContainedMods()) modIds.add(mod.getModId()); } return new ClassMeta(cls, loadedFrom, apiInfo, mods); }
/** * Get the {@link JAXBContext} from an existing {@link Class} object. If the class's owning * package is a valid JAXB package, this method redirects to {@link #getFromCache(Package)} * otherwise a new JAXB context is created and NOT cached. * * @param aClass The class for which the JAXB context is to be created. May not be <code>null * </code>. * @param aClassLoader Class loader to use. May be <code>null</code> in which case the default * class loader is used. * @return Never <code>null</code>. */ @Nonnull public JAXBContext getFromCache( @Nonnull final Class<?> aClass, @Nullable final ClassLoader aClassLoader) { ValueEnforcer.notNull(aClass, "Class"); final Package aPackage = aClass.getPackage(); if (aPackage.getAnnotation(XmlSchema.class) != null) { // Redirect to cached version return getFromCache(aPackage, aClassLoader); } // E.g. an internal class - try anyway! if (GlobalDebug.isDebugMode()) s_aLogger.info("Creating JAXB context for class " + aClass.getName()); if (aClassLoader != null) s_aLogger.warn( "Package " + aPackage.getName() + " does not seem to be JAXB generated. Therefore a new JAXBContext is created and the provided ClassLoader is ignored!"); try { return JAXBContext.newInstance(aClass); } catch (final JAXBException ex) { final String sMsg = "Failed to create JAXB context for class '" + aClass.getName() + "'"; s_aLogger.error(sMsg + ": " + ex.getMessage()); throw new IllegalArgumentException(sMsg, ex); } }
private List<String> dependenciesOf(Package source) { List<String> result = new ArrayList<>(); if (source.isAnnotationPresent(DependsUpon.class)) for (Class<?> target : source.getAnnotation(DependsUpon.class).packagesOf()) result.add(target.getPackage().getName()); return result; }
/** * Inspects the resource to determine what api it belongs to. It does this by looking for the * WebApi package annotation. * * @param resource * @return Api */ public static Api inspectApi(Class<?> resource) { Package myPackage = resource.getPackage(); Annotation annot = myPackage.getAnnotation(WebApi.class); if (annot != null) { Map<String, Object> annotAttribs = AnnotationUtils.getAnnotationAttributes(annot); String apiName = String.valueOf(annotAttribs.get("name")); String apiScope = String.valueOf(annotAttribs.get("scope")); String apiVersion = String.valueOf(annotAttribs.get("version")); return Api.valueOf(apiName, apiScope, apiVersion); } return null; }
private static String getNamespace(Class<?> jaxbClass, Map<Package, String> pkgToNamespace) { Package pkg = jaxbClass.getPackage(); String namespace = pkgToNamespace.get(pkg); if (namespace == null) { XmlSchema schemaAnnot = pkg.getAnnotation(XmlSchema.class); if (schemaAnnot != null) { namespace = schemaAnnot.namespace(); // XmlNs[] xmlns = schemaAnnot.xmlns(); Useful if we need prefix for namespace pkgToNamespace.put(pkg, namespace); } } return namespace; }
public <A extends Annotation> A getPackageAnnotation(Class<A> a, Class clazz, Locatable srcPos) { Package p = clazz.getPackage(); if (p == null) return null; Map<Package, Annotation> cache = packageCache.get(a); if (cache == null) { cache = new HashMap<Package, Annotation>(); packageCache.put(a, cache); } if (cache.containsKey(p)) return (A) cache.get(p); else { A ann = LocatableAnnotation.create(p.getAnnotation(a), srcPos); cache.put(p, ann); return ann; } }
/** Looks to the default plug-in directory location to initialize this store */ public void initializeOrUpdateStore() { try { List<PackageInfo> list = PackageInfoPeerClassFinder.findPackageInfo(); for (PackageInfo pi : list) { for (String className : pi.getClassList()) { try { // If we don't have the class Class<?> o = Class.forName(className); if (o == null) { throw new Exception("Class not available"); } } catch (NoClassDefFoundError ncdfe) { // By Design: gobbling up this error to reduce the // non-needed noise upon startup. If there is a real // issue, then it will bubble up somewhere else. } catch (Exception e) { // Explicitly load classes from packages that have // package-info try { ClassLoader.getSystemClassLoader().loadClass(className); } catch (java.lang.NoClassDefFoundError ncdfe) { // By Design: gobbling up this error to reduce the // non-needed noise upon startup. If there is a real // issue, then it will bubble up somewhere else. } } Package packageItem = Package.getPackage(pi.getName()); if (null != packageItem.getAnnotation(MockeyRequestInspector.class)) { Class<?> x = doesThisImplementIRequestInspector(className); if (x != null && !this.reqInspectorClassNameList.contains(x)) { this.reqInspectorClassNameList.add(x); logger.debug("Plugin added: " + className); } } } } } catch (Exception e) { logger.error("Found a Mockey.jar, but unable read mockey jar", e); } }
/** Finds the editor message bundle for the supplied class. */ protected static String findMessageBundle(Class<?> clazz) { EditorMessageBundle annotation = clazz.getAnnotation(EditorMessageBundle.class); if (annotation != null) { return annotation.value(); } Class<?> eclazz = clazz.getEnclosingClass(); if (eclazz != null) { return getMessageBundle(eclazz); } String name = clazz.getName(); int idx; while ((idx = name.lastIndexOf('.')) != -1) { name = name.substring(0, idx); Package pkg = Package.getPackage(name); if (pkg != null) { annotation = pkg.getAnnotation(EditorMessageBundle.class); if (annotation != null) { return annotation.value(); } } } return EditorMessageBundle.DEFAULT; }
private void scanPackage(Package pkg) { GwtcUnit data = nodes.get(pkg); if (data == null) { Object ancestor = findAncestor(pkg); if (ancestor != null) { addPackage((Package) ancestor); } return; } else if (data.gwtc == null) { return; } Gwtc gwtc = pkg.getAnnotation(Gwtc.class); addGwtcPackage(gwtc, pkg, false); X_Log.trace(getClass(), "Parent of ", pkg, "is", data.getParent()); ClassLoader cl = Thread.currentThread().getContextClassLoader(); for (ClassFile file : classpath.get().findClassesInPackage(pkg.getName())) { X_Log.trace(getClass(), "Checking class file ", file.getName()); try { if (file.getEnclosedName().equals("package-info")) { X_Log.info(getClass(), "Loading package", file); Package p = GwtReflectJre.getPackage(file.getPackage(), cl); if (!finishedPackages.contains(p)) { gwtcService.addPackage(p, false); } } else { Class<?> clazz = cl.loadClass(file.getName()); X_Log.trace(getClass(), "Loaded class", clazz); if (!finishedClasses.contains(clazz)) { X_Log.info(getClass(), "Adding class", clazz); gwtcService.addClass(clazz); } } } catch (Exception e) { X_Log.warn(getClass(), "Error encountered trying to load class ", file.getName(), e); } } }
@Override @Nullable @IsLocked(ELockType.WRITE) public JAXBContext getValueToCache(@Nullable final JAXBContextCacheKey aCacheKey) { if (aCacheKey == null) return null; final Package aPackage = aCacheKey.getPackage(); final ClassLoader aClassLoader = aCacheKey.getClassLoader(); if (GlobalDebug.isDebugMode()) s_aLogger.info( "Creating JAXB context for package " + aPackage.getName() + (aClassLoader == null ? "" : " using ClassLoader " + aClassLoader)); try { // When using "-npa" on JAXB no package-info class is created! if (aPackage.getAnnotation(XmlSchema.class) == null && GenericReflection.getClassFromNameSafe(aPackage.getName() + ".ObjectFactory") == null) { s_aLogger.warn( "The package " + aPackage.getName() + " does not seem to be JAXB generated! Trying to create a JAXBContext anyway."); } return JAXBContext.newInstance(aPackage.getName(), aClassLoader); } catch (final JAXBException ex) { final String sMsg = "Failed to create JAXB context for package '" + aPackage.getName() + "'" + (aClassLoader == null ? "" : " using ClassLoader " + aClassLoader); s_aLogger.error(sMsg + ": " + ex.getMessage()); throw new IllegalArgumentException(sMsg, ex); } }
private void scanClass(Class<?> clazz) { GwtcUnit data = nodes.get(clazz); if (data == null) { Object ancestor = findAncestor(clazz); return; } if (data.gwtc == null) { return; } gwtcService.addClass(clazz); Object parent; if (data.isFindParent()) { parent = findAncestor(data.source); if (parent != null) { GwtcUnit parentNode = nodes.get(parent); data.setParent(parentNode); if (data.isFindAllParents()) { while (parent != null) { Object ancestor = findAncestor(parent); if (ancestor == null) { break; } else { GwtcUnit ancestorNode = nodes.get(ancestor); parentNode.setParent(ancestorNode); parent = ancestor; parentNode = ancestorNode; } } } } } Gwtc gwtc = clazz.getAnnotation(Gwtc.class); Class<?> c; parent = c = clazz; if (gwtc == null) { while (c != Object.class) { gwtc = c.getAnnotation(Gwtc.class); if (gwtc != null) { parent = c; maybeAddAncestors(gwtc, c); break; } c = c.getSuperclass(); } Package pkg; parent = pkg = clazz.getPackage(); if (gwtc == null) { gwtc = pkg.getAnnotation(Gwtc.class); String parentName = pkg.getName(); search: while (gwtc == null) { int ind = parentName.lastIndexOf('.'); if (ind == -1) { break; } parentName = parentName.substring(0, ind); pkg = GwtReflect.getPackage(parentName); while (pkg == null) { ind = parentName.lastIndexOf('.'); if (ind == -1) { X_Log.warn("No package found for ", clazz.getPackage(), "; aborting @Gwtc search"); break search; } parentName = parentName.substring(0, ind); pkg = GwtReflect.getPackage(parentName); } gwtc = pkg.getAnnotation(Gwtc.class); } if (gwtc != null) { parent = pkg; maybeAddAncestors(gwtc, pkg); } } else { maybeAddAncestors(gwtc, pkg); } } else { maybeAddAncestors(gwtc, c); inherit(data); } if (parent != null) { X_Log.trace(getClass(), "Next annotated parent of ", c, "is", parent); } }
static { myPackage = VersionAnnotation.class.getPackage(); version = myPackage.getAnnotation(VersionAnnotation.class); }
public GwtcUnit(Package from) { gwtc = from.getAnnotation(Gwtc.class); source = from; type = GwtcUnitType.Package; }
private void gatherInfo() { XmlAccessorType accessorType; rootElementName = null; XmlAccessType accessType = null; XmlRootElement rootE = (XmlRootElement) jaxbClass.getAnnotation(XmlRootElement.class); if (rootE != null) { rootElementName = rootE.name(); } xmlType = (XmlType) jaxbClass.getAnnotation(XmlType.class); accessorType = (XmlAccessorType) jaxbClass.getAnnotation(XmlAccessorType.class); if (accessorType == null) { Package pkg = jaxbClass.getPackage(); accessorType = (XmlAccessorType) pkg.getAnnotation(XmlAccessorType.class); } if (accessorType != null) { accessType = accessorType.value(); } if (accessType == null) { // Default value for JAXB accessType = XmlAccessType.PUBLIC_MEMBER; } Field fields[] = jaxbClass.getDeclaredFields(); for (Field field : fields) { XmlTransient xmlTransient = (XmlTransient) field.getAnnotation(XmlTransient.class); if (xmlTransient != null) { continue; } Annotation fAnnots[] = field.getAnnotations(); if ((fAnnots == null) || (fAnnots.length == 0)) { boolean autoFields = (accessType.equals(XmlAccessType.PUBLIC_MEMBER) || accessType.equals(XmlAccessType.FIELD)); if (!autoFields) { continue; } } processFieldRelatedAnnotations(fAnnots, field.getName(), field.getGenericType()); } Method methods[] = jaxbClass.getDeclaredMethods(); for (Method method : methods) { XmlTransient xmlTransient = (XmlTransient) method.getAnnotation(XmlTransient.class); if (xmlTransient != null) { continue; } if (!isGetterOrSetter(method)) { continue; } Annotation mAnnots[] = method.getAnnotations(); if ((mAnnots == null) || (mAnnots.length == 0)) { boolean autoGettersSetters = (accessType.equals(XmlAccessType.PUBLIC_MEMBER) || accessType.equals(XmlAccessType.PROPERTY)); if (!autoGettersSetters) { continue; } } processFieldRelatedAnnotations( mAnnots, guessFieldNameFromGetterOrSetter(method.getName()), method.getGenericReturnType()); } }