美文网首页
反射基类

反射基类

作者: 王岩_shang | 来源:发表于2017-07-17 16:52 被阅读20次
/*
 * Copyright (C) 2012 Square, Inc.
 * Copyright (C) 2013 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dagger.internal;

import java.lang.reflect.AccessibleObject;


/**
 * Provides a point of configuration of the basic resolving functions within Dagger, namely
 * that of Module handling, injection binding creation, and static injection.  A plugin must
 * provide all resolution methods
 */
public abstract class Loader {
  private final Memoizer<ClassLoader, Memoizer<String, Class<?>>> caches =
      new Memoizer<ClassLoader, Memoizer<String, Class<?>>>() {
        @Override protected Memoizer<String, Class<?>> create(final ClassLoader classLoader) {
          return new Memoizer<String, Class<?>>() {
            @Override protected Class<?> create(String className) {
              try {
                return classLoader.loadClass(className);
              } catch (ClassNotFoundException e) {
                return Void.class; // Cache the failure (negative case).
              }
            }
          };
        }
      };

  /**
   * Returns a binding that uses {@code @Inject} annotations, or null if no valid binding can
   * be found or created.
   */
  public abstract Binding<?> getAtInjectBinding(
      String key, String className, ClassLoader classLoader, boolean mustHaveInjections);

  /**
   * Returns a module adapter for {@code moduleClass} or throws a {@code TypeNotPresentException} if
   * none can be found.
   */
  public abstract <T> ModuleAdapter<T> getModuleAdapter(Class<T> moduleClass);

  /**
   * Returns the static injection for {@code injectedClass}.
   */
  public abstract StaticInjection getStaticInjection(Class<?> injectedClass);

  /**
   * Loads a class from a {@code ClassLoader}-specific cache if it's already there, or
   * loads it from the given {@code ClassLoader} and caching it for future requests.  Failures
   * to load are also cached using the Void.class type.  A null {@code ClassLoader} is assumed
   * to be the system classloader.
   */
  protected Class<?> loadClass(ClassLoader classLoader, String name) {
    // A null classloader is the system classloader.
    classLoader = (classLoader != null) ? classLoader : ClassLoader.getSystemClassLoader();
    return caches.get(classLoader).get(name);
  }

  /**
   * Instantiates a class using its default constructor and the given {@link ClassLoader}. This
   * method does not attempt to {@linkplain AccessibleObject#setAccessible set accessibility}.
   */
  protected <T> T instantiate(String name, ClassLoader classLoader) {
    try {
      Class<?> generatedClass = loadClass(classLoader, name);
      if (generatedClass == Void.class) {
        return null;
      }
      @SuppressWarnings("unchecked")
      T instance = (T) generatedClass.newInstance();
      return instance;
    } catch (InstantiationException e) {
      throw new RuntimeException("Failed to initialize " + name, e);
    } catch (IllegalAccessException e) {
      throw new RuntimeException("Failed to initialize " + name, e);
    }
  }

}

相关文章

  • 反射基类

  • 玩转java反射

    原文 类加载器什么是类加载器类加载器的职责类加载器的组成 反射什么是反射如何使用反射反射在Android中的应用 ...

  • 2020-07-04【反射】

    类加载 类加载器 反射概述 获取Class类的对象 反射获取构造方法并使用 反射获取成员遍历并使用 反射获取成员方...

  • Java 子类反射笔记

    当用反射获取子类重写的父类方法时,如果多个子类对父类进行了重写,且修改了参数类型或返回值类型,如果获取的不是基类方...

  • Dart基础4-反射

    mirror 1. 反射类 ClassMirror 1.1 获取反射类对象 Type 类型, 即 class类的类...

  • 反射

    实体类 反射获取实例 反射获取方法 反射获取属性 补充 testBean.getClasses()返回调用类的所有...

  • Java学习笔记 27 - 类的加载器、反射

    本文内容介绍1、类加载器2、反射构造方法3、反射成员变量4、反射成员方法5、反射配置文件运行类中的方法 01类加载...

  • PHP Reflection 反射

    反射 ReflectionClass 类 PHP反射机制 反射在 PHP 中的应用

  • JAVA基础之反射

    1.通过反射获取类获取反射对象(反射入口):Class 1.Class.forName(全类名) 2.类名.cla...

  • Servlet学习:Servlet部分优化

    将多个Servlet优化成一个,在基类的service方法中利用反射做转发。 当然也可以在本类的service中做...

网友评论

      本文标题:反射基类

      本文链接:https://www.haomeiwen.com/subject/enzfkxtx.html