List of usage examples for java.lang Class getField
@CallerSensitive public Field getField(String name) throws NoSuchFieldException, SecurityException
From source file:egovframework.rte.itl.webservice.EgovWebServiceContext.java
/** * ?.// w w w . jav a 2s .c o m */ protected void initServerInfo() { LOG.debug("Initialzise Server Info"); List<WebServiceServerDefinition> webServiceServerDefinitions = new ArrayList<WebServiceServerDefinition>(); List<IntegrationDefinition> integrationDefinitions = integrationDefinitionDao .getIntegrationDefinitionOfProvider(organizationId, systemId); LOG.debug("Scan IntegrationDefinitions"); for (IntegrationDefinition integrationDefinition : integrationDefinitions) { LOG.debug("Create Service Info of IntegrationDefinition(" + integrationDefinition + ")"); if (integrationDefinition == null) { LOG.error("IntegrationDefinition is null"); continue; } else if (integrationDefinition.isValid() == false) { LOG.error("IntegrationDefinition is invalid"); continue; } else if (integrationDefinition.isUsing() == false) { LOG.info("IntegrationDefinition(" + integrationDefinition + ") is not usable"); continue; } else if (integrationDefinition.getProvider().isUsing() == false) { LOG.info("IntegrationDefinition(" + integrationDefinition + ")'s provider service is not usable"); continue; } WebServiceServerDefinition webServiceServerDefinition = webServiceServerDefinitionDao .getWebServiceServerDefinition(integrationDefinition.getProvider()); if (webServiceServerDefinition == null) { LOG.error("WebServiceServerDefinition of IntegrationDefinition(" + integrationDefinition + ") does not exist."); continue; } else if (webServiceServerDefinition.isValid() == false) { LOG.error("WebServiceServerDefinition of IntegrationDefinition(" + integrationDefinition + ") is invalid"); continue; } if (webServiceServerDefinitions.contains(webServiceServerDefinition) == false) { webServiceServerDefinitions.add(webServiceServerDefinition); } } LOG.debug("Create WebService Server Module"); serverList = new ArrayList<ServerInfo>(); for (WebServiceServerDefinition webServiceServerDefinition : webServiceServerDefinitions) { LOG.debug("webServiceServerDefinition = " + webServiceServerDefinition); RecordType requestType = null; try { requestType = (RecordType) typeLoader .getType(webServiceServerDefinition.getServiceDefinition().getRequestMessageTypeId()); } catch (NoSuchTypeException e) { LOG.error("RequestMessageType RecordType(id = \"" + webServiceServerDefinition.getServiceDefinition().getRequestMessageTypeId() + "\")'s definition does not exist.", e); continue; } catch (CircularInheritanceException e) { LOG.error("RequestMessageType RecordType(id = \"" + webServiceServerDefinition.getServiceDefinition().getRequestMessageTypeId() + "\") has circular inheritance.", e); continue; } LOG.debug("RequestMesageType = " + requestType); RecordType responseType = null; try { responseType = (RecordType) typeLoader .getType(webServiceServerDefinition.getServiceDefinition().getResponseMessageTypeId()); } catch (NoSuchTypeException e) { LOG.error("ResponseMessageType RecordType(id = \"" + webServiceServerDefinition.getServiceDefinition().getResponseMessageTypeId() + "\")'s definition does not exist.", e); continue; } catch (CircularInheritanceException e) { LOG.error("ResponseMessageType RecordType(id = \"" + webServiceServerDefinition.getServiceDefinition().getResponseMessageTypeId() + "\") has circular inheritance.", e); continue; } LOG.debug("ResponseMessageType = " + responseType); ServiceEndpointInfo serviceEndpointInfo = null; try { serviceEndpointInfo = new ServiceEndpointInfoImpl(webServiceServerDefinition, requestType, responseType); } catch (IllegalArgumentException e) { LOG.error("Cannot create ServiceEndpointInfoImpl", e); continue; } LOG.debug("ServiceEndpointInfo = " + serviceEndpointInfo); Class<?> serviceImplClass = null; try { serviceImplClass = classLoader.loadClass(serviceEndpointInfo); } catch (ClassNotFoundException e) { LOG.error("Cannot load ServerEndpoint Class", e); continue; } LOG.debug("ServiceEndpoint Class = " + serviceImplClass); Object serviceImpl = null; try { serviceImpl = serviceImplClass.newInstance(); } catch (IllegalAccessException e) { LOG.error("Cannot instantiate ServiceEndpoint", e); continue; } catch (InstantiationException e) { LOG.error("Cannot instantiate ServiceEndpoint", e); continue; } LOG.debug("ServiceEndpoint instance = " + serviceImpl); EgovIntegrationServiceProvider provider = null; try { provider = (EgovIntegrationServiceProvider) applicationContext .getBean(webServiceServerDefinition.getServiceDefinition().getServiceProviderBeanId()); } catch (BeansException e) { LOG.error("Cannot get providerBean(id = \"" + webServiceServerDefinition.getServiceDefinition().getServiceProviderBeanId() + "\")", e); continue; } LOG.debug("EgovIntegrationServiceProvider = " + provider); ServiceBridge serviceBridge = null; try { serviceBridge = new ServiceBridgeImpl(provider, serviceEndpointInfo, messageConverter); } catch (IllegalArgumentException e) { LOG.error("Cannot create ServiceBridge", e); continue; } LOG.debug("ServiceBridge = " + serviceBridge); Field fieldServiceBridge = null; String fieldName = classLoader.getFieldNameOfServiceBridge(); try { fieldServiceBridge = serviceImplClass.getField(fieldName); } catch (NoSuchFieldException e) { LOG.error("ServiceEndpoint does not have the field(name = \"" + fieldName + "\")", e); continue; } try { fieldServiceBridge.set(serviceImpl, serviceBridge); } catch (IllegalAccessException e) { LOG.error("Cannot set ServiceBridge to provider", e); continue; } catch (IllegalArgumentException e) { LOG.error("Cannot set ServiceBridge to provider", e); continue; } catch (SecurityException e) { LOG.error("Cannot set ServiceBridge to provider", e); continue; } LOG.debug("Add new ServiceInfo"); serverList.add(new ServerInfo(webServiceServerDefinition.getAddress(), serviceImpl)); } LOG.debug("Finished Initializing Server Info"); }
From source file:xj.property.activity.HXBaseActivity.ChatActivity.java
/** * ?gridview?view/* ww w . j ava 2 s .co m*/ * * @param i * @return */ private View getGridChildView(int i) { View view = View.inflate(this, R.layout.expression_gridview, null); ExpandGridView gv = (ExpandGridView) view.findViewById(R.id.gridview); List<String> list = new ArrayList<String>(); if (i == 1) { List<String> list1 = reslist.subList(0, 20);/// 20/27//34 list.addAll(list1); } else if (i == 2) { list.addAll(reslist.subList(20, 40)); } else if (i == 3) { list.addAll(reslist.subList(40, 60)); } else if (i == 4) { list.addAll(reslist.subList(60, 80)); } else if (i == 5) { list.addAll(reslist.subList(80, reslist.size())); } list.add("delete_expression"); final ExpressionAdapter expressionAdapter = new ExpressionAdapter(this, 1, list); gv.setAdapter(expressionAdapter); gv.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { String filename = expressionAdapter.getItem(position); try { // ???? // ????? if (buttonSetModeKeyboard.getVisibility() != View.VISIBLE) { if (filename != "delete_expression") { // ? // ????SmileUtils Class clz = Class.forName("xj.property.utils.SmileUtils"); Field field = clz.getField(filename); int selectionStart = mEditTextContent.getSelectionStart();// ?? Spannable smiledText = SmileUtils.getSmiledText(getmContext(), (String) field.get(null)); Editable editableText = mEditTextContent.getEditableText(); editableText.insert(selectionStart, smiledText); // mEditTextContent.append(SmileUtils.getSmiledText(getmContext(), (String) field.get(null))); } else { // if (!TextUtils.isEmpty(mEditTextContent.getText())) { int selectionStart = mEditTextContent.getSelectionStart();// ?? if (selectionStart > 0) { String body = mEditTextContent.getText().toString(); String tempStr = body.substring(0, selectionStart); int i = tempStr.lastIndexOf("[");// ??? if (i != -1) { /// ?? CharSequence cs = tempStr.substring(i, selectionStart); if (SmileUtils.containsKey(cs.toString())) /// ??? mEditTextContent.getEditableText().delete(i, selectionStart); else mEditTextContent.getEditableText().delete(selectionStart - 1, selectionStart); } else { mEditTextContent.getEditableText().delete(selectionStart - 1, selectionStart); } } } } } } catch (Exception e) { e.printStackTrace(); } } }); return view; }
From source file:gda.plots.SimplePlot.java
/** * A tricky little service method which searches the static int fields of a class for one called fieldName and * returns its integer value.//from w w w .j a v a 2s . co m * * @param fieldName * the field name to get * @param theClass * find in this class * @return the integer value of the named field or -1 if not found */ public static int getIntFieldFromClass(String fieldName, Class<?> theClass) { Integer found; int field = -1; try { found = (Integer) theClass.getField(fieldName.toUpperCase()).get(null); field = found.intValue(); } catch (NoSuchFieldException e) { logger.error("Class " + theClass + " does not contain " + fieldName); } catch (IllegalAccessException e) { logger.error("Class " + theClass + " will not allow access"); } return field; }
From source file:com.sos.JSHelper.Options.JSOptionsClass.java
private String getOptionValue(final Class c, final String pstrOptionName) { @SuppressWarnings("unused") final String conMethodName = conClassName + "::getOptionValue"; String strValue = null;//from w w w . j ava 2s . co m Field objField = null; try { objField = c.getField(pstrOptionName); Object objO = objField.get(this); if (objO instanceof String) { strValue = (String) objField.get(this); } else { if (objO instanceof SOSOptionElement) { SOSOptionElement objDE = (SOSOptionElement) objO; strValue = objDE.Value(); } } } // try catch (final NoSuchFieldException objException) { Method objMethod; try { /** * Die Methode, die gesucht wird, hat keine Parameter, weil * diese ein "getter" ist. * Deshalb in "getMethod" als zweites Argument "null", andernfalls * erwischt die JVM eine andere Methode (zum Beispiel den "setter"). */ objMethod = c.getMethod(pstrOptionName, null); /** * ein "getter" hat keine Parameter, deshalb auch hier wieder * als zweites Argument "null" angeben. */ strValue = (String) objMethod.invoke(this, null); } catch (final SecurityException exception) { exception.printStackTrace(); } catch (final NoSuchMethodException exception) { // c = super.getClass(); // try { // objMethod = c.getMethod(strOptionName, null); // strValue = (String) objMethod.invoke(this, null); // } // try // catch (Exception objExc) { // objExc.printStackTrace(); // } // finally { // // // } // finally } catch (final IllegalArgumentException exception) { exception.printStackTrace(); } catch (final InvocationTargetException exception) { exception.printStackTrace(); } catch (final IllegalAccessException exception) { exception.printStackTrace(); } } catch (final IllegalAccessException exception) { exception.printStackTrace(); } return strValue; }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#valueOf(java.lang.Object, java.lang.Class, boolean)}. *//*from w w w . ja va 2 s . com*/ @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())); } }
From source file:jp.furplag.util.commons.NumberUtilsTest.java
/** * {@link jp.furplag.util.commons.NumberUtils#add(java.lang.Object, java.lang.Number, java.lang.Class)}. *//* w w w . j a v a 2s . co m*/ @SuppressWarnings("unchecked") @Test public void testAddObjectNumberClassOfT() { assertEquals("null", null, add(null, null, null)); assertEquals("null", null, add("123.456", null, null)); assertEquals("null", null, add("123.456", 1, null)); assertEquals("null", null, add("", 10, null)); assertEquals("null", null, add(null, 10, null)); assertEquals("null", null, add(123.456, 10, null)); assertEquals("123 + .456: Float", (Object) 123.456f, add(123, .456, Float.class)); assertEquals("123 + .456: Float", (Object) Float.class, add(123, .456, Float.class).getClass()); assertEquals("123 + .456: Float", (Object) Float.class, add(123L, .456d, Float.class).getClass()); 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); assertEquals("null: default: " + type.getSimpleName(), valueOf(null, typeOfN), add(null, null, typeOfN)); assertEquals("123.456: " + type.getSimpleName(), valueOf(123 + .456, typeOfN), add("123", .456, typeOfN)); assertEquals("NaN + 123: " + type.getSimpleName(), valueOf(123, typeOfN), add("NaN", 123, typeOfN)); assertEquals("123 + NaN: " + type.getSimpleName(), valueOf("123", typeOfN), add("123", Float.NaN, typeOfN)); assertEquals("invalid + 123: " + type.getSimpleName(), valueOf(123, typeOfN), add("not a number", 123, typeOfN)); if (Double.class.equals(wrapper)) { expected = (wrapper.getField("MAX_VALUE").getDouble(null) * -1) + 123; } else if (Float.class.equals(wrapper)) { expected = Float.NEGATIVE_INFINITY; } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = valueOf("-Infinity", typeOfN); } else { expected = INFINITY_DOUBLE.negate().add(BigDecimal.valueOf(123)); if (BigInteger.class.equals(type)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("-Infinity: Double: + 123: " + type.getSimpleName(), expected, add("-Infinity", 123, typeOfN)); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getField("POSITIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = valueOf("Infinity", typeOfN); } else { expected = INFINITY_DOUBLE.add(BigDecimal.valueOf(123)); if (BigInteger.class.equals(type)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("Infinity: Double: + 123: " + type.getSimpleName(), expected, add("Infinity", 123, typeOfN)); if (Double.class.equals(wrapper)) { expected = (wrapper.getField("MAX_VALUE").getDouble(null) * -1) + 123; } else if (Float.class.equals(wrapper)) { expected = Float.NEGATIVE_INFINITY; } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = valueOf("-Infinity", typeOfN); } else { expected = INFINITY_DOUBLE.negate().add(BigDecimal.valueOf(123)); if (BigInteger.class.equals(type)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("123 - Infinity: Double: " + type.getSimpleName(), expected, add("123", Double.NEGATIVE_INFINITY, typeOfN)); if (ObjectUtils.isAny(wrapper, Double.class, Float.class)) { expected = wrapper.getField("POSITIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = valueOf("Infinity", typeOfN); } else { expected = INFINITY_DOUBLE.add(BigDecimal.valueOf(123)); if (BigInteger.class.equals(type)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("123 + Infinity: Double: " + type.getSimpleName(), expected, add("123", Double.POSITIVE_INFINITY, 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.NumberObject} *///from w ww .ja v a 2 s . c om @SuppressWarnings("unchecked") @Test public void NumberObjectTest() { try { Class<?> numberObject = ClassLoader.getSystemClassLoader() .loadClass(NumberUtils.class.getName() + "$NumberObject"); Constructor<?> c = numberObject.getDeclaredConstructor(Class.class); c.setAccessible(true); Method ofType = numberObject.getMethod("of", Class.class); ofType.setAccessible(true); Method ofN = numberObject.getMethod("of", Number.class); ofN.setAccessible(true); Method parsable = numberObject.getDeclaredMethod("parsable", Number.class); parsable.setAccessible(true); Method contains = numberObject.getDeclaredMethod("contains", Number.class); contains.setAccessible(true); Method valueOf = numberObject.getDeclaredMethod("valueOf", Number.class); valueOf.setAccessible(true); for (Class<?> type : NUMBERS) { Object o = c.newInstance(type); Class<? extends Number> wrapper = (Class<? extends Number>) ClassUtils.primitiveToWrapper(type); Object numob = ofType.invoke(null, type); assertEquals("ofType: " + type.getSimpleName(), o, numob); Number n = null; if (!type.isPrimitive()) { if (ClassUtils.isPrimitiveWrapper(type)) { n = (Number) ClassUtils.primitiveToWrapper(type).getMethod("valueOf", String.class) .invoke(null, "1"); } else { n = (Number) type.getField("ONE").get(null); } if (type.equals(byte.class)) assertEquals("ofN: 1: " + type.getSimpleName(), o, ofN.invoke(null, n)); } assertEquals("parsable: -1: " + type.getSimpleName(), true, parsable.invoke(numob, -1)); assertEquals("parsable: 0: " + type.getSimpleName(), true, parsable.invoke(numob, 0)); assertEquals("parsable: 1: " + type.getSimpleName(), true, parsable.invoke(numob, 1)); assertEquals("parsable: null: " + type.getSimpleName(), !type.isPrimitive(), parsable.invoke(numob, (Number) null)); Object expected = ObjectUtils.isAny(wrapper, Float.class, Double.class, BigDecimal.class, BigInteger.class); assertEquals("parsable: Infinity: Double: " + type.getSimpleName(), expected, parsable.invoke(numob, Double.POSITIVE_INFINITY)); assertEquals("parsable: Infinity: Double: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE)); assertEquals("parsable: Infinity: Double: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE.toBigInteger())); assertEquals("parsable: Infinity: Float: " + type.getSimpleName(), expected, parsable.invoke(numob, Float.POSITIVE_INFINITY)); assertEquals("parsable: Infinity: Float: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT)); assertEquals("parsable: Infinity: Float: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT.toBigInteger())); assertEquals("parsable: -Infinity: Double: " + type.getSimpleName(), expected, parsable.invoke(numob, Double.NEGATIVE_INFINITY)); assertEquals("parsable: -Infinity: Double: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE.negate())); assertEquals("parsable: -Infinity: Double: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_DOUBLE.negate().toBigInteger())); assertEquals("parsable: -Infinity: Float: " + type.getSimpleName(), expected, parsable.invoke(numob, Float.NEGATIVE_INFINITY)); assertEquals("parsable: -Infinity: Float: BigDecimal: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT.negate())); assertEquals("parsable: -Infinity: Float: BigInteger: " + type.getSimpleName(), expected, parsable.invoke(numob, INFINITY_FLOAT.negate().toBigInteger())); expected = ObjectUtils.isAny(wrapper, Float.class, Double.class); assertEquals("parsable: NaN: Float: " + type.getSimpleName(), expected, parsable.invoke(numob, Float.NaN)); assertEquals("parsable: NaN: Double: " + type.getSimpleName(), expected, parsable.invoke(numob, Double.NaN)); if (Byte.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getByte(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getByte(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, Short.MIN_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Short.MAX_VALUE)); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123.456f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getByte(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getByte(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, Short.MIN_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Short.MAX_VALUE)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123.456f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 1234.56f)); } if (Short.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getShort(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getShort(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, Integer.MIN_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Integer.MAX_VALUE)); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123.456f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getShort(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getShort(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, Integer.MIN_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Integer.MAX_VALUE)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 12345.6f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 123456.789f)); } if (Integer.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getInt(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getInt(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, Long.MIN_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Long.MAX_VALUE)); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123456.789f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getInt(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getInt(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, Long.MIN_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Long.MAX_VALUE)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123456.789f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 12345678912345678912.3456d)); } if (Long.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MIN_VALUE").getLong(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getLong(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), false, parsable.invoke(numob, BigInteger.valueOf(Long.MIN_VALUE).pow(2))); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, BigInteger.valueOf(Long.MAX_VALUE).pow(2))); assertEquals("parsable: fraction: " + type.getSimpleName(), false, parsable.invoke(numob, 123.456f)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MIN_VALUE").getLong(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getLong(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, BigInteger.valueOf(Long.MIN_VALUE).pow(2))); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, BigInteger.valueOf(Long.MAX_VALUE).pow(2))); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123456.789f)); assertEquals("contains: overflow: fraction: " + type.getSimpleName(), false, contains.invoke(numob, 12345678912345678912.3456f)); } if (Float.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, -wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, -Double.MAX_VALUE)); assertEquals("parsable: overflow: max: " + type.getSimpleName(), false, parsable.invoke(numob, Double.MAX_VALUE)); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, -wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getFloat(null))); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, -Double.MAX_VALUE)); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, Double.MAX_VALUE)); } if (Double.class.equals(wrapper)) { assertEquals("parsable: contains: min: " + type.getSimpleName(), true, parsable.invoke(numob, -wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("parsable: contains: max: " + type.getSimpleName(), true, parsable.invoke(numob, wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("parsable: overflow: min: " + type.getSimpleName(), true, parsable.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN).negate())); assertEquals("parsable: overflow: max: " + type.getSimpleName(), true, parsable.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN))); assertEquals("contains: min: " + type.getSimpleName(), true, contains.invoke(numob, -wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("contains: max: " + type.getSimpleName(), true, contains.invoke(numob, wrapper.getField("MAX_VALUE").getDouble(null))); assertEquals("contains: overflow: min: " + type.getSimpleName(), false, contains.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN).negate())); assertEquals("contains: overflow: max: " + type.getSimpleName(), false, contains.invoke(numob, INFINITY_DOUBLE.multiply(BigDecimal.TEN))); } if (!ClassUtils.isPrimitiveWrapper(wrapper)) { assertEquals("parsable: fraction: " + type.getSimpleName(), BigDecimal.class.equals(type), parsable.invoke(numob, 123.456f)); assertEquals("contains: fraction: " + type.getSimpleName(), true, contains.invoke(numob, 123.456f)); } if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, "123"); } else { expected = new BigDecimal("123"); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } for (Class<?> valueType : OBJECTS) { if (ClassUtils.isPrimitiveWrapper(valueType)) { n = (Number) valueType.getMethod("valueOf", String.class).invoke(null, "123"); } else { n = new BigDecimal("123"); if (BigInteger.class.equals(valueType)) n = ((BigDecimal) n).toBigInteger(); } assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); } n = 123.456f; if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, n.toString()); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, Integer.toString(((Float) n).intValue())); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); n = 1.23456789E-6d; if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, n.toString()); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getMethod("valueOf", String.class).invoke(null, Integer.toString(((Double) n).intValue())); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: " + n + " (" + n.getClass().getSimpleName() + "): " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals( "valueOf: " + n + " (" + n.getClass().getSimpleName() + "): class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); n = INFINITY_DOUBLE.pow(2); if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getField("POSITIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MAX_VALUE").get(null); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: Huge: " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals("valueOf: Huge: class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); n = INFINITY_DOUBLE.pow(2).negate(); if (ObjectUtils.isAny(wrapper, Float.class, Double.class)) { expected = wrapper.getField("NEGATIVE_INFINITY").get(null); } else if (ClassUtils.isPrimitiveWrapper(wrapper)) { expected = wrapper.getField("MIN_VALUE").get(null); } else { expected = new BigDecimal(n.toString()); if (BigInteger.class.equals(wrapper)) expected = ((BigDecimal) expected).toBigInteger(); } assertEquals("valueOf: Huge: negative: " + type.getSimpleName(), expected, valueOf.invoke(numob, n)); assertEquals("valueOf: Huge: negative: class: " + type.getSimpleName(), expected.getClass(), valueOf.invoke(numob, n).getClass()); } } catch (Exception e) { e.printStackTrace(); fail(e.getMessage() + "\n" + Arrays.toString(e.getStackTrace())); } }
From source file:alice.tuprolog.lib.OOLibrary.java
/** * get the value of the field//from w w w . j av a 2s. co m */ private boolean java_get(PTerm objId, PTerm fieldTerm, PTerm what) { if (!fieldTerm.isAtom()) { return false; } String fieldName = ((Struct) fieldTerm).getName(); Object obj = null; try { Class<?> cl = null; if (objId.isCompound() && ((Struct) objId).getName().equals("class")) { String clName = null; if (((Struct) objId).getArity() == 1) clName = alice.util.Tools.removeApices(((Struct) objId).getArg(0).toString()); if (clName != null) { try { cl = Class.forName(clName, true, dynamicLoader); } catch (ClassNotFoundException ex) { getEngine().logger.warn("Java class not found: " + clName); return false; } catch (Exception ex) { getEngine().logger.warn("Static field " + fieldName + " not found in class " + alice.util.Tools.removeApices(((Struct) objId).getArg(0).toString())); return false; } } } else { String objName = alice.util.Tools.removeApices(objId.toString()); obj = currentObjects.get(objName); if (obj == null) { return false; } cl = obj.getClass(); } Field field = cl.getField(fieldName); Class<?> fc = field.getType(); field.setAccessible(true); if (fc.equals(Integer.TYPE) || fc.equals(Byte.TYPE)) { int value = field.getInt(obj); return unify(what, new alice.tuprolog.Int(value)); } else if (fc.equals(java.lang.Long.TYPE)) { long value = field.getLong(obj); return unify(what, new alice.tuprolog.Long(value)); } else if (fc.equals(java.lang.Float.TYPE)) { float value = field.getFloat(obj); return unify(what, new alice.tuprolog.Float(value)); } else if (fc.equals(java.lang.Double.TYPE)) { double value = field.getDouble(obj); return unify(what, new alice.tuprolog.Double(value)); } else { // the field value is an object Object res = field.get(obj); return bindDynamicObject(what, res); } } catch (NoSuchFieldException ex) { getEngine().logger.warn("Field " + fieldName + " not found in class " + objId); return false; } catch (Exception ex) { getEngine().logger.warn("Generic error in accessing the field"); return false; } }
From source file:org.apache.openejb.server.axis.assembler.HeavyweightTypeInfoBuilder.java
/** * Map the (nested) fields of a XML Schema Type to Java Beans properties or public fields of the specified Java Class. * * @param javaClass the java class to map * @param xmlTypeInfo the xml schema for the type * @param javaXmlTypeMapping the java to xml type mapping metadata * @param typeInfo the JaxRpcTypeInfo for this type * @throws OpenEJBException if the XML Schema Type can not be mapped to the Java Class */// w w w. java 2 s . co m private void mapFields(Class javaClass, XmlTypeInfo xmlTypeInfo, JavaXmlTypeMapping javaXmlTypeMapping, JaxRpcTypeInfo typeInfo) throws OpenEJBException { // Skip arrays since they can't define a variable-mapping element if (!javaClass.isArray()) { // if there is a variable-mapping, log a warning if (!javaXmlTypeMapping.getVariableMapping().isEmpty()) { log.warn("Ignoring variable-mapping defined for class " + javaClass + " which is an array."); } return; } // Index Java bean properties by name Map<String, Class> properties = new HashMap<String, Class>(); try { PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(javaClass).getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { properties.put(propertyDescriptor.getName(), propertyDescriptor.getPropertyType()); } } catch (IntrospectionException e) { throw new OpenEJBException("Class " + javaClass + " is not a valid javabean", e); } for (VariableMapping variableMapping : javaXmlTypeMapping.getVariableMapping()) { String fieldName = variableMapping.getJavaVariableName(); if (variableMapping.getXmlAttributeName() != null) { JaxRpcFieldInfo fieldInfo = new JaxRpcFieldInfo(); fieldInfo.name = fieldName; // verify that the property exists on the java class Class javaType = properties.get(fieldName); if (javaType == null) { throw new OpenEJBException("field name " + fieldName + " not found in " + properties); } String attributeLocalName = variableMapping.getXmlAttributeName(); QName xmlName = new QName("", attributeLocalName); fieldInfo.xmlName = xmlName; fieldInfo.xmlType = xmlTypeInfo.attributes.get(attributeLocalName); if (fieldInfo.xmlType == null) { throw new OpenEJBException( "attribute " + xmlName + " not found in schema " + xmlTypeInfo.qname); } typeInfo.fields.add(fieldInfo); } else { JaxRpcFieldInfo fieldInfo = new JaxRpcFieldInfo(); fieldInfo.isElement = true; fieldInfo.name = fieldName; // verify that the property exists on the java class or there is a public field Class javaType = properties.get(fieldName); if (javaType == null) { //see if it is a public field try { Field field = javaClass.getField(fieldName); javaType = field.getType(); } catch (NoSuchFieldException e) { throw new OpenEJBException("field name " + fieldName + " not found in " + properties, e); } } QName xmlName = new QName("", variableMapping.getXmlElementName()); XmlElementInfo nestedElement = xmlTypeInfo.elements.get(xmlName); if (nestedElement == null) { String ns = xmlTypeInfo.qname.getNamespaceURI(); xmlName = new QName(ns, variableMapping.getXmlElementName()); nestedElement = xmlTypeInfo.elements.get(xmlName); if (nestedElement == null) { throw new OpenEJBException( "element " + xmlName + " not found in schema " + xmlTypeInfo.qname); } } fieldInfo.isNillable = nestedElement.nillable || hasEncoded; fieldInfo.xmlName = xmlName; // xml type if (nestedElement.xmlType != null) { fieldInfo.xmlType = nestedElement.xmlType; } else { QName anonymousName; if (xmlTypeInfo.anonymous) { anonymousName = new QName(xmlTypeInfo.qname.getNamespaceURI(), xmlTypeInfo.qname.getLocalPart() + ">" + nestedElement.qname.getLocalPart()); } else { anonymousName = new QName(xmlTypeInfo.qname.getNamespaceURI(), ">" + xmlTypeInfo.qname.getLocalPart() + ">" + nestedElement.qname.getLocalPart()); } fieldInfo.xmlType = anonymousName; } if (javaType.isArray()) { fieldInfo.minOccurs = nestedElement.minOccurs; fieldInfo.maxOccurs = nestedElement.maxOccurs; } typeInfo.fields.add(fieldInfo); } } }
From source file:alice.tuprolog.lib.OOLibrary.java
/** * set the field value of an object/*from w w w . j av a 2 s . c o m*/ */ private boolean java_set(PTerm objId, PTerm fieldTerm, PTerm what) { what = what.getTerm(); if (!fieldTerm.isAtom() || what instanceof Var) return false; String fieldName = ((Struct) fieldTerm).getName(); Object obj = null; try { Class<?> cl = null; if (objId.isCompound() && ((Struct) objId).getName().equals("class")) { String clName = null; // Case: class(className) if (((Struct) objId).getArity() == 1) clName = alice.util.Tools.removeApices(((Struct) objId).getArg(0).toString()); if (clName != null) { try { cl = Class.forName(clName, true, dynamicLoader); } catch (ClassNotFoundException ex) { getEngine().logger.warn("Java class not found: " + clName); return false; } catch (Exception ex) { getEngine().logger.warn("Static field " + fieldName + " not found in class " + alice.util.Tools.removeApices(((Struct) objId).getArg(0).toString())); return false; } } } else { String objName = alice.util.Tools.removeApices(objId.toString()); obj = currentObjects.get(objName); if (obj != null) { cl = obj.getClass(); } else { return false; } } // first check for primitive data field Field field = cl.getField(fieldName); if (what instanceof Number) { Number wn = (Number) what; if (wn instanceof Int) { field.setInt(obj, wn.intValue()); } else if (wn instanceof alice.tuprolog.Double) { field.setDouble(obj, wn.doubleValue()); } else if (wn instanceof alice.tuprolog.Long) { field.setLong(obj, wn.longValue()); } else if (wn instanceof alice.tuprolog.Float) { field.setFloat(obj, wn.floatValue()); } else { return false; } } else { String what_name = alice.util.Tools.removeApices(what.toString()); Object obj2 = currentObjects.get(what_name); if (obj2 != null) { field.set(obj, obj2); } else { // consider value as a simple string field.set(obj, what_name); } } return true; } catch (NoSuchFieldException ex) { getEngine().logger.warn("Field " + fieldName + " not found in class " + objId); return false; } catch (Exception ex) { return false; } }