java.lang.Class.forName()方法的使用及代码示例

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

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

Class.forName介绍

[英]Returns a Class object which represents the class with the given name. The name should be the name of a non-primitive class, as described in the Class. Primitive types can not be found using this method; use int.class or Integer.TYPE instead.

If the class has not yet been loaded, it is loaded and initialized first. This is done through either the class loader of the calling class or one of its parent class loaders. It is possible that a static initializer is run as a result of this call.
[中]返回表示具有给定名称的类的类对象。名称应为非基本类的名称,如类中所述。使用此方法找不到基元类型;使用int.class或Integer。改为打字。
如果尚未加载该类,则首先加载并初始化该类。这是通过调用类的类装入器或其父类装入器之一完成的。此调用可能导致运行静态初始值设定项。

代码示例

代码示例来源:origin: iluwatar/java-design-patterns

private static Class<?> getCommandClass(String request) {
  Class<?> result;
  try {
   result = Class.forName("com.iluwatar.front.controller." + request + "Command");
  } catch (ClassNotFoundException e) {
   result = UnknownCommand.class;
  }
  return result;
 }
}

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

private Object readResolve() {

    try {
      Class c = Class.forName("java.time.OffsetDateTime");
      Method m = c.getDeclaredMethod("of", Class.forName("java.time.LocalDateTime"),
          Class.forName("java.time.ZoneOffset"));
      return m.invoke(null, dateTime, offset);
    } catch (Throwable t) {
      // ignore
    }
    return null;
  }
}

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

@Override
 public Class<?> loadFinalizer() {
  try {
   return Class.forName(FINALIZER_CLASS_NAME);
  } catch (ClassNotFoundException e) {
   throw new AssertionError(e);
  }
 }
}

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

private static boolean isPresent(String className) {
  try {
    Class.forName(className, false, LogAdapter.class.getClassLoader());
    return true;
  }
  catch (ClassNotFoundException ex) {
    return false;
  }
}

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

private Object readResolve() {
    try {
      Class c = Class.forName("java.time.Instant");
      Method m = c.getDeclaredMethod("ofEpochSecond", long.class, long.class);
      return m.invoke(null, seconds, nanos);
    } catch (Throwable t) {
      // ignore
    }
    return null;
  }
}

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

private Object readResolve() {
    try {
      Class c = Class.forName("java.time.Period");
      Method m = c.getDeclaredMethod("of", int.class, int.class, int.class);
      return m.invoke(null, years, months, days);
    } catch (Throwable t) {
      // ignore
    }
    return null;
  }
}

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

public YearHandle(Object o) {
  try {
    Class c = Class.forName("java.time.Year");
    Method m = c.getDeclaredMethod("getValue");
    this.year = (Integer) m.invoke(o);
  } catch (Throwable t) {
    // ignore
  }
}

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

private Object readResolve() {
    try {
      Class c = Class.forName("java.time.Year");
      Method m = c.getDeclaredMethod("of", int.class);
      return m.invoke(null, year);
    } catch (Throwable t) {
      // ignore
    }
    return null;
  }
}

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

public InstantHandle(Object o) {
  try {
    Class c = Class.forName("java.time.Instant");
    Method m = c.getDeclaredMethod("getEpochSecond");
    this.seconds = (Long) m.invoke(o);
    m = c.getDeclaredMethod("getNano");
    this.nanos = (Integer) m.invoke(o);
  } catch (Throwable t) {
    // ignore
  }
}

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

public DurationHandle(Object o) {
  try {
    Class c = Class.forName("java.time.Duration");
    Method m = c.getDeclaredMethod("getSeconds");
    this.seconds = (Long) m.invoke(o);
    m = c.getDeclaredMethod("getNano");
    this.nanos = (Integer) m.invoke(o);
  } catch (Throwable t) {
    // ignore
  }
}

代码示例来源:origin: square/okhttp

public CertificateChainCleaner buildCertificateChainCleaner(X509TrustManager trustManager) {
 try {
  Class<?> extensionsClass = Class.forName("android.net.http.X509TrustManagerExtensions");
  Constructor<?> constructor = extensionsClass.getConstructor(X509TrustManager.class);
  Object extensions = constructor.newInstance(trustManager);
  Method checkServerTrusted = extensionsClass.getMethod(
    "checkServerTrusted", X509Certificate[].class, String.class, String.class);
  return new AndroidCertificateChainCleaner(extensions, checkServerTrusted);
 } catch (Exception e) {
  throw new AssertionError(e);
 }
}

代码示例来源:origin: bumptech/glide

static ClassName checkResult() {
 try {
  Class.forName(ANDROIDX_CHECK_RESULT_ANNOTATION.reflectionName());
  return ANDROIDX_CHECK_RESULT_ANNOTATION;
 } catch (ClassNotFoundException e) {
  return CHECK_RESULT_ANNOTATION;
 }
}

代码示例来源:origin: bumptech/glide

static ClassName nonNull() {
 try {
  Class.forName(ANDROIDX_NONNULL_ANNOTATION.reflectionName());
  return ANDROIDX_NONNULL_ANNOTATION;
 } catch (ClassNotFoundException e) {
  return NONNULL_ANNOTATION;
 }
}

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

private static boolean isZoneId(Class cl) {
  try {
    return isJava8() && Class.forName("java.time.ZoneId").isAssignableFrom(cl);
  } catch (ClassNotFoundException e) {
    // ignore
  }
  return false;
}

代码示例来源:origin: square/okhttp

@Override public boolean isCleartextTrafficPermitted(String hostname) {
 try {
  Class<?> networkPolicyClass = Class.forName("android.security.NetworkSecurityPolicy");
  Method getInstanceMethod = networkPolicyClass.getMethod("getInstance");
  Object networkSecurityPolicy = getInstanceMethod.invoke(null);
  return api24IsCleartextTrafficPermitted(hostname, networkPolicyClass, networkSecurityPolicy);
 } catch (ClassNotFoundException | NoSuchMethodException e) {
  return super.isCleartextTrafficPermitted(hostname);
 } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
  throw new AssertionError("unable to determine cleartext support", e);
 }
}

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

private static Method getJdkNextDown() throws Exception {
 try {
  return Math.class.getMethod("nextDown", double.class);
 } catch (NoSuchMethodException expectedBeforeJava8) {
  return Class.forName("sun.misc.FpUtils").getMethod("nextDown", double.class);
 }
}

代码示例来源:origin: square/okhttp

public static ConscryptPlatform buildIfSupported() {
 try {
  // Trigger an early exception over a fatal error, prefer a RuntimeException over Error.
  Class.forName("org.conscrypt.Conscrypt");
  if (!Conscrypt.isAvailable()) {
   return null;
  }
  return new ConscryptPlatform();
 } catch (ClassNotFoundException e) {
  return null;
 }
}

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

@Bean
  @Lazy
  public Object notLoadable() throws Exception {
    return Class.forName("does.not.exist").newInstance();
  }
}

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

@Test
public void testCollectionsEmptyList() throws Exception {
  CollectionToCollectionConverter converter = new CollectionToCollectionConverter(new GenericConversionService());
  TypeDescriptor type = new TypeDescriptor(getClass().getField("list"));
  converter.convert(list, type, TypeDescriptor.valueOf(Class.forName("java.util.Collections$EmptyList")));
}

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

public void testLexicographicalComparatorChoice() throws Exception {
 Comparator<byte[]> defaultComparator = UnsignedBytes.lexicographicalComparator();
 assertNotNull(defaultComparator);
 assertSame(defaultComparator, UnsignedBytes.lexicographicalComparator());
 if (unsafeComparatorAvailable()) {
  assertSame(defaultComparator.getClass(), Class.forName(unsafeComparatorClassName()));
 } else {
  assertSame(defaultComparator, UnsignedBytes.lexicographicalComparatorJavaImpl());
 }
}

相关文章

Class类方法