Example usage for java.lang NoSuchMethodException getClass

List of usage examples for java.lang NoSuchMethodException getClass

Introduction

In this page you can find the example usage for java.lang NoSuchMethodException getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.chiorichan.plugin.PluginManager.java

/**
 * Registers the specified plugin loader
 * // w w w. j a v  a 2s  . co  m
 * @param loader
 *            Class name of the PluginLoader to register
 * @throws IllegalArgumentException
 *             Thrown when the given Class is not a valid PluginLoader
 */
public void registerInterface(Class<? extends PluginLoader> loader) throws IllegalArgumentException {
    PluginLoader instance;

    if (PluginLoader.class.isAssignableFrom(loader)) {
        Constructor<? extends PluginLoader> constructor;

        try {
            constructor = loader.getConstructor();
            instance = constructor.newInstance();
        } catch (NoSuchMethodException ex) {
            try {
                constructor = loader.getConstructor(Loader.class);
                instance = constructor.newInstance(Loader.getInstance());
            } catch (NoSuchMethodException ex1) {
                String className = loader.getName();

                throw new IllegalArgumentException(String.format(
                        "Class %s does not have a public %s(Server) constructor", className, className), ex1);
            } catch (Exception ex1) {
                throw new IllegalArgumentException(String.format(
                        "Unexpected exception %s while attempting to construct a new instance of %s",
                        ex.getClass().getName(), loader.getName()), ex1);
            }
        } catch (Exception ex) {
            throw new IllegalArgumentException(
                    String.format("Unexpected exception %s while attempting to construct a new instance of %s",
                            ex.getClass().getName(), loader.getName()),
                    ex);
        }
    } else {
        throw new IllegalArgumentException(
                String.format("Class %s does not implement interface PluginLoader", loader.getName()));
    }

    Pattern[] patterns = instance.getPluginFileFilters();

    synchronized (this) {
        for (Pattern pattern : patterns) {
            fileAssociations.put(pattern, instance);
        }
    }
}

From source file:org.apache.cassandra.io.compress.CompressionParameters.java

private static ICompressor createCompressor(Class<?> compressorClass, Map<String, String> compressionOptions)
        throws ConfigurationException {
    if (compressorClass == null) {
        if (!compressionOptions.isEmpty())
            throw new ConfigurationException("Unknown compression options (" + compressionOptions.keySet()
                    + ") since no compression class found");
        return null;
    }// w w w  .j a  va  2  s  .c  o m

    try {
        Method method = compressorClass.getMethod("create", Map.class);
        ICompressor compressor = (ICompressor) method.invoke(null, compressionOptions);
        // Check for unknown options
        AbstractSet<String> supportedOpts = Sets.union(compressor.supportedOptions(), GLOBAL_OPTIONS);
        for (String provided : compressionOptions.keySet())
            if (!supportedOpts.contains(provided))
                throw new ConfigurationException("Unknown compression options " + provided);
        return compressor;
    } catch (NoSuchMethodException e) {
        throw new ConfigurationException("create method not found", e);
    } catch (SecurityException e) {
        throw new ConfigurationException("Access forbiden", e);
    } catch (IllegalAccessException e) {
        throw new ConfigurationException("Cannot access method create in " + compressorClass.getName(), e);
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        throw new ConfigurationException(String.format("%s.create() threw an error: %s",
                compressorClass.getSimpleName(), cause == null ? e.getClass().getName() + " " + e.getMessage()
                        : cause.getClass().getName() + " " + cause.getMessage()),
                e);
    } catch (ExceptionInInitializerError e) {
        throw new ConfigurationException("Cannot initialize class " + compressorClass.getName());
    }
}

From source file:org.dragonet.plugin.MixedPluginManager.java

/**
 * Registers the specified plugin loader
 *
 * @param loader Class name of the PluginLoader to register
 * @throws IllegalArgumentException Thrown when the given Class is not a
 *     valid PluginLoader/* w ww .j  a  v  a 2  s .  c  o  m*/
 */
@Override
public void registerInterface(Class<? extends PluginLoader> loader) throws IllegalArgumentException {
    PluginLoader instance;

    if (PluginLoader.class.isAssignableFrom(loader)) {
        Constructor<? extends PluginLoader> constructor;

        try {
            constructor = loader.getConstructor(Server.class);
            instance = constructor.newInstance(server);
        } catch (NoSuchMethodException ex) {
            String className = loader.getName();

            throw new IllegalArgumentException(String.format(
                    "Class %s does not have a public %s(Server) constructor", className, className), ex);
        } catch (Exception ex) {
            throw new IllegalArgumentException(
                    String.format("Unexpected exception %s while attempting to construct a new instance of %s",
                            ex.getClass().getName(), loader.getName()),
                    ex);
        }
    } else {
        throw new IllegalArgumentException(
                String.format("Class %s does not implement interface PluginLoader", loader.getName()));
    }

    Pattern[] patterns = instance.getPluginFileFilters();

    synchronized (this) {
        for (Pattern pattern : patterns) {
            fileAssociations.put(pattern, instance);
        }
    }
}

From source file:org.saiku.adhoc.service.SaikuAdhocContentGenerator.java

@Override
public void createContent(OutputStream out) throws Exception {
    {/*from  w ww  .  ja  v a2  s.  c om*/

        final IParameterProvider pathParams = parameterProviders.get("path");
        final IParameterProvider requestParams = parameterProviders.get("request");

        try {

            final Class[] params = { IParameterProvider.class, OutputStream.class };

            final String method = pathParams.getStringParameter("path", null).split("/")[1].toLowerCase();

            try {
                final Method mthd = this.getClass().getMethod(method, params);
                mthd.invoke(this, requestParams, out);
            } catch (NoSuchMethodException e) {
                logger.error("could not invoke " + method);
            } catch (InvocationTargetException e) {
                //get to the cause and rethrow properly
                Throwable target = e.getTargetException();
                if (!e.equals(target)) {//just in case
                                        //get to the real cause
                    while (target != null && target instanceof InvocationTargetException) {
                        target = ((InvocationTargetException) target).getTargetException();
                    }
                }
                if (target instanceof Exception) {
                    throw (Exception) target;
                } else {
                    throw new Exception(target);
                }
            }
        } catch (Exception e) {
            final String message = e.getCause() != null
                    ? e.getCause().getClass().getName() + " - " + e.getCause().getMessage()
                    : e.getClass().getName() + " - " + e.getMessage();
            logger.error(message, e);
        }
    }
}

From source file:org.wings.plaf.LookAndFeel.java

/**
 * Utility method that creates an Object of class <code>clazz</code>
 * using the single String arg constructor.
 *
 * @param value object as a string//from   w  w w .ja  v  a  2 s  . co m
 * @param clazz class of the object
 * @return the object
 */
public static Object makeObject(String value, Class clazz) {
    Object result;
    try {
        if (value.startsWith("new ")) {
            int bracket = value.indexOf("(");
            String name = value.substring("new ".length(), bracket);
            clazz = Class.forName(name, true, Thread.currentThread().getContextClassLoader());
            result = clazz.newInstance();
        } else {
            if (clazz.isPrimitive())
                clazz = (Class) wrappers.get(clazz);
            Constructor constructor = clazz.getConstructor(new Class[] { String.class });
            result = constructor.newInstance(new Object[] { value });
        }
    } catch (NoSuchMethodException e) {
        log.fatal(value + " : " + clazz.getName() + " doesn't have a single String arg constructor", e);
        result = null;
    } catch (Exception e) {
        log.error(e.getClass().getName() + " : " + value, e);
        result = null;
    }
    return result;
}

From source file:org.wso2.carbon.micro.integrator.security.MicroIntegratorSecurityUtils.java

/**
 * This method initializes the user store manager class
 *
 * @param className   class name of the user store manager class
 * @param realmConfig realm configuration defined
 * @return initialized UserStoreManager class
 * @throws UserStoreException//w  w w .  ja v  a  2 s.  c o m
 */
public static Object createObjectWithOptions(String className, RealmConfiguration realmConfig)
        throws UserStoreException {
    /*
    Since different User Store managers contain constructors requesting different sets of arguments, this method
    tries to invoke the constructor with different combinations of arguments
     */
    Class[] initClassOpt1 = new Class[] { RealmConfiguration.class, ClaimManager.class,
            ProfileConfigurationManager.class };
    Object[] initObjOpt1 = new Object[] { realmConfig, null, null };
    Class[] initClassOpt2 = new Class[] { RealmConfiguration.class, Integer.class };
    Object[] initObjOpt2 = new Object[] { realmConfig, -1234 };
    Class[] initClassOpt3 = new Class[] { RealmConfiguration.class };
    Object[] initObjOpt3 = new Object[] { realmConfig };
    Class[] initClassOpt4 = new Class[] {};
    Object[] initObjOpt4 = new Object[] {};
    try {
        Class clazz = Class.forName(className);
        Object newObject = null;
        if (log.isDebugEnabled()) {
            log.debug("Start initializing the UserStoreManager class with first option");
        }

        Constructor constructor;
        try {
            constructor = clazz.getConstructor(initClassOpt1);
            newObject = constructor.newInstance(initObjOpt1);
            return newObject;
        } catch (NoSuchMethodException e) {
            if (log.isDebugEnabled()) {
                log.debug("Cannont initialize " + className + " trying second option");
            }
        }

        try {
            constructor = clazz.getConstructor(initClassOpt2);
            newObject = constructor.newInstance(initObjOpt2);
            return newObject;
        } catch (NoSuchMethodException e) {
            if (log.isDebugEnabled()) {
                log.debug("Cannont initialize " + className + " using the option 2");
            }
        }

        try {
            constructor = clazz.getConstructor(initClassOpt3);
            newObject = constructor.newInstance(initObjOpt3);
            return newObject;
        } catch (NoSuchMethodException e) {
            if (log.isDebugEnabled()) {
                log.debug("Cannont initialize " + className + " using the option 3");
            }
        }

        try {
            constructor = clazz.getConstructor(initClassOpt4);
            newObject = constructor.newInstance(initObjOpt4);
            return newObject;
        } catch (NoSuchMethodException e) {
            if (log.isDebugEnabled()) {
                log.debug("Cannont initialize " + className + " using the option 4");
            }
            throw new UserStoreException(e.getMessage(), e);
        }
    } catch (Throwable e) {
        if (log.isDebugEnabled()) {
            log.debug("Cannot create " + className, e);
        }
        throw new UserStoreException(e.getMessage() + "Type " + e.getClass(), e);
    }
}

From source file:org.wso2.carbon.user.core.common.AbstractUserStoreManager.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private UserStoreManager createSecondaryUserStoreManager(RealmConfiguration realmConfig, UserRealm realm)
        throws UserStoreException {

    if (!isSecureCall.get()) {
        Class argTypes[] = new Class[] { RealmConfiguration.class, UserRealm.class };
        Object object = callSecure("createSecondaryUserStoreManager", new Object[] { realmConfig, realm },
                argTypes);//  ww  w.ja v  a2s. c o m
        return (UserStoreManager) object;
    }

    // setting global realm configurations such as everyone role, admin role and admin user
    realmConfig.setEveryOneRoleName(this.realmConfig.getEveryOneRoleName());
    realmConfig.setAdminUserName(this.realmConfig.getAdminUserName());
    realmConfig.setAdminRoleName(this.realmConfig.getAdminRoleName());

    String className = realmConfig.getUserStoreClass();
    if (className == null) {
        String errmsg = "Unable to add user store. UserStoreManager class name is null.";
        if (log.isDebugEnabled()) {
            log.debug(errmsg);
        }
        throw new UserStoreException(errmsg);
    }

    HashMap<String, Object> properties = new HashMap<String, Object>();
    properties.put(UserCoreConstants.DATA_SOURCE, this.dataSource);
    properties.put(UserCoreConstants.FIRST_STARTUP_CHECK, false);

    Class[] initClassOpt1 = new Class[] { RealmConfiguration.class, Map.class, ClaimManager.class,
            ProfileConfigurationManager.class, UserRealm.class, Integer.class };
    Object[] initObjOpt1 = new Object[] { realmConfig, properties, realm.getClaimManager(), null, realm,
            tenantId };

    // These two methods won't be used
    Class[] initClassOpt2 = new Class[] { RealmConfiguration.class, Map.class, ClaimManager.class,
            ProfileConfigurationManager.class, UserRealm.class };
    Object[] initObjOpt2 = new Object[] { realmConfig, properties, realm.getClaimManager(), null, realm };

    Class[] initClassOpt3 = new Class[] { RealmConfiguration.class, Map.class };
    Object[] initObjOpt3 = new Object[] { realmConfig, properties };

    try {
        Class clazz = Class.forName(className);
        Constructor constructor = null;
        Object newObject = null;

        if (log.isDebugEnabled()) {
            log.debug("Start initializing class with the first option");
        }

        try {
            constructor = clazz.getConstructor(initClassOpt1);
            newObject = constructor.newInstance(initObjOpt1);
            return (UserStoreManager) newObject;
        } catch (NoSuchMethodException e) {
            // if not found try again.
            if (log.isDebugEnabled()) {
                log.debug("Cannont initialize " + className + " using the option 1");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("End initializing class with the first option");
        }

        try {
            constructor = clazz.getConstructor(initClassOpt2);
            newObject = constructor.newInstance(initObjOpt2);
            return (UserStoreManager) newObject;
        } catch (NoSuchMethodException e) {
            // if not found try again.
            if (log.isDebugEnabled()) {
                log.debug("Cannot initialize " + className + " using the option 2");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("End initializing class with the second option");
        }

        try {
            constructor = clazz.getConstructor(initClassOpt3);
            newObject = constructor.newInstance(initObjOpt3);
            return (UserStoreManager) newObject;
        } catch (NoSuchMethodException e) {
            // cannot initialize in any of the methods. Throw exception.
            String message = "Cannot initialize " + className + ". Error " + e.getMessage();
            if (log.isDebugEnabled()) {
                log.debug(message, e);
            }
            throw new UserStoreException(message, e);
        }

    } catch (Throwable e) {
        String errorMessage = "Cannot create " + className;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(e.getMessage() + "Type " + e.getClass(), e);
    }

}

From source file:org.wso2.carbon.user.core.common.DefaultRealm.java

@SuppressWarnings({ "rawtypes", "unchecked" })
private Object createObjectWithOptions(String className, RealmConfiguration realmConfig, Map properties)
        throws UserStoreException {

    Class[] initClassOpt1 = new Class[] { RealmConfiguration.class, Map.class, ClaimManager.class,
            ProfileConfigurationManager.class, UserRealm.class, Integer.class };
    Object[] initObjOpt1 = new Object[] { realmConfig, properties, claimMan, null, this, tenantId };

    Class[] initClassOpt2 = new Class[] { RealmConfiguration.class, Map.class, ClaimManager.class,
            ProfileConfigurationManager.class, UserRealm.class };
    Object[] initObjOpt2 = new Object[] { realmConfig, properties, claimMan, null, this };

    Class[] initClassOpt3 = new Class[] { RealmConfiguration.class, Map.class };
    Object[] initObjOpt3 = new Object[] { realmConfig, properties };

    try {//from  w  ww .  j  a  v  a2  s . c  o  m
        Class clazz = Class.forName(className);
        Constructor constructor = null;
        Object newObject = null;

        if (log.isDebugEnabled()) {
            log.debug("Start initializing class with the first option");
        }

        try {
            constructor = clazz.getConstructor(initClassOpt1);
            newObject = constructor.newInstance(initObjOpt1);
            return newObject;
        } catch (NoSuchMethodException e) {
            // if not found try again.
            if (log.isDebugEnabled()) {
                log.debug("Cannont initialize " + className + " using the option 1");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("End initializing class with the first option");
        }

        try {
            constructor = clazz.getConstructor(initClassOpt2);
            newObject = constructor.newInstance(initObjOpt2);
            return newObject;
        } catch (NoSuchMethodException e) {
            // if not found try again.
            if (log.isDebugEnabled()) {
                log.debug("Cannont initialize " + className + " using the option 2");
            }
        }

        if (log.isDebugEnabled()) {
            log.debug("End initializing class with the second option");
        }

        try {
            constructor = clazz.getConstructor(initClassOpt3);
            newObject = constructor.newInstance(initObjOpt3);
            return newObject;
        } catch (NoSuchMethodException e) {
            // cannot initialize in any of the methods. Throw exception.
            String message = "Cannot initialize " + className + ". Error " + e.getMessage();
            if (log.isDebugEnabled()) {
                log.debug(message, e);
            }
            throw new UserStoreException(message, e);
        }

    } catch (Throwable e) {
        String errorMessage = "Cannot create " + className;
        if (log.isDebugEnabled()) {
            log.debug(errorMessage, e);
        }
        throw new UserStoreException(e.getMessage() + "Type " + e.getClass(), e);
    }

}