本文整理了Java中java.lang.Class.newInstance()
方法的一些代码示例,展示了Class.newInstance()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Class.newInstance()
方法的具体详情如下:
包路径:java.lang.Class
类名称:Class
方法名:newInstance
[英]Returns a new instance of the class represented by this Class, created by invoking the default (that is, zero-argument) constructor. If there is no such constructor, or if the creation fails (either because of a lack of available memory or because an exception is thrown by the constructor), an InstantiationException is thrown. If the default constructor exists but is not accessible from the context where this method is invoked, an IllegalAccessException is thrown.
[中]返回该类表示的类的新实例,该实例是通过调用默认(即零参数)构造函数创建的。如果没有这样的构造函数,或者创建失败(因为缺少可用内存或构造函数引发异常),则会引发InstanceionException。如果默认构造函数存在,但无法从调用此方法的上下文中访问,则会引发IllegalAccessException。
代码示例来源:origin: greenrobot/EventBus
@Override
public SubscriberInfo getSuperSubscriberInfo() {
if(superSubscriberInfoClass == null) {
return null;
}
try {
return superSubscriberInfoClass.newInstance();
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
代码示例来源:origin: spring-projects/spring-framework
@Override
public Object run() throws Exception {
return cl.newInstance();
}
}, acc);
代码示例来源:origin: apache/incubator-dubbo
public static Object newInstance(String name) {
try {
return forName(name).newInstance();
} catch (InstantiationException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
代码示例来源:origin: apache/incubator-dubbo
protected Object instantiate()
throws Exception {
try {
if (_constructor != null)
return _constructor.newInstance(_constructorArgs);
else
return _type.newInstance();
} catch (Exception e) {
throw new HessianProtocolException("'" + _type.getName() + "' could not be instantiated", e);
}
}
代码示例来源:origin: apache/incubator-dubbo
public static Object newInstance(String name) {
try {
return forName(name).newInstance();
} catch (InstantiationException e) {
throw new IllegalStateException(e.getMessage(), e);
} catch (IllegalAccessException e) {
throw new IllegalStateException(e.getMessage(), e);
}
}
代码示例来源:origin: alibaba/druid
public void setStatLoggerClassName(String className) {
Class<?> clazz;
try {
clazz = Class.forName(className);
DruidDataSourceStatLogger statLogger = (DruidDataSourceStatLogger) clazz.newInstance();
this.setStatLogger(statLogger);
} catch (Exception e) {
throw new IllegalArgumentException(className, e);
}
}
代码示例来源:origin: apache/incubator-dubbo
@SuppressWarnings("unchecked")
private T createAdaptiveExtension() {
try {
return injectExtension((T) getAdaptiveExtensionClass().newInstance());
} catch (Exception e) {
throw new IllegalStateException("Can't create adaptive extension " + type + ", cause: " + e.getMessage(), e);
}
}
代码示例来源:origin: apache/incubator-dubbo
@SuppressWarnings("unchecked")
private T createAdaptiveExtension() {
try {
return injectExtension((T) getAdaptiveExtensionClass().newInstance());
} catch (Exception e) {
throw new IllegalStateException("Can't create adaptive extension " + type + ", cause: " + e.getMessage(), e);
}
}
代码示例来源:origin: iluwatar/java-design-patterns
private Command getCommand(String request) {
Class<?> commandClass = getCommandClass(request);
try {
return (Command) commandClass.newInstance();
} catch (Exception e) {
throw new ApplicationException(e);
}
}
代码示例来源:origin: libgdx/libgdx
public static GdxTest newTest (String testName) {
testName = originalToObfuscated.get(testName, testName);
try {
return forName(testName).newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}
代码示例来源:origin: libgdx/libgdx
/** Creates a new instance of the class represented by the supplied Class. */
static public <T> T newInstance (Class<T> c) throws ReflectionException {
try {
return c.newInstance();
} catch (InstantiationException e) {
throw new ReflectionException("Could not instantiate instance of class: " + c.getName(), e);
} catch (IllegalAccessException e) {
throw new ReflectionException("Could not instantiate instance of class: " + c.getName(), e);
}
}
代码示例来源:origin: spring-projects/spring-framework
@Bean
@Lazy
public Object notLoadable() throws Exception {
return Class.forName("does.not.exist").newInstance();
}
}
代码示例来源:origin: alibaba/fastjson
public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
PropertyProcessable processable;
try {
processable = this.type.newInstance();
} catch (Exception e) {
throw new JSONException("craete instance error");
}
Object object =parser.parse(processable, fieldName);
return (T) object;
}
代码示例来源:origin: alibaba/druid
public void setPasswordCallbackClassName(String passwordCallbackClassName) throws Exception {
Class<?> clazz = Utils.loadClass(passwordCallbackClassName);
if (clazz != null) {
this.passwordCallback = (PasswordCallback) clazz.newInstance();
} else {
LOG.error("load passwordCallback error : " + passwordCallbackClassName);
this.passwordCallback = null;
}
}
代码示例来源:origin: alibaba/druid
public void setValidConnectionCheckerClassName(String validConnectionCheckerClass) throws Exception {
Class<?> clazz = Utils.loadClass(validConnectionCheckerClass);
ValidConnectionChecker validConnectionChecker = null;
if (clazz != null) {
validConnectionChecker = (ValidConnectionChecker) clazz.newInstance();
this.validConnectionChecker = validConnectionChecker;
} else {
LOG.error("load validConnectionCheckerClass error : " + validConnectionCheckerClass);
}
}
代码示例来源:origin: google/guava
private void runTestMethod(ClassLoader classLoader) throws Exception {
Class<?> test = classLoader.loadClass(FuturesTest.class.getName());
Object testInstance = test.newInstance();
test.getMethod("setUp").invoke(testInstance);
test.getMethod(getName()).invoke(testInstance);
test.getMethod("tearDown").invoke(testInstance);
}
代码示例来源:origin: google/guava
private void runTestMethod(ClassLoader classLoader) throws Exception {
Class<?> test = classLoader.loadClass(AbstractFutureTest.class.getName());
test.getMethod(getName()).invoke(test.newInstance());
}
代码示例来源:origin: alibaba/druid
public Object createInstance(BeanInfo beanInfo) {
try {
return beanInfo.getClazz().newInstance();
} catch (InstantiationException ex) {
throw new DruidRuntimeException("create instance error", ex);
} catch (IllegalAccessException ex) {
throw new DruidRuntimeException("create instance error", ex);
}
}
代码示例来源:origin: skylot/jadx
private void makeInstance() throws Exception {
String fullName = clsNode.getFullName();
instance = getClassLoader().loadClass(fullName).newInstance();
if (instance == null) {
throw new NullPointerException("Instantiation failed");
}
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void testManualProxyJavaWithStaticPointcutAndTwoClassLoaders() throws Exception {
LogUserAdvice logAdvice = new LogUserAdvice();
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression(String.format("execution(* %s.TestService.*(..))", getClass().getName()));
// Test with default class loader first...
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, new TestServiceImpl(), "TestServiceImpl");
// Then try again with a different class loader on the target...
SimpleThrowawayClassLoader loader = new SimpleThrowawayClassLoader(new TestServiceImpl().getClass().getClassLoader());
// Make sure the interface is loaded from the parent class loader
loader.excludeClass(TestService.class.getName());
loader.excludeClass(TestException.class.getName());
TestService other = (TestService) loader.loadClass(TestServiceImpl.class.getName()).newInstance();
testAdvice(new DefaultPointcutAdvisor(pointcut, logAdvice), logAdvice, other, "TestServiceImpl");
}
内容来源于网络,如有侵权,请联系作者删除!