Example usage for java.lang Class getConstructor

List of usage examples for java.lang Class getConstructor

Introduction

In this page you can find the example usage for java.lang Class getConstructor.

Prototype

@CallerSensitive
public Constructor<T> getConstructor(Class<?>... parameterTypes)
        throws NoSuchMethodException, SecurityException 

Source Link

Document

Returns a Constructor object that reflects the specified public constructor of the class represented by this Class object.

Usage

From source file:Main.java

public static List<String> getDexEntries() {
    try {/*from   w w w  .  j  a  va  2  s.co  m*/
        Object app = Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null);
        Object codePath = Class.forName("android.app.Application").getMethod("getPackageCodePath").invoke(app);
        Class<?> dexFileClass = Class.forName("dalvik.system.DexFile");
        Object dexFile = dexFileClass.getConstructor(String.class).newInstance(codePath);
        Enumeration<String> entries = (Enumeration<String>) dexFileClass.getMethod("entries").invoke(dexFile);
        List<String> dexEntries = new LinkedList<String>();
        while (entries.hasMoreElements()) {
            String entry = entries.nextElement();
            entry = entry.replace('.', '/') + ".class";
            dexEntries.add(entry);
        }
        dexFileClass.getMethod("close").invoke(dexFile);
        return dexEntries;
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    } catch (IllegalArgumentException e) {
        throw new RuntimeException(e);
    } catch (InvocationTargetException e) {
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException(e);
    } catch (SecurityException e) {
        throw new RuntimeException(e);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    } catch (InstantiationException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static Map<String, Object> getProperties(Element root) {
    Map<String, Object> map = new HashMap<String, Object>();
    Element[] list = getChildrenByName(root, "property");
    for (int i = 0; i < list.length; i++) {
        String name = list[i].getAttribute("name");
        String type = list[i].getAttribute("type");
        String valueString = getText(list[i]);
        try {//  w ww.j  a  v  a2  s .c o m
            Class<?> cls = Class.forName(type);
            Constructor<?> con = cls.getConstructor(new Class<?>[] { String.class

            });
            Object value = con.newInstance(new Object[] { valueString

            });
            map.put(name, value);
        } catch (Exception e) {
            System.err.println("Unable to parse property '" + name +

                    "'='" + valueString + "': " + e.toString());
        }
    }
    return map;
}

From source file:Main.java

private static <T> T newInstance(Context context, Class clazz, Class[] constructorSig, Object... arguments) {
    try {/* w  w w  .j a va  2  s  . c o m*/
        Constructor<?> constructor = clazz.getConstructor(constructorSig);
        return (T) constructor.newInstance(arguments);
    } catch (Exception e) {
        Log.w(LOG_TAG, "Cannot instantiate class: " + clazz.getName(), e);
    }
    return null;
}

From source file:com.espertech.esper.util.ConstructorHelper.java

private static Constructor getObjectArrayConstructor(Class clazz) throws NoSuchMethodException {
    return clazz.getConstructor(EMPTY_OBJECT_ARRAY_TYPE);
}

From source file:Main.java

public static Map<String, Object> getProperties(Element paramElement) {
    HashMap<String, Object> localHashMap = new HashMap<String, Object>();
    Element[] arrayOfElement = getChildrenByName(paramElement, "property");
    for (int i = 0; i < arrayOfElement.length; ++i) {
        String str1 = arrayOfElement[i].getAttribute("name");
        String str2 = arrayOfElement[i].getAttribute("type");
        String str3 = getText(arrayOfElement[i]);
        try {/*from ww  w  .  j  av a  2  s  . co m*/
            Class<?> localClass = Class.forName(str2);
            Constructor<?> localConstructor = localClass.getConstructor(new Class[] { String.class });
            Object localObject = localConstructor.newInstance(new Object[] { str3 });
            localHashMap.put(str1, localObject);
        } catch (Exception localException) {
            System.err.println(
                    "Unable to parse property '" + str1 + "'='" + str3 + "': " + localException.toString());
        }
    }
    return localHashMap;
}

From source file:com.github.fge.jsonschema.library.validator.DraftV3ValidatorDictionary.java

private static Constructor<? extends KeywordValidator> constructor(final Class<? extends KeywordValidator> c) {
    try {// w  w  w. j  a  v  a 2 s .co m
        return c.getConstructor(JsonNode.class);
    } catch (NoSuchMethodException e) {
        throw new RuntimeException("No appropriate constructor found", e);
    }
}

From source file:com.ibatis.common.logging.LogFactory.java

private static void tryImplementation(String testClassName, String implClassName) {
    if (logConstructor == null) {
        try {//from   www .j  av a  2 s. c om
            Resources.classForName(testClassName);
            Class implClass = Resources.classForName(implClassName);
            logConstructor = implClass.getConstructor(new Class[] { Class.class });
        } catch (Throwable t) {
        }
    }
}

From source file:Main.java

public static Constructor<?> getConstructor(Class<?> targetClass, Class<?>... types) {
    if (targetClass == null || types == null)
        return null;
    try {/* w ww . j  a v a  2s  .  co  m*/
        return targetClass.getConstructor(types);
    } catch (SecurityException e) {
        // ignore
    } catch (NoSuchMethodException e) {
        // ignore
    }
    return null;
}

From source file:Main.java

public static Constructor<?> getConstructor(Class<?> targetClass, Class<?>... types) {
    if (targetClass == null || types == null)
        return null;
    try {//from w w w. jav a 2  s  . c  o  m
        return targetClass.getConstructor(types);
    } catch (SecurityException e) {
        e.printStackTrace();
    } catch (NoSuchMethodException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.inmobi.conduit.CheckpointProviderFactory.java

private static CheckpointProvider getProvider(InputStream in) throws Exception {
    CheckpointProvider provider = null;//from w  ww .ja va2s  .  c o m
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    try {
        String line;
        String providerName = null;
        String providerDir = null;
        do {
            line = reader.readLine();
            String[] keyVal = line.split("=");
            if (keyVal != null && keyVal.length == 2 && keyVal[0].equalsIgnoreCase(CHECKPOINT_PROVIDER)) {
                //provider = (CheckpointProvider) Class.forName(keyVal[1])
                //.newInstance();
                providerName = keyVal[1];
            }
            if (keyVal != null && keyVal.length == 2 && keyVal[0].equalsIgnoreCase(CHECKPOINT_PROVIDER_DIR)) {
                providerDir = keyVal[1];
            }
        } while (line != null);

        if (providerName != null && providerDir != null) {
            Class providerClass = Class.forName(providerName);
            Constructor constructor = providerClass.getConstructor(String.class);
            provider = (CheckpointProvider) constructor.newInstance(new Object[] { providerDir });
        }
    } catch (Exception e) {
        //return default provider
        LOG.debug("Error creating CheckPointProvider", e);
        return null;
    }
    return provider;
}