ClassValue in Java 7

后端 未结 4 1829
南笙
南笙 2020-12-29 01:37

While browsing the Java 7 API documentation I stumbled upon the new class java.lang.ClassValue with the following rather minimal documentation:

Lazily

4条回答
  •  灰色年华
    2020-12-29 02:15

    ClassValue cache something about the class.

    Here is a part of code (at Lucene 5.0 AttributeSource.java):

    /** a cache that stores all interfaces for known implementation classes for performance (slow reflection) */
    private static final ClassValue[]> implInterfaces = new ClassValue[]>() {
        @Override
        protected Class[] computeValue(Class clazz) {
          final Set> intfSet = new LinkedHashSet<>();
          // find all interfaces that this attribute instance implements
          // and that extend the Attribute interface
          do {
            for (Class curInterface : clazz.getInterfaces()) {
              if (curInterface != Attribute.class && Attribute.class.isAssignableFrom(curInterface)) {
                intfSet.add(curInterface.asSubclass(Attribute.class));
              }
            }
            clazz = clazz.getSuperclass();
          } while (clazz != null);
          @SuppressWarnings({"unchecked", "rawtypes"}) final Class[] a =
              intfSet.toArray(new Class[intfSet.size()]);
          return a;
        }
    };
    

提交回复
热议问题