Example usage for java.lang Class getDeclaredConstructor

List of usage examples for java.lang Class getDeclaredConstructor

Introduction

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

Prototype

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

Source Link

Document

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

Usage

From source file:org.commoncrawl.hadoop.mergeutils.SequenceFileReader.java

public SequenceFileReader(FileSystem fileSystem, Configuration conf, Vector<Path> inputSegments,
        SpillWriter<KeyType, ValueType> spillWriter, Class<KeyType> keyClass, Class<ValueType> valueClass)
        throws IOException {

    _sourceFileSystem = fileSystem;/*  w  w w. j a  v a2s . c o  m*/
    _config = conf;
    _inputSegments = inputSegments;
    _writer = spillWriter;

    try {
        this._keyConstructor = keyClass.getDeclaredConstructor(emptyArray);
        this._keyConstructor.setAccessible(true);
        this._valConstructor = valueClass.getDeclaredConstructor(emptyArray);
        this._valConstructor.setAccessible(true);
    } catch (SecurityException e) {
        LOG.error(CCStringUtils.stringifyException(e));
        throw new RuntimeException(e);
    } catch (NoSuchMethodException e) {
        LOG.error(CCStringUtils.stringifyException(e));
        throw new RuntimeException(e);
    }

}

From source file:at.ac.tuwien.infosys.jcloudscale.test.unit.TestReflectionUtil.java

<T> Constructor<T> resolveConstructor(Class<T> type, Class<?>... paramTypes) {
    Constructor<T> expected = null;
    try {/*w w  w .  j  av a  2  s  . co  m*/
        expected = type.getDeclaredConstructor(paramTypes);
    } catch (NoSuchMethodException e) {
        // Ignore
    }
    Constructor<T> actual = null;
    try {
        actual = findConstructor(type, paramTypes);
    } catch (Exception e) {
        // Ignore
    }
    assertEquals(expected, actual);
    return actual;
}

From source file:org.apache.ranger.authorization.hive.authorizer.RangerHiveMetastoreAuthorizer.java

public void init() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> RangerHiveMetastoreAuthorizer.init()");
    }/*from w  w  w  .  j  a v a 2 s . c o  m*/

    try {

        rangerPluginClassLoader = RangerPluginClassLoader.getInstance(RANGER_PLUGIN_TYPE, this.getClass());

        @SuppressWarnings("unchecked")
        Class<MetaStorePreEventListener> cls = (Class<MetaStorePreEventListener>) Class
                .forName(RANGER_HIVE_METASTORE_AUTHORIZER_IMPL_CLASSNAME, true, rangerPluginClassLoader);

        activatePluginClassLoader();
        Constructor ctor = cls.getDeclaredConstructor(Configuration.class);
        rangerHiveMetastoreAuthorizerImpl = (MetaStorePreEventListener) ctor.newInstance(getConf());
    } catch (Exception e) {
        // check what need to be done
        LOG.error("Error Enabling RangerHdfsPluing", e);
    } finally {
        deactivatePluginClassLoader();
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("<== RangerHdfsAuthorizer.init()");
    }
}

From source file:com.quattroresearch.antibody.plugin.PluginLoader.java

/**
 * Loads the toolbar plugins and inserts them into the toolbar.
 *///from  ww w.  j  a  v  a  2 s.c  o  m
public void loadToolbarPlugins() {
    String toolbarPluginConfig = PreferencesService.getInstance().getApplicationPrefs()
            .getString("plugins.toolbar");
    if (toolbarPluginConfig == null) {
        return;
    }
    String[] toolbarPlugins = toolbarPluginConfig.split(";");
    for (String singleConfig : toolbarPlugins) {
        try {

            Class<?> clazz = classLoader.findClass(singleConfig);

            if (clazz != null) {
                Constructor<?> constructor;

                constructor = clazz.getDeclaredConstructor(JFrame.class);

                ToolBarToggleButton toolBarButton = (ToolBarToggleButton) constructor
                        .newInstance(UIService.getInstance().getMainFrame());

                UIService.getInstance().getToolBar().add(toolBarButton);
            }

        } catch (ClassNotFoundException exc) {
            System.err.println("Toolbar Plugin class was not found: " + exc.toString());
        } catch (Exception exc) {
            System.err.println(exc.toString());
        }
    }
}

From source file:StorageEngineClient.CombineFileRecordReader.java

@SuppressWarnings("deprecation")
public CombineFileRecordReader(JobConf job, CombineFileSplit split, Reporter reporter,
        Class<? extends RecordReader<K, V>> rrClass) throws IOException {
    this.split = split;
    this.jc = job;
    this.rrClass = rrClass;
    this.reporter = reporter;
    this.idx = 0;
    this.curReader = null;
    this.progress = 0;

    try {// w w  w .j  a v  a  2s. com
        rrConstructor = rrClass.getDeclaredConstructor(constructorSignature);
        rrConstructor.setAccessible(true);
    } catch (Exception e) {
        throw new RuntimeException(rrClass.getName() + " does not have valid constructor", e);
    }
    initNextRecordReader();
}

From source file:com.quattroresearch.antibody.plugin.PluginLoader.java

/**
 * Loads the menu plugins and inserts them into the menu.
 *//*from w  w  w.  j  a va2  s.c o  m*/
public void loadMenuPlugins() {
    String menuPluginConfig = PreferencesService.getInstance().getApplicationPrefs().getString("plugins.menu");
    if (menuPluginConfig == null) {
        return;
    }
    String[] menuPlugins = menuPluginConfig.split(";");
    for (String singleConfig : menuPlugins) {
        try {

            Class<?> clazz = classLoader.findClass(singleConfig);

            if (clazz != null) {
                Constructor<?> constructor;

                constructor = clazz.getDeclaredConstructor(JFrame.class);

                AbstractEditorAction action = (AbstractEditorAction) constructor
                        .newInstance(UIService.getInstance().getMainFrame());

                boolean isAdded = false;
                JMenuBar menubar = UIService.getInstance().getMainFrame().getJMenuBar();
                for (int i = 0; i < menubar.getMenuCount(); i++) {
                    JMenu menu = menubar.getMenu(i);
                    if (menu.getText().equals(action.getMenuName())) {
                        menu.add(action);
                        isAdded = true;
                        break;
                    }
                }
                if (!isAdded) {
                    JMenu newMenu = new JMenu(action.getMenuName());
                    newMenu.add(action);
                    menubar.add(newMenu, menubar.getMenuCount() - 1);
                }
            }
        } catch (ClassNotFoundException exc) {
            System.err.println("Menu Plugin class was not found: " + exc.toString());
        } catch (Exception exc) {
            System.err.println(exc.toString());
        }
    }
}

From source file:catalina.startup.ContextRuleSet.java

public void begin(Attributes attributes) throws Exception {

    // Look up the required parent class loader
    Container container = (Container) digester.peek();
    ClassLoader parentClassLoader = container.getParentClassLoader();

    // Instantiate a new Loader implementation object
    String className = loaderClass;
    if (attributeName != null) {
        String value = attributes.getValue(attributeName);
        if (value != null)
            className = value;//from  w  ww. j  a  v  a  2s . c om
    }
    Class clazz = Class.forName(className);
    Class types[] = { ClassLoader.class };
    Object args[] = { parentClassLoader };
    Constructor constructor = clazz.getDeclaredConstructor(types);
    Loader loader = (Loader) constructor.newInstance(args);

    // Push the new loader onto the stack
    digester.push(loader);
    if (digester.getDebug() >= 1)
        digester.log("new " + loader.getClass().getName());

}

From source file:org.apache.hadoop.hbase.thrift2.client.ThriftConnection.java

public ThriftConnection(Configuration conf, ExecutorService pool, final User user) throws IOException {
    this.conf = conf;
    this.user = user;
    this.host = conf.get(Constants.HBASE_THRIFT_SERVER_NAME);
    this.port = conf.getInt(Constants.HBASE_THRIFT_SERVER_PORT, -1);
    Preconditions.checkArgument(port > 0);
    Preconditions.checkArgument(host != null);
    this.isFramed = conf.getBoolean(Constants.FRAMED_CONF_KEY, Constants.FRAMED_CONF_DEFAULT);
    this.isCompact = conf.getBoolean(Constants.COMPACT_CONF_KEY, Constants.COMPACT_CONF_DEFAULT);
    this.operationTimeout = conf.getInt(HConstants.HBASE_CLIENT_OPERATION_TIMEOUT,
            HConstants.DEFAULT_HBASE_CLIENT_OPERATION_TIMEOUT);
    this.connectTimeout = conf.getInt(SOCKET_TIMEOUT_CONNECT, DEFAULT_SOCKET_TIMEOUT_CONNECT);

    String className = conf.get(Constants.HBASE_THRIFT_CLIENT_BUIDLER_CLASS,
            DefaultThriftClientBuilder.class.getName());
    try {//from   w  ww  .  ja  v a 2  s . c o  m
        Class<?> clazz = Class.forName(className);
        Constructor<?> constructor = clazz.getDeclaredConstructor(ThriftConnection.class);
        constructor.setAccessible(true);
        clientBuilder = (ThriftClientBuilder) constructor.newInstance(this);
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.rdfservice.filter.LanguageFilteringRDFServiceTest.java

@SuppressWarnings("unchecked")
private Comparator<Object> buildRowIndexedLiteralSortByLang() {
    try {/*w  ww . ja  v  a2s.c  o m*/
        Class<?> clazz = Class.forName(COLLATOR_CLASSNAME);
        Class<?>[] argTypes = { LanguageFilteringRDFService.class };
        Constructor<?> constructor = clazz.getDeclaredConstructor(argTypes);
        constructor.setAccessible(true);

        return (Comparator<Object>) constructor.newInstance(filteringRDFService);
    } catch (Exception e) {
        throw new RuntimeException("Could not create a collator", e);
    }
}

From source file:jenkins.security.apitoken.ApiTokenStatsTest.java

private ApiTokenStats.SingleTokenStats createSingleTokenStatsByReflection(String uuid, String dateString,
        Integer counter) throws Exception {
    Class<ApiTokenStats.SingleTokenStats> clazz = ApiTokenStats.SingleTokenStats.class;
    Constructor<ApiTokenStats.SingleTokenStats> constructor = clazz.getDeclaredConstructor(String.class);
    constructor.setAccessible(true);/*from  ww  w  .  jav a2  s .c  o  m*/

    ApiTokenStats.SingleTokenStats result = constructor.newInstance(uuid);

    {
        Field field = clazz.getDeclaredField("useCounter");
        field.setAccessible(true);
        field.set(result, counter);
    }
    if (dateString != null) {
        Field field = clazz.getDeclaredField("lastUseDate");
        field.setAccessible(true);
        field.set(result, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse(dateString));
    }

    return result;
}