Search in sources :

Example 1 with Constructor

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);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method)

Example 2 with Constructor

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);
}
Also used : GenericArrayType(java.lang.reflect.GenericArrayType) WildcardType(java.lang.reflect.WildcardType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) Constructor(java.lang.reflect.Constructor) Method(java.lang.reflect.Method)

Example 3 with Constructor

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;
        }
    }
}
Also used : Constructor(java.lang.reflect.Constructor) SparkConf(org.apache.spark.SparkConf) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with Constructor

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;
}
Also used : Constructor(java.lang.reflect.Constructor)

Example 5 with Constructor

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);
}
Also used : Constructor(java.lang.reflect.Constructor)

Aggregations

Constructor (java.lang.reflect.Constructor)1311 InvocationTargetException (java.lang.reflect.InvocationTargetException)281 Method (java.lang.reflect.Method)252 IOException (java.io.IOException)128 Field (java.lang.reflect.Field)111 ArrayList (java.util.ArrayList)106 Test (org.junit.Test)92 DOMTestDocumentBuilderFactory (org.w3c.domts.DOMTestDocumentBuilderFactory)74 JUnitTestSuiteAdapter (org.w3c.domts.JUnitTestSuiteAdapter)73 List (java.util.List)61 JAXPDOMTestDocumentBuilderFactory (org.w3c.domts.JAXPDOMTestDocumentBuilderFactory)58 Map (java.util.Map)50 Type (java.lang.reflect.Type)39 Annotation (java.lang.annotation.Annotation)38 HashMap (java.util.HashMap)38 HashSet (java.util.HashSet)31 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)31 ParameterizedType (java.lang.reflect.ParameterizedType)30 File (java.io.File)20 URL (java.net.URL)20