java.lang.Thread.getContextClassLoader()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.3k)|赞(0)|评价(0)|浏览(216)

本文整理了Java中java.lang.Thread.getContextClassLoader()方法的一些代码示例,展示了Thread.getContextClassLoader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Thread.getContextClassLoader()方法的具体详情如下:
包路径:java.lang.Thread
类名称:Thread
方法名:getContextClassLoader

Thread.getContextClassLoader介绍

[英]Returns the context ClassLoader for this Thread. The context ClassLoader is provided by the creator of the thread for use by code running in this thread when loading classes and resources. If not #setContextClassLoader, the default is the ClassLoader context of the parent Thread. The context ClassLoader of the primordial thread is typically set to the class loader used to load the application.

If a security manager is present, and the invoker's class loader is not null and is not the same as or an ancestor of the context class loader, then this method invokes the security manager's SecurityManager#checkPermission(java.security.Permission)method with a RuntimePermission("getClassLoader") permission to verify that retrieval of the context class loader is permitted.
[中]返回此线程的上下文类加载器。上下文类加载器由线程的创建者提供,供在加载类和资源时在此线程中运行的代码使用。如果不是#setContextClassLoader,则默认为父线程的ClassLoader上下文。原始线程的上下文类加载器通常设置为用于加载应用程序的类加载器。
如果存在安全管理器,并且调用方的类加载器不为null,并且与上下文类加载器不相同或不是上下文类加载器的祖先,然后,此方法使用RuntimePermission(“getClassLoader”)权限调用安全管理器的SecurityManager#checkPermission(java.security.Permission)方法,以验证是否允许检索上下文类加载器。

代码示例

代码示例来源:origin: apache/incubator-dubbo

@Override
public ClassLoader getClassLoader() {
  return Thread.currentThread().getContextClassLoader();
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public ClassLoader getClassLoader() {
  return Thread.currentThread().getContextClassLoader();
}

代码示例来源:origin: apache/incubator-dubbo

public CompactedObjectInputStream(InputStream in) throws IOException {
  this(in, Thread.currentThread().getContextClassLoader());
}

代码示例来源:origin: netty/netty

static ClassLoader getContextClassLoader() {
  if (System.getSecurityManager() == null) {
    return Thread.currentThread().getContextClassLoader();
  } else {
    return AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() {
      @Override
      public ClassLoader run() {
        return Thread.currentThread().getContextClassLoader();
      }
    });
  }
}

代码示例来源:origin: apache/incubator-dubbo

private static Class<?> arrayForName(String className) throws ClassNotFoundException {
  return Class.forName(className.endsWith("[]")
      ? "[L" + className.substring(0, className.length() - 2) + ";"
      : className, true, Thread.currentThread().getContextClassLoader());
}

代码示例来源:origin: apache/incubator-dubbo

private static Class<?> arrayForName(String className) throws ClassNotFoundException {
  return Class.forName(className.endsWith("[]")
      ? "[L" + className.substring(0, className.length() - 2) + ";"
      : className, true, Thread.currentThread().getContextClassLoader());
}

代码示例来源:origin: apache/incubator-dubbo

public static Object deserialize(JavaBeanDescriptor beanDescriptor) {
  Object result = deserialize(
      beanDescriptor,
      Thread.currentThread().getContextClassLoader());
  return result;
}

代码示例来源:origin: apache/incubator-dubbo

public static Object deserialize(JavaBeanDescriptor beanDescriptor) {
  Object result = deserialize(
      beanDescriptor,
      Thread.currentThread().getContextClassLoader());
  return result;
}

代码示例来源:origin: apache/incubator-dubbo

@Override
@SuppressWarnings("unchecked")
public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
  return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, new InvokerInvocationHandler(invoker));
}

代码示例来源:origin: apache/incubator-dubbo

@Override
@SuppressWarnings("unchecked")
public <T> T getProxy(Invoker<T> invoker, Class<?>[] interfaces) {
  return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), interfaces, new InvokerInvocationHandler(invoker));
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void contextInitialized(ServletContextEvent event) {
  CachedIntrospectionResults.acceptClassLoader(Thread.currentThread().getContextClassLoader());
}

代码示例来源:origin: apache/incubator-dubbo

public static Class<?> forNameWithThreadContextClassLoader(String name)
    throws ClassNotFoundException {
  return forName(name, Thread.currentThread().getContextClassLoader());
}

代码示例来源:origin: apache/incubator-dubbo

public static Class<?> forNameWithThreadContextClassLoader(String name)
    throws ClassNotFoundException {
  return forName(name, Thread.currentThread().getContextClassLoader());
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
  ClassLoader ocl = Thread.currentThread().getContextClassLoader();
  Thread.currentThread().setContextClassLoader(invoker.getInterface().getClassLoader());
  try {
    return invoker.invoke(invocation);
  } finally {
    Thread.currentThread().setContextClassLoader(ocl);
  }
}

代码示例来源:origin: apache/incubator-dubbo

@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
  ClassLoader ocl = Thread.currentThread().getContextClassLoader();
  Thread.currentThread().setContextClassLoader(invoker.getInterface().getClassLoader());
  try {
    return invoker.invoke(invocation);
  } finally {
    Thread.currentThread().setContextClassLoader(ocl);
  }
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public void contextDestroyed(ServletContextEvent event) {
  CachedIntrospectionResults.clearClassLoader(Thread.currentThread().getContextClassLoader());
  Introspector.flushCaches();
}

代码示例来源:origin: apache/incubator-dubbo

public static ClassGenerator newInstance() {
  return new ClassGenerator(getClassPool(Thread.currentThread().getContextClassLoader()));
}

代码示例来源:origin: apache/incubator-dubbo

public static ClassGenerator newInstance() {
  return new ClassGenerator(getClassPool(Thread.currentThread().getContextClassLoader()));
}

代码示例来源:origin: spring-projects/spring-framework

public static void setCurrentWebApplicationContext(WebApplicationContext applicationContext) {
  setCurrentWebApplicationContext(Thread.currentThread().getContextClassLoader(), applicationContext);
}

代码示例来源:origin: google/guava

public void testGetResource_contextClassLoaderNull() {
 ClassLoader oldContextLoader = Thread.currentThread().getContextClassLoader();
 try {
  Thread.currentThread().setContextClassLoader(null);
  assertNotNull(Resources.getResource("com/google/common/io/testdata/i18n.txt"));
  try {
   Resources.getResource("no such resource");
   fail("Should get IllegalArgumentException");
  } catch (IllegalArgumentException expected) {
  }
 } finally {
  Thread.currentThread().setContextClassLoader(oldContextLoader);
 }
}

相关文章