Example usage for java.lang InstantiationException getClass

List of usage examples for java.lang InstantiationException getClass

Introduction

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

Prototype

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

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:com.feilong.commons.core.util.CollectionsUtilTest.java

/**
 * Test./*from w  ww .  j a v a  2  s .  c  om*/
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void test() {
    try {
        Class clz = User.class;
        Collection collection = CollectionUtils.typedCollection(new ArrayList(), clz);
        collection.add(clz.newInstance());

        log.info(collection.size() + "");
        for (Object object : collection) {
            User user = (User) object;
            log.info(user.getName());
        }

        log.info("hahahah");

        Collection<User> collection2 = collection;
        log.info(collection2.size() + "");
        for (Object object : collection2) {
            User user = (User) object;
            log.info(user.getName());
        }

    } catch (InstantiationException e) {
        log.error(e.getClass().getName(), e);
    } catch (IllegalAccessException e) {
        log.error(e.getClass().getName(), e);
    }
}

From source file:de.cenote.jasperstarter.Report.java

private Map<String, Object> getCmdLineReportParams() {
    JRParameter[] jrParameterArray = jasperReport.getParameters();
    Map<String, JRParameter> jrParameters = new HashMap<String, JRParameter>();
    Map<String, Object> parameters = new HashMap<String, Object>();
    List<String> params;
    if (config.hasParams()) {
        params = config.getParams();//from   w  ww.j a  v a 2  s.com
        for (JRParameter rp : jrParameterArray) {
            jrParameters.put(rp.getName(), rp);
        }
        String paramName = null;
        //String paramType = null;
        String paramValue = null;

        for (String p : params) {
            try {
                paramName = p.split("=")[0];
                paramValue = p.split("=", 2)[1];
                if (config.isVerbose()) {
                    System.out.println("Using report parameter: " + paramName + " = " + paramValue);
                }
            } catch (Exception e) {
                throw new IllegalArgumentException("Wrong report param format! " + p, e);
            }
            if (!jrParameters.containsKey(paramName)) {
                throw new IllegalArgumentException("Parameter '" + paramName + "' does not exist in report!");
            }

            JRParameter reportParam = jrParameters.get(paramName);

            try {
                // special parameter handlers must also implemeted in
                // ParameterPanel.java
                if (Date.class.equals(reportParam.getValueClass())) {
                    // Date must be in ISO8601 format. Example 2012-12-31
                    DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd");
                    parameters.put(paramName, (Date) dateFormat.parse(paramValue));
                } else if (Image.class.equals(reportParam.getValueClass())) {
                    Image image = Toolkit.getDefaultToolkit()
                            .createImage(JRLoader.loadBytes(new File(paramValue)));
                    MediaTracker traker = new MediaTracker(new Panel());
                    traker.addImage(image, 0);
                    try {
                        traker.waitForID(0);
                    } catch (Exception e) {
                        throw new IllegalArgumentException("Image tracker error: " + e.getMessage(), e);
                    }
                    parameters.put(paramName, image);
                } else if (Locale.class.equals(reportParam.getValueClass())) {
                    parameters.put(paramName, LocaleUtils.toLocale(paramValue));
                } else {
                    // handle generic parameters with string constructor
                    try {
                        parameters.put(paramName, reportParam.getValueClass().getConstructor(String.class)
                                .newInstance(paramValue));
                    } catch (InstantiationException ex) {
                        throw new IllegalArgumentException("Parameter '" + paramName + "' of type '"
                                + reportParam.getValueClass().getName() + " caused " + ex.getClass().getName()
                                + " " + ex.getMessage(), ex);
                    } catch (IllegalAccessException ex) {
                        throw new IllegalArgumentException("Parameter '" + paramName + "' of type '"
                                + reportParam.getValueClass().getName() + " caused " + ex.getClass().getName()
                                + " " + ex.getMessage(), ex);
                    } catch (InvocationTargetException ex) {
                        Throwable cause = ex.getCause();
                        throw new IllegalArgumentException("Parameter '" + paramName + "' of type '"
                                + reportParam.getValueClass().getName() + " caused "
                                + cause.getClass().getName() + " " + cause.getMessage(), cause);
                    } catch (NoSuchMethodException ex) {
                        throw new IllegalArgumentException("Parameter '" + paramName + "' of type '"
                                + reportParam.getValueClass().getName() + " with value '" + paramValue
                                + "' is not supported by JasperStarter!", ex);
                    }
                }
            } catch (NumberFormatException e) {
                throw new IllegalArgumentException(
                        "NumberFormatException: " + e.getMessage() + "\" in \"" + p + "\"", e);
            } catch (java.text.ParseException e) {
                throw new IllegalArgumentException(e.getMessage() + "\" in \"" + p + "\"", e);
            } catch (JRException e) {
                throw new IllegalArgumentException("Unable to load image from: " + paramValue, e);
            }
        }
    }
    return parameters;
}

From source file:org.codehaus.groovy.grails.web.binding.GrailsDataBinder.java

private Object autoInstantiateEmbeddedInstance(Class<?> type) {
    Object created = null;/* w w w  .j a  v a  2s .co m*/
    try {
        created = type.newInstance();
    } catch (InstantiationException e) {
        LOG.error(String.format("Unable to auto-create type %s, %s thrown in constructor", type, e.getClass()));
    } catch (IllegalAccessException e) {
        LOG.error(String.format("Unable to auto-create type %s, cannot access constructor", type));
    }
    return created;
}

From source file:org.codehaus.grepo.core.validator.GenericValidationUtils.java

/**
 * Validates the given {@code result} using the given {@link ResultValidator} {@code clazz}.
 *
 * @param mpi The method parameter info.
 * @param clazz The validator clazz (must not be null).
 * @param result The result to validate.
 * @throws Exception in case of errors (like validation errors).
 * @throws ValidationException if the given {@link ResultValidator} cannot be instantiated.
 *//*from   ww  w.j  ava  2s . co m*/
public static void validateResult(MethodParameterInfo mpi, Class<? extends ResultValidator> clazz,
        Object result) throws Exception, ValidationException {
    if (isValidResultValidator(clazz)) {
        ResultValidator validator = null;
        try {
            logger.debug("Using result validator '{}' for validating result '{}'", clazz, result);
            validator = clazz.newInstance();
        } catch (InstantiationException e) {
            String msg = String.format("Unable to create new instance of '%s': '%s'", clazz.getName(),
                    e.getMessage());
            throw new ValidationException(msg, e);
        } catch (IllegalAccessException e) {
            String msg = String.format("Unable to create new instance of '%s': '%s'", clazz.getName(),
                    e.getMessage());
            throw new ValidationException(msg, e);
        }

        try {
            validator.validate(result);
        } catch (Exception e) {
            logger.debug("Validation error occured: {}", e.getMessage());

            if (mpi.isMethodCompatibleWithException(e)) {
                throw e;
            } else {
                String m = "Exception '%s' is not compatible with  method '%s' (exeptionTypes: %s) "
                        + "- exception will be wrapped in a ValidationException";
                String msg = String.format(m, e.getClass().getName(), mpi.getMethodName(),
                        ArrayUtils.toString(mpi.getMethod().getExceptionTypes()));
                logger.error(msg);
                throw new ValidationException(msg);
            }
        }
    }
}