List of usage examples for java.lang Class getDeclaredConstructor
@CallerSensitive public Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
From source file:it.cnr.icar.eric.client.xml.registry.LifeCycleManagerImpl.java
/** * Creates instances of information model interfaces (factory method). To * create an Organization, use this method as follows: * /*from ww w .j a v a 2 s. c om*/ * <pre> * Organization org = (Organization) lifeCycleMgr.createObject(LifeCycleManager.ORGANIZATION); * </pre> * <p> * <DL> * <DT><B>Capability Level: 0 </B> * </DL> * * @param interfaceName * the unqualified name of an interface in the * javax.xml.registry.infomodel package * * @return an Object that can then be cast to an instance of the interface * * @throws JAXRException * if the JAXR provider encounters an internal error * * @throws InvalidRequestException * if the interface is not an interface in the * javax.xml.registry.infomodel package * * @throws UnsupportedCapabilityException * if the client attempts to create an instance of an infomodel * interface that is not supported by the capability level of * the JAXR provider */ public Object createObject(String className) throws JAXRException, InvalidRequestException, UnsupportedCapabilityException { Object obj = null; try { // Try to find extended constructor by nickname Constructor<?> cons = imFactory.getConstructor1Arg(className); if (cons != null) { // use extended constructor Object[] args = { this }; obj = cons.newInstance(args); // set extended type String typeId = imFactory.getTypeName(className); BusinessQueryManagerImpl bqm = (BusinessQueryManagerImpl) regService.getBusinessQueryManager(); Concept typeConcept = (Concept) bqm.getRegistryObject(typeId, LifeCycleManager.CONCEPT); if (obj instanceof Association) { ((Association) obj).setAssociationType(typeConcept); } else if (obj instanceof ExtrinsicObject) { ((ExtrinsicObjectImpl) obj).setObjectType(typeConcept); } } else { // proceed the default way: infomodel class className = "it.cnr.icar.eric.client.xml.registry.infomodel." + BindingUtility.mapEbXMLNameToJAXRName(className) + "Impl"; Class<?> cls = this.getClass().getClassLoader().loadClass(className); Class<?> lcmCls = this.getClass().getClassLoader() .loadClass("it.cnr.icar.eric.client.xml.registry.LifeCycleManagerImpl"); @SuppressWarnings("rawtypes") Class[] parmTypes = { lcmCls }; cons = cls.getDeclaredConstructor(parmTypes); Object[] args = { this }; obj = cons.newInstance(args); } } catch (ClassNotFoundException e) { throw new InvalidRequestException(JAXRResourceBundle.getInstance() .getString("message.error.invalid.classname", new Object[] { className })); } catch (NoSuchMethodException e) { throw new JAXRException(e); } catch (InvocationTargetException e) { throw new JAXRException(e.getCause()); } catch (IllegalAccessException e) { throw new JAXRException(e); } catch (InstantiationException e) { throw new JAXRException(e); } catch (ExceptionInInitializerError e) { throw new JAXRException(e); } catch (SecurityException e) { throw new JAXRException(e); } return obj; }
From source file:org.apache.geode.internal.InternalDataSerializer.java
/** * Instantiates an instance of {@code DataSerializer} * * @throws IllegalArgumentException If the class can't be instantiated * * @see DataSerializer#register(Class)/*ww w .j a v a 2 s. c om*/ */ private static DataSerializer newInstance(Class c) { if (!DataSerializer.class.isAssignableFrom(c)) { throw new IllegalArgumentException(LocalizedStrings.DataSerializer_0_DOES_NOT_EXTEND_DATASERIALIZER .toLocalizedString(c.getName())); } Constructor init; try { init = c.getDeclaredConstructor(new Class[0]); } catch (NoSuchMethodException ignored) { StringId s = LocalizedStrings.DataSerializer_CLASS_0_DOES_NOT_HAVE_A_ZEROARGUMENT_CONSTRUCTOR; Object[] args = new Object[] { c.getName() }; if (c.getDeclaringClass() != null) { s = LocalizedStrings.DataSerializer_CLASS_0_DOES_NOT_HAVE_A_ZEROARGUMENT_CONSTRUCTOR_IT_IS_AN_INNER_CLASS_OF_1_SHOULD_IT_BE_A_STATIC_INNER_CLASS; args = new Object[] { c.getName(), c.getDeclaringClass() }; } throw new IllegalArgumentException(s.toLocalizedString(args)); } DataSerializer s; try { init.setAccessible(true); s = (DataSerializer) init.newInstance(new Object[0]); } catch (IllegalAccessException ignored) { throw new IllegalArgumentException( LocalizedStrings.DataSerializer_COULD_NOT_INSTANTIATE_AN_INSTANCE_OF_0 .toLocalizedString(c.getName())); } catch (InstantiationException ex) { throw new IllegalArgumentException( LocalizedStrings.DataSerializer_COULD_NOT_INSTANTIATE_AN_INSTANCE_OF_0 .toLocalizedString(c.getName()), ex); } catch (InvocationTargetException ex) { throw new IllegalArgumentException(LocalizedStrings.DataSerializer_WHILE_INSTANTIATING_AN_INSTANCE_OF_0 .toLocalizedString(c.getName()), ex); } return s; }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#ceil(java.lang.Object, java.lang.Number, java.lang.Class)}. *///from w w w . ja v a2 s. com @SuppressWarnings("unchecked") @Test public void testCeilObjectNumberClassOfT() { assertEquals("null", null, ceil(null, null, null)); assertEquals("null", null, ceil(null, 2, null)); assertEquals("null", 4f, ceil(3.14f, 0, null)); assertEquals("null", 3.15d, ceil(Math.PI, 2, null)); for (Class<?> type : NUMBERS) { try { Object expected = null; Class<? extends Number> typeOfN = (Class<? extends Number>) type; Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type); if (Double.class.equals(wrapper)) { expected = 3.15d; } else if (Float.class.equals(wrapper)) { expected = 3.15f; } else if (BigDecimal.class.equals(wrapper)) { expected = new BigDecimal("3.15"); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "3"); } else { Constructor<?> c = type.getDeclaredConstructor(String.class); expected = c.newInstance("3"); } for (Class<?> scaleType : OBJECTS) { Object o = null; if (ClassUtils.isPrimitiveWrapper(scaleType)) { o = scaleType.getMethod("valueOf", String.class).invoke(null, "2"); } else { Constructor<?> c = scaleType.getDeclaredConstructor(String.class); o = c.newInstance("2"); } assertEquals( "PI: Float: " + type.getSimpleName() + "(scale: " + scaleType.getSimpleName() + ")", expected, ceil(3.141592653589793f, (Number) o, typeOfN)); assertEquals( "PI: Double: " + type.getSimpleName() + "(scale: " + scaleType.getSimpleName() + ")", expected, ceil(Math.PI, (Number) o, typeOfN)); assertEquals("PI: BigDecimal: " + type.getSimpleName() + "(scale: " + scaleType.getSimpleName() + ")", expected, ceil(new BigDecimal(Math.PI), (Number) o, typeOfN)); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } } }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#floor(java.lang.Object, java.lang.Number, java.lang.Class)}. *///w w w. j a v a 2 s .co m @SuppressWarnings("unchecked") @Test public void testFloorObjectNumberClassOfT() { assertEquals("null", null, floor(null, null, null)); assertEquals("null", null, floor(null, 2, null)); assertEquals("null", 3f, floor(3.14f, 0, null)); assertEquals("null", 3.14d, floor(Math.PI, 2, null)); for (Class<?> type : NUMBERS) { try { Object expected = null; Class<? extends Number> typeOfN = (Class<? extends Number>) type; Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type); if (Double.class.equals(wrapper)) { expected = 3.14d; } else if (Float.class.equals(wrapper)) { expected = 3.14f; } else if (BigDecimal.class.equals(wrapper)) { expected = new BigDecimal("3.14"); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "3"); } else { Constructor<?> c = type.getDeclaredConstructor(String.class); expected = c.newInstance("3"); } for (Class<?> scaleType : OBJECTS) { Object o = null; if (ClassUtils.isPrimitiveWrapper(scaleType)) { o = scaleType.getMethod("valueOf", String.class).invoke(null, "2"); } else { Constructor<?> c = scaleType.getDeclaredConstructor(String.class); o = c.newInstance("2"); } assertEquals( "PI: Float: " + type.getSimpleName() + "(scale: " + scaleType.getSimpleName() + ")", expected, floor(3.141592653589793f, (Number) o, typeOfN)); assertEquals( "PI: Double: " + type.getSimpleName() + "(scale: " + scaleType.getSimpleName() + ")", expected, floor(Math.PI, (Number) o, typeOfN)); assertEquals("PI: BigDecimal: " + type.getSimpleName() + "(scale: " + scaleType.getSimpleName() + ")", expected, floor(new BigDecimal(Math.PI), (Number) o, typeOfN)); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } } }
From source file:hu.bme.mit.sette.common.model.snippet.Snippet.java
/** * Parses a method which should be considered in coverage. * * @param includedClass/*from ww w . ja v a 2 s .c om*/ * the Java class of the method * @param includedMethodString * the string representing the included method * @param v * a {@link MethodValidator} * @param classLoader * the class loader for loading snippet project classes */ private void parseIncludedMethod(final Class<?> includedClass, final String includedMethodString, final MethodValidator v, final ClassLoader classLoader) { Matcher matcher = Snippet.METHOD_STRING_PATTERN.matcher(includedMethodString); if (!matcher.matches() || matcher.groupCount() != 2) { // invalid method string String message = String.format("The included method string must match " + "the required format.\n" + "(includedMethodString: [%s])", includedMethodString); v.addException(message); } else { // valid method string String includedMethodName = matcher.group(1).trim(); String[] paramTypeStrings = StringUtils.split(matcher.group(2), ','); Class<?>[] paramTypes = new Class<?>[paramTypeStrings.length]; boolean isConstructor = includedMethodName.equals(includedClass.getSimpleName()); boolean shouldAdd = true; // only add if there was no problem with // the parameters // check parameter types for (int i = 0; i < paramTypes.length; i++) { String parameterTypeString = paramTypeStrings[i].trim(); if (StringUtils.isBlank(parameterTypeString)) { // blank parameter type string String message = String.format("The included method string has " + "a blank parameter type.\n" + "(includedMethodString: [%s])\n" + "(index: [%d])", includedMethodString, i); v.addException(message); shouldAdd = false; } else { try { paramTypes[i] = ClassUtils.getClass(classLoader, parameterTypeString); } catch (ClassNotFoundException e) { // parameter type was not found String format = "The parameter type in " + "the included method string " + "could not have been loaded.\n" + "(includedMethodString: [%s])\n" + "(index: [%d])"; String message = String.format(format, includedMethodString, i); v.addException(message); shouldAdd = false; } } } if (shouldAdd) { // get included method object if (isConstructor) { try { // only search declared constructors Constructor<?> found = includedClass.getDeclaredConstructor(paramTypes); addIncludedConstructor(found, v); } catch (NoSuchMethodException e) { String format = "Included constructor cannot be found " + "(it must be declared in the class)\n" + "(includedClass: [%s])\n" + "(includedMethodString: [%s])"; String message = String.format(format, includedClass, includedMethodString); v.addException(message); } } else { try { // only search declared methods Method found = includedClass.getDeclaredMethod(includedMethodName, paramTypes); addIncludedMethod(found, v); } catch (NoSuchMethodException e) { String format = "Included method cannot be found " + "(it must be declared in the class)\n" + "(includedClass: [%s])\n" + "(includedMethodString: [%s])"; String message = String.format(format, includedClass, includedMethodString); v.addException(message, e); } } } } }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#valueOf(java.lang.Object, java.lang.Class)}. *//*from w w w . j a v a 2 s.c om*/ @SuppressWarnings("unchecked") @Test public void testValueOfObjectClassOfT() { assertEquals("null", valueOf(null, null, false), valueOf(null, null)); try { for (Class<?> type : NUMBERS) { Object o = null; assertEquals("null: " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = Float.NaN; assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = Double.NaN; assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); for (Class<?> valueType : OBJECTS) { if (ClassUtils.isPrimitiveWrapper(valueType)) { o = valueType.getMethod("valueOf", String.class).invoke(null, "1"); } else { o = valueType.getField("ONE").get(null); } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); } for (Class<?> valueType : OBJECTS) { if (ClassUtils.isPrimitiveWrapper(valueType)) { o = valueType.getMethod("valueOf", String.class).invoke(null, "-123"); } else { Constructor<?> c = valueType.getDeclaredConstructor(String.class); o = c.newInstance("-123"); } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); } for (Class<?> valueType : OBJECTS) { if (ObjectUtils.isAny(valueType, Double.class, Float.class)) { o = valueType.getMethod("valueOf", String.class).invoke(null, "123.456"); } else if (BigDecimal.class.equals(valueType)) { Constructor<?> c = valueType.getDeclaredConstructor(String.class); o = c.newInstance("123.456"); } else { continue; } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); } for (Class<?> valueType : OBJECTS) { if (ObjectUtils.isAny(valueType, Double.class, Float.class)) { o = valueType.getMethod("valueOf", String.class).invoke(null, "-123.456"); } else if (BigDecimal.class.equals(valueType)) { Constructor<?> c = valueType.getDeclaredConstructor(String.class); o = c.newInstance("-123.456"); } else { continue; } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); } o = INFINITY_DOUBLE.pow(2); assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = INFINITY_DOUBLE.pow(2).negate(); assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = INFINITY_DOUBLE.pow(2).toBigInteger(); assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = INFINITY_DOUBLE.pow(2).toBigInteger().negate(); assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = ""; assertEquals("\"" + o + "\": " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = "1"; assertEquals("\"" + o + "\": " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = "-123456E-3"; assertEquals("\"" + o + "\": " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = "Infinity"; assertEquals("\"" + o + "\": " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); o = "-Infinity"; assertEquals("\"" + o + "\": " + type.getSimpleName(), valueOf(o, (Class<? extends Number>) type, false), valueOf(o, (Class<? extends Number>) type)); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#valueOf(java.lang.Object)}. *//*from w w w. j ava 2s.c om*/ @SuppressWarnings("unchecked") @Test public void testValueOfObject() { try { for (Class<?> type : NUMBERS) { Object o = null; Object expected = null; Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type); assertEquals("null", expected, valueOf(null)); o = Float.NaN; expected = o; assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o)); o = Double.NaN; expected = o; assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o)); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { o = wrapper.getMethod("valueOf", String.class).invoke(null, "123.456"); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { o = wrapper.getMethod("valueOf", String.class).invoke(null, "123"); } else { Constructor<?> c = type.getDeclaredConstructor(String.class); o = c.newInstance(BigInteger.class.equals(type) ? "123" : "123.456"); } expected = o; assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o)); o = INFINITY_DOUBLE.pow(2); expected = o; assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), o, valueOf(o)); o = INFINITY_DOUBLE.pow(2).negate(); expected = o; assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), o, valueOf(o)); o = INFINITY_DOUBLE.pow(2).toBigInteger(); expected = o; assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), o, valueOf(o)); o = INFINITY_DOUBLE.pow(2).toBigInteger().negate(); expected = o; assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), o, valueOf(o)); o = ""; expected = null; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "not a number."; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = new Object[] { 1, 2, 3 }; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "NaN"; expected = Double.NaN; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "Infinity"; expected = Double.POSITIVE_INFINITY; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "-Infinity"; expected = Double.NEGATIVE_INFINITY; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "123.456f"; expected = (Float) 123.456f; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "123.456d"; expected = (Double) 123.456d; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "1.23456E2d"; expected = (Double) 123.456d; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "1.23456E+2d"; expected = (Double) 123.456d; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "1000000000000"; expected = 1000000000000L; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "1E12"; expected = (Float) 1000000000000f; assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); o = "1E600"; expected = new BigDecimal("1E600"); assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o)); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }
From source file:org.opencms.search.CmsSearchManager.java
/** * Returns an analyzer for the given class name.<p> * // ww w .ja va 2s.com * Since Lucene 3.0, many analyzers require a "version" parameter in the constructor and * can not be created by a simple <code>newInstance()</code> call. * This method will create analyzers by name for the {@link CmsSearchIndex#LUCENE_VERSION} version.<p> * * @param className the class name of the analyzer * @param stemmer the optional stemmer parameter required for the snowball analyzer * * @return the appropriate lucene analyzer * * @throws Exception if something goes wrong * * @deprecated The stemmer parameter is used only by the snownall analyzer, which is deprecated in Lucene 3. */ @Deprecated public static Analyzer getAnalyzer(String className, String stemmer) throws Exception { Analyzer analyzer = null; Class<?> analyzerClass; try { analyzerClass = Class.forName(className); } catch (ClassNotFoundException e) { // allow Lucene standard classes to be written in a short form analyzerClass = Class.forName(LUCENE_ANALYZER + className); } // since Lucene 3.0 most analyzers need a "version" parameter and don't support an empty constructor if (StandardAnalyzer.class.equals(analyzerClass)) { // the Lucene standard analyzer is used analyzer = new StandardAnalyzer(CmsSearchIndex.LUCENE_VERSION); } else if (CmsGallerySearchAnalyzer.class.equals(analyzerClass)) { // OpenCms gallery multiple language analyzer analyzer = new CmsGallerySearchAnalyzer(CmsSearchIndex.LUCENE_VERSION); } else { boolean hasEmpty = false; boolean hasVersion = false; boolean hasVersionWithString = false; // another analyzer is used, check if we find a suitable constructor Constructor<?>[] constructors = analyzerClass.getConstructors(); for (int i = 0; i < constructors.length; i++) { Constructor<?> c = constructors[i]; Class<?>[] parameters = c.getParameterTypes(); if (parameters.length == 0) { // an empty constructor has been found hasEmpty = true; } if ((parameters.length == 1) && parameters[0].equals(Version.class)) { // a constructor with a Lucene version parameter has been found hasVersion = true; } if ((stemmer != null) && (parameters.length == 2) && parameters[0].equals(Version.class) && parameters[1].equals(String.class)) { // a constructor with a Lucene version parameter and a String has been found hasVersionWithString = true; } } if (hasVersionWithString) { // a constructor with a Lucene version parameter and a String has been found analyzer = (Analyzer) analyzerClass .getDeclaredConstructor(new Class[] { Version.class, String.class }) .newInstance(CmsSearchIndex.LUCENE_VERSION, stemmer); } else if (hasVersion) { // a constructor with a Lucene version parameter has been found analyzer = (Analyzer) analyzerClass.getDeclaredConstructor(new Class[] { Version.class }) .newInstance(CmsSearchIndex.LUCENE_VERSION); } else if (hasEmpty) { // an empty constructor has been found analyzer = (Analyzer) analyzerClass.newInstance(); } } return analyzer; }
From source file:jp.co.acroquest.jsonic.JSON.java
protected <T> T create(Context context, Class<? extends T> c) throws Exception { Object instance = null;// w w w. jav a 2 s .co m JSONHint hint = context.getHint(); if (hint != null && hint.type() != Object.class) c = hint.type().asSubclass(c); if (c.isInterface()) { if (SortedMap.class.equals(c)) { instance = new TreeMap<Object, Object>(); } else if (Map.class.equals(c)) { instance = new LinkedHashMap<Object, Object>(); } else if (SortedSet.class.equals(c)) { instance = new TreeSet<Object>(); } else if (Set.class.equals(c)) { instance = new LinkedHashSet<Object>(); } else if (List.class.equals(c)) { instance = new ArrayList<Object>(); } else if (Collection.class.equals(c)) { instance = new ArrayList<Object>(); } else if (Appendable.class.equals(c)) { instance = new StringBuilder(); } } else if (Modifier.isAbstract(c.getModifiers())) { if (Calendar.class.equals(c)) { instance = Calendar.getInstance(); } } else if ((c.isMemberClass() || c.isAnonymousClass()) && !Modifier.isStatic(c.getModifiers())) { Class<?> eClass = c.getEnclosingClass(); Constructor<?> con = c.getDeclaredConstructor(eClass); con.setAccessible(true); if (context.contextObject != null && eClass.isAssignableFrom(context.contextObject.getClass())) { instance = con.newInstance(context.contextObject); } else { instance = con.newInstance((Object) null); } } else { if (Date.class.isAssignableFrom(c)) { try { Constructor<?> con = c.getDeclaredConstructor(long.class); con.setAccessible(true); instance = con.newInstance(0l); } catch (NoSuchMethodException e) { // no handle } } if (instance == null) { Constructor<?> con = c.getDeclaredConstructor(); con.setAccessible(true); instance = con.newInstance(); } } return c.cast(instance); }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#valueOf(java.lang.Object, java.lang.Class, boolean)}. *//*from www . j av a 2 s.c o m*/ @SuppressWarnings("unchecked") @Test public void testValueOfObjectClassOfTBoolean() { assertEquals("null", null, valueOf(null, null, false)); assertEquals("null: fallback", null, valueOf(null, null, true)); try { for (Class<?> type : NUMBERS) { Object o = null; Object expected = null; Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type); if (type.isPrimitive()) expected = wrapper.getMethod("valueOf", String.class).invoke(null, "0"); assertEquals("null: " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "0"); } else { expected = wrapper.getField("ZERO").get(null); } assertEquals("null: fallback: " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("null: typeEquals: " + type.getSimpleName(), ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = Float.NaN; expected = !type.isPrimitive() ? null : wrapper.getMethod("valueOf", String.class).invoke(null, "0"); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) expected = wrapper.getMethod("valueOf", String.class).invoke(null, "NaN"); assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "NaN"); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "0"); } else { expected = wrapper.getField("ZERO").get(null); } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals(o + "(" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = Double.NaN; expected = !type.isPrimitive() ? null : wrapper.getMethod("valueOf", String.class).invoke(null, "0"); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) expected = wrapper.getMethod("valueOf", String.class).invoke(null, "NaN"); assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "NaN"); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "0"); } else { expected = wrapper.getField("ZERO").get(null); } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals(o + "(" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "1"); } else { expected = wrapper.getField("ONE").get(null); } for (Class<?> valueType : OBJECTS) { if (ClassUtils.isPrimitiveWrapper(valueType)) { o = valueType.getMethod("valueOf", String.class).invoke(null, "1"); } else { o = valueType.getField("ONE").get(null); } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals(o + "(" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); } if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "-123"); } else { Constructor<?> c = type.getDeclaredConstructor(String.class); expected = c.newInstance("-123"); } for (Class<?> valueType : OBJECTS) { if (ClassUtils.isPrimitiveWrapper(valueType)) { o = valueType.getMethod("valueOf", String.class).invoke(null, "-123"); } else { Constructor<?> c = valueType.getDeclaredConstructor(String.class); o = c.newInstance("-123"); } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals(o + "(" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); } if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "123.456"); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "123"); } else { Constructor<?> c = type.getDeclaredConstructor(String.class); expected = c.newInstance(BigInteger.class.equals(type) ? "123" : "123.456"); } for (Class<?> valueType : OBJECTS) { if (ObjectUtils.isAny(valueType, Double.class, Float.class)) { o = valueType.getMethod("valueOf", String.class).invoke(null, "123.456"); } else if (BigDecimal.class.equals(valueType)) { Constructor<?> c = valueType.getDeclaredConstructor(String.class); o = c.newInstance("123.456"); } else { continue; } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals(o + "(" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); } if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "-123.456"); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "-123"); } else { Constructor<?> c = type.getDeclaredConstructor(String.class); expected = c.newInstance(BigInteger.class.equals(type) ? "-123" : "-123.456"); } for (Class<?> valueType : OBJECTS) { if (ObjectUtils.isAny(valueType, Double.class, Float.class)) { o = valueType.getMethod("valueOf", String.class).invoke(null, "-123.456"); } else if (BigDecimal.class.equals(valueType)) { Constructor<?> c = valueType.getDeclaredConstructor(String.class); o = c.newInstance("-123.456"); } else { continue; } assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals(o + "(" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals(o + "(" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); } o = INFINITY_DOUBLE.pow(2); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getField("POSITIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MAX_VALUE").get(null); } else { expected = BigInteger.class.equals(type) ? INFINITY_DOUBLE.pow(2).toBigInteger() : INFINITY_DOUBLE.pow(2); } assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("Huge: (" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = INFINITY_DOUBLE.pow(2).negate(); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getField("NEGATIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MIN_VALUE").get(null); } else { expected = BigInteger.class.equals(type) ? INFINITY_DOUBLE.pow(2).toBigInteger().negate() : INFINITY_DOUBLE.pow(2).negate(); } assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("Huge: (" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = INFINITY_DOUBLE.pow(2).toBigInteger(); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getField("POSITIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MAX_VALUE").get(null); } else { expected = BigInteger.class.equals(type) ? INFINITY_DOUBLE.pow(2).toBigInteger() : INFINITY_DOUBLE.pow(2); } assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("Huge: (" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = INFINITY_DOUBLE.pow(2).toBigInteger().negate(); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getField("NEGATIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MIN_VALUE").get(null); } else { expected = BigInteger.class.equals(type) ? INFINITY_DOUBLE.pow(2).toBigInteger().negate() : INFINITY_DOUBLE.pow(2).negate(); } assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals("Huge: (" + o.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("Huge: (" + o.getClass() + "): typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = ""; expected = !type.isPrimitive() ? null : wrapper.getMethod("valueOf", String.class).invoke(null, "0"); assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "0"); } else { expected = wrapper.getField("ZERO").get(null); } assertEquals("\"" + o + "\": fallback: " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("\"" + o + "\": typeEquals: " + type.getSimpleName(), ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = "1"; if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "1"); } else { expected = wrapper.getField("ONE").get(null); } assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("\"" + o + "\": typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = "-123456E-3"; if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "-123.456"); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "-123"); } else { Constructor<?> c = type.getDeclaredConstructor(String.class); expected = c.newInstance(BigInteger.class.equals(type) ? "-123" : "-123.456"); } assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("\"" + o + "\": typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = "Infinity"; if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getField("POSITIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MAX_VALUE").get(null); } else { expected = BigInteger.class.equals(type) ? INFINITY_DOUBLE.toBigInteger() : INFINITY_DOUBLE; } assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("\"" + o + "\": typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); o = "-Infinity"; if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getField("NEGATIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MIN_VALUE").get(null); } else { expected = BigInteger.class.equals(type) ? INFINITY_DOUBLE.toBigInteger().negate() : INFINITY_DOUBLE.negate(); } assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, false)); assertEquals("\"" + o + "\": " + type.getSimpleName(), expected, valueOf(o, (Class<? extends Number>) type, true)); assertEquals("\"" + o + "\": typeEquals: " + type, ClassUtils.primitiveToWrapper(expected.getClass()), valueOf(o, (Class<? extends Number>) type, true).getClass()); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }