Exemple #1
1
  static JavaMembers lookupClass(
      Scriptable scope, Class<?> dynamicType, Class<?> staticType, boolean includeProtected) {
    JavaMembers members;
    ClassCache cache = ClassCache.get(scope);
    Map<Class<?>, JavaMembers> ct = cache.getClassCacheMap();

    Class<?> cl = dynamicType;
    for (; ; ) {
      members = ct.get(cl);
      if (members != null) {
        if (cl != dynamicType) {
          // member lookup for the original class failed because of
          // missing privileges, cache the result so we don't try
          // again
          ct.put(dynamicType, members);
        }
        return members;
      }
      try {
        members = new JavaMembers(cache.getAssociatedScope(), cl, includeProtected);
        break;
      } catch (SecurityException e) {
        // Reflection may fail for objects that are in a restricted
        // access package (e.g. sun.*). If we get a security
        // exception, try again with the static type if it is interface.
        // Otherwise, try superclass
        if (staticType != null && staticType.isInterface()) {
          cl = staticType;
          staticType = null; // try staticType only once
        } else {
          Class<?> parent = cl.getSuperclass();
          if (parent == null) {
            if (cl.isInterface()) {
              // last resort after failed staticType interface
              parent = ScriptRuntime.ObjectClass;
            } else {
              throw e;
            }
          }
          cl = parent;
        }
      }
    }

    if (cache.isCachingEnabled()) {
      ct.put(cl, members);
      if (cl != dynamicType) {
        // member lookup for the original class failed because of
        // missing privileges, cache the result so we don't try again
        ct.put(dynamicType, members);
      }
    }
    return members;
  }