use of java.lang.reflect.Constructor in project elasticsearch by elastic.
the class TypeLiteral method getExceptionTypes.
/**
* Returns the resolved generic exception types thrown by {@code constructor}.
*
* @param methodOrConstructor a method or constructor defined by this or any supertype.
* @since 2.0
*/
public List<TypeLiteral<?>> getExceptionTypes(Member methodOrConstructor) {
Type[] genericExceptionTypes;
if (methodOrConstructor instanceof Method) {
Method method = (Method) methodOrConstructor;
if (!method.getDeclaringClass().isAssignableFrom(rawType)) {
throw new IllegalArgumentException(method + " is not defined by a supertype of " + type);
}
genericExceptionTypes = method.getGenericExceptionTypes();
} else if (methodOrConstructor instanceof Constructor) {
Constructor<?> constructor = (Constructor<?>) methodOrConstructor;
if (!constructor.getDeclaringClass().isAssignableFrom(rawType)) {
throw new IllegalArgumentException(constructor + " does not construct a supertype of " + type);
}
genericExceptionTypes = constructor.getGenericExceptionTypes();
} else {
throw new IllegalArgumentException("Not a method or a constructor: " + methodOrConstructor);
}
return resolveAll(genericExceptionTypes);
}
use of java.lang.reflect.Constructor in project elasticsearch by elastic.
the class TypeLiteral method getParameterTypes.
/**
* Returns the resolved generic parameter types of {@code methodOrConstructor}.
*
* @param methodOrConstructor a method or constructor defined by this or any supertype.
* @since 2.0
*/
public List<TypeLiteral<?>> getParameterTypes(Member methodOrConstructor) {
Type[] genericParameterTypes;
if (methodOrConstructor instanceof Method) {
Method method = (Method) methodOrConstructor;
if (!method.getDeclaringClass().isAssignableFrom(rawType)) {
throw new IllegalArgumentException(method + " is not defined by a supertype of " + type);
}
genericParameterTypes = method.getGenericParameterTypes();
} else if (methodOrConstructor instanceof Constructor) {
Constructor constructor = (Constructor) methodOrConstructor;
if (!constructor.getDeclaringClass().isAssignableFrom(rawType)) {
throw new IllegalArgumentException(constructor + " does not construct a supertype of " + type);
}
genericParameterTypes = constructor.getGenericParameterTypes();
} else {
throw new IllegalArgumentException("Not a method or a constructor: " + methodOrConstructor);
}
return resolveAll(genericParameterTypes);
}
use of java.lang.reflect.Constructor in project zeppelin by apache.
the class SparkInterpreter method createHttpServer.
private Object createHttpServer(File outputDir) {
SparkConf conf = new SparkConf();
try {
// try to create HttpServer
Constructor<?> constructor = getClass().getClassLoader().loadClass("org.apache.spark.HttpServer").getConstructor(new Class[] { SparkConf.class, File.class, SecurityManager.class, int.class, String.class });
Object securityManager = createSecurityManager(conf);
return constructor.newInstance(new Object[] { conf, outputDir, securityManager, 0, "HTTP Server" });
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
// fallback to old constructor
Constructor<?> constructor = null;
try {
constructor = getClass().getClassLoader().loadClass("org.apache.spark.HttpServer").getConstructor(new Class[] { File.class, SecurityManager.class, int.class, String.class });
return constructor.newInstance(new Object[] { outputDir, createSecurityManager(conf), 0, "HTTP Server" });
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e1) {
logger.error(e1.getMessage(), e1);
return null;
}
}
}
use of java.lang.reflect.Constructor in project Openfire by igniterealtime.
the class TickerFactory method createTicker.
public Ticker createTicker(String tickerClassName, String id) throws TickerException {
Ticker ticker = null;
if (tickerClassName != null && tickerClassName.length() > 0) {
try {
Class tickerClass = Class.forName(tickerClassName);
Class[] params = new Class[] { String.class };
Constructor constructor = tickerClass.getConstructor(params);
if (constructor != null) {
Object[] args = new Object[] { new String("TickerTest") };
ticker = (Ticker) (constructor.newInstance(args));
} else {
Logger.println(id + ": Unable to find constructor for class " + tickerClassName);
}
} catch (Exception e) {
Logger.println(id + ": Unable to create ticker for class " + tickerClassName + " " + e.getMessage());
}
}
if (ticker == null) {
ticker = new TickerSleep(id);
if (Logger.logLevel >= Logger.LOG_MOREINFO) {
Logger.println(id + ": using default ticker TickerSleep");
}
}
return ticker;
}
use of java.lang.reflect.Constructor in project jmonkeyengine by jMonkeyEngine.
the class FieldSerializer method checkClass.
protected void checkClass(Class clazz) {
// See if the class has a public no-arg constructor
try {
savedCtors.put(clazz, clazz.getConstructor());
return;
} catch (NoSuchMethodException e) {
//throw new RuntimeException( "Registration error: no-argument constructor not found on:" + clazz );
}
// See if it has a non-public no-arg constructor
try {
Constructor ctor = clazz.getDeclaredConstructor();
// Make sure we can call it later.
ctor.setAccessible(true);
savedCtors.put(clazz, ctor);
return;
} catch (NoSuchMethodException e) {
}
throw new RuntimeException("Registration error: no-argument constructor not found on:" + clazz);
}
Aggregations