private static boolean lookForInterface( java.lang.Class<?> klass, String className, Set<java.lang.Class<?>> alreadyVisited) { if (klass.getName().equals(className)) return true; // did we already visit this type? if (!alreadyVisited.add(klass)) return false; // first see if it satisfies it directly SatisfiedTypes satisfiesAnnotation = klass.getAnnotation(SatisfiedTypes.class); if (satisfiesAnnotation != null) { for (String satisfiedType : satisfiesAnnotation.value()) { satisfiedType = declClassName(satisfiedType); int i = satisfiedType.indexOf('<'); if (i > 0) { satisfiedType = satisfiedType.substring(0, i); } try { if (lookForInterface( java.lang.Class.forName(satisfiedType, true, klass.getClassLoader()), className, alreadyVisited)) { return true; } } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } } // now look at this class's interfaces for (java.lang.Class<?> intrface : klass.getInterfaces()) { if (lookForInterface(intrface, className, alreadyVisited)) return true; } // no luck return false; }
private static boolean classExtendsClass(java.lang.Class<?> klass, String className) { if (klass == null) return false; if (klass.getName().equals(className)) return true; if ((className.equals("ceylon.language.Basic")) && klass != java.lang.Object.class && !isSubclassOfString(klass) // && klass!=java.lang.String.class && !klass.isAnnotationPresent(Class.class) && (!klass.isInterface() || !klass.isAnnotationPresent(Ceylon.class))) { // TODO: this is broken for a Java class that // extends a Ceylon class return true; } Class classAnnotation = klass.getAnnotation(Class.class); if (classAnnotation != null) { String superclassName = declClassName(classAnnotation.extendsType()); int i = superclassName.indexOf('<'); if (i > 0) { superclassName = superclassName.substring(0, i); } if (superclassName.isEmpty()) { return false; } try { return classExtendsClass( java.lang.Class.forName(superclassName, true, klass.getClassLoader()), className); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } return classExtendsClass(klass.getSuperclass(), className); }
/** * Get the package for the specified class. The class's class loader is used to find the package * instance corresponding to the specified class. If the class loader is the bootstrap class * loader, which may be represented by {@code null} in some implementations, then the set of * packages loaded by the bootstrap class loader is searched to find the package. * * <p>Packages have attributes for versions and specifications only if the class loader created * the package instance with the appropriate attributes. Typically those attributes are defined in * the manifests that accompany the classes. * * @param class the class to get the package of. * @return the package of the class. It may be null if no package information is available from * the archive or codebase. */ static Package getPackage(Class<?> c) { String name = c.getName(); int i = name.lastIndexOf('.'); if (i != -1) { name = name.substring(0, i); ClassLoader cl = c.getClassLoader(); if (cl != null) { return cl.getPackage(name); } else { return getSystemPackage(name); } } else { return null; } }
private static boolean classSatisfiesInterface( java.lang.Class<?> klass, String className, Set<java.lang.Class<?>> alreadyVisited) { if (klass == null || klass == ceylon.language.Anything.class) return false; if ((className.equals("ceylon.language.Identifiable")) && klass != java.lang.Object.class // && klass!=java.lang.String.class && !klass.isAnnotationPresent(Ceylon.class)) { // TODO: this is broken for a Java class that // extends a Ceylon class return true; } if (className.equals("ceylon.language.Identifiable") && isSubclassOfString(klass)) { return false; } // try the interfaces if (lookForInterface(klass, className, alreadyVisited)) return true; // try its superclass Class classAnnotation = klass.getAnnotation(Class.class); String superclassName; if (classAnnotation != null) { superclassName = declClassName(classAnnotation.extendsType()); int i = superclassName.indexOf('<'); if (i > 0) { superclassName = superclassName.substring(0, i); } } else { // Maybe the class didn't have an extends, so implictly Basic superclassName = "ceylon.language.Basic"; } if (!superclassName.isEmpty()) { try { return classSatisfiesInterface( java.lang.Class.forName(superclassName, true, klass.getClassLoader()), className, alreadyVisited); } catch (ClassNotFoundException e) { throw new RuntimeException(e); } } return classSatisfiesInterface(klass.getSuperclass(), className, alreadyVisited); }