List of usage examples for java.lang Class isPrimitive
@HotSpotIntrinsicCandidate public native boolean isPrimitive();
From source file:egovframework.rte.itl.webservice.service.impl.EgovWebServiceClassLoaderImpl.java
private byte[] createServiceEndpointClass(final ServiceEndpointInfo serviceEndpointInfo) throws ClassNotFoundException { String serviceEndpointInterfaceClassName = getServiceEndpointInterfaceClassName( serviceEndpointInfo.getServiceName()); String serviceEndpointClassName = getServiceEndpointClassName(serviceEndpointInfo.getServiceName()); String asmServiceEndpointInterfaceClassName = serviceEndpointInterfaceClassName.replace('.', '/'); String asmServiceEndpointClassName = serviceEndpointClassName.replace('.', '/'); // ClassWriter classWriter = new ClassWriter(false); ClassWriter classWriter = new ClassWriter(0); classWriter.visit(V1_5, // version ACC_PUBLIC, // access asmServiceEndpointClassName, // name null, // signature "java/lang/Object", // superName new String[] { asmServiceEndpointInterfaceClassName }); // interfaces // Create Annotation AnnotationVisitor annotationVisitor = classWriter.visitAnnotation(DESC_OF_WEB_SERVICE, true); annotationVisitor.visit("endpointInterface", serviceEndpointInterfaceClassName); annotationVisitor.visit("targetNamespace", serviceEndpointInfo.getNamespace()); // annotationVisitor.visit("name", // serviceProviderInfo.getServiceName()); annotationVisitor.visit("serviceName", serviceEndpointInfo.getServiceName()); annotationVisitor.visit("portName", serviceEndpointInfo.getPortName()); annotationVisitor.visitEnd();/*from ww w. ja v a2s . c om*/ // Create Attribute FieldVisitor fieldVisitor = classWriter.visitField(ACC_PUBLIC, // access FIELD_NAME_OF_SERVICE_BRIDGE, // name DESC_OF_SERVICE_BRIDGE_CLASS, // desc null, // signature null); // value fieldVisitor.visitEnd(); // Create Constructor MethodVisitor methodVisitor = classWriter.visitMethod(ACC_PUBLIC, // access "<init>", // name "()V", // desc null, // signature null); // exceptions methodVisitor.visitCode(); methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitMethodInsn(INVOKESPECIAL, // opcode "java/lang/Object", // owner "<init>", // name "()V"); // desc methodVisitor.visitInsn(RETURN); methodVisitor.visitMaxs(1, 1); methodVisitor.visitEnd(); // Create Method ServiceParamInfo returnInfo = serviceEndpointInfo.getReturnInfo(); Collection<ServiceParamInfo> paramInfos = serviceEndpointInfo.getParamInfos(); StringBuffer desc = new StringBuffer("("); StringBuffer signature = new StringBuffer("("); for (ServiceParamInfo info : paramInfos) { Class<?> paramClass = loadClass(info.getType()); org.objectweb.asm.Type paramType = org.objectweb.asm.Type.getType(paramClass); String paramSign = paramType.getDescriptor(); if (info.getMode().equals(OUT) || info.getMode().equals(INOUT)) { if (paramClass.isPrimitive()) { paramClass = wrapperClasses.get(paramClass); paramType = org.objectweb.asm.Type.getType(paramClass); paramSign = paramType.getDescriptor(); } paramClass = Holder.class; paramType = TYPE_OF_HOLDER; paramSign = "Ljavax/xml/ws/Holder<" + paramSign + ">;"; } desc.append(paramType.getDescriptor()); signature.append(paramSign); } desc.append(")"); signature.append(")"); org.objectweb.asm.Type returnType = (returnInfo == null ? returnType = org.objectweb.asm.Type.VOID_TYPE : org.objectweb.asm.Type.getType(loadClass(returnInfo.getType()))); desc.append(returnType.getDescriptor()); signature.append(returnType.getDescriptor()); methodVisitor = classWriter.visitMethod(ACC_PUBLIC, // access serviceEndpointInfo.getOperationName(), // name desc.toString(), // desc signature.toString(), // signature null); // exceptions int mapPosition = paramInfos.size() + 1; methodVisitor.visitCode(); methodVisitor.visitTypeInsn(NEW, "java/util/HashMap"); methodVisitor.visitInsn(DUP); methodVisitor.visitMethodInsn(INVOKESPECIAL, // opcode "java/util/HashMap", // owner "<init>", // name "()V"); // desc methodVisitor.visitVarInsn(ASTORE, mapPosition); int i = 1; for (ServiceParamInfo info : paramInfos) { methodVisitor.visitVarInsn(ALOAD, mapPosition); methodVisitor.visitLdcInsn(info.getName()); methodVisitor.visitVarInsn(ALOAD, i); methodVisitor.visitMethodInsn(INVOKEINTERFACE, // opcode "java/util/Map", // owner "put", // name "(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;"); // desc methodVisitor.visitInsn(POP); i++; } methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitFieldInsn(GETFIELD, // opcode asmServiceEndpointClassName, // owner FIELD_NAME_OF_SERVICE_BRIDGE, // name DESC_OF_SERVICE_BRIDGE_CLASS); // desc methodVisitor.visitVarInsn(ALOAD, mapPosition); methodVisitor.visitMethodInsn(INVOKEINTERFACE, // opcode NAME_OF_SERVICE_BRIDGE_CLASS, // owner "doService", // name "(Ljava/util/Map;)Ljava/lang/Object;"); // desc if (returnInfo != null) { methodVisitor.visitTypeInsn(CHECKCAST, // opcode returnType.getInternalName()); // type methodVisitor.visitInsn(ARETURN); } else { methodVisitor.visitInsn(POP); methodVisitor.visitInsn(RETURN); } methodVisitor.visitMaxs(paramInfos.size(), paramInfos.size() + 2); methodVisitor.visitEnd(); // Class finalize classWriter.visitEnd(); // try // { // DataOutputStream dos = new DataOutputStream( // new FileOutputStream("EgovType" + // serviceProviderInfo.getServiceName() + // "Impl.class")); // dos.write(classWriter.toByteArray()); // dos.close(); // } // catch (IOException e) // { // e.printStackTrace(); // } return classWriter.toByteArray(); }
From source file:net.paoding.rose.jade.core.UpdateOperation.java
private Object executeSignle(DataAccess dataAccess, Map<String, Object> parameters, Class<?> returnType) { // ID/* w ww. ja va 2 s . co m*/ ReturnGeneratedKeys genKey = modifier.getAnnotation(ReturnGeneratedKeys.class); if (genKey != null) { // INSERT ID Number number = dataAccess.insertReturnId(sql, modifier, parameters); return NumberUtil.convertNumberToTargetClass(number, returnType); } if (returnType == Identity.class) { // INSERT ID Number number = dataAccess.insertReturnId(sql, modifier, parameters); // ? return new Identity(number); } else { // UPDATE / DELETE int updated = dataAccess.update(sql, modifier, parameters); // ? if (returnType.isPrimitive()) { returnType = ClassUtils.primitiveToWrapper(returnType); } // ? if (returnType == Boolean.class) { return Boolean.valueOf(updated > 0); } else if (returnType == Long.class) { return Long.valueOf(updated); } else if (returnType == Integer.class) { return Integer.valueOf(updated); } } return null; // }
From source file:com.adaptris.core.marshaller.xstream.AliasedElementReflectionConverter.java
/** * Unmarshall the XML data from the reader and create the object instance. *///from w w w .j a v a 2 s.c o m @Override public Object doUnmarshal(final Object result, final HierarchicalStreamReader reader, final UnmarshallingContext context) { final Class resultType = result.getClass(); final Set<FastField> seenFields = new HashSet() { public boolean add(Object e) { if (!super.add(e)) { throw new DuplicateFieldException(((FastField) e).getName()); } return true; } }; // process attributes before recursing into child elements. Iterator<?> it = reader.getAttributeNames(); while (it.hasNext()) { String attrAlias = (String) it.next(); // TODO: realMember should return FastField String attrName = mapper.realMember(resultType, mapper.attributeForAlias(attrAlias)); Field field = reflectionProvider.getFieldOrNull(resultType, attrName); if (field != null && shouldUnmarshalField(field)) { Class classDefiningField = field.getDeclaringClass(); if (!mapper.shouldSerializeMember(classDefiningField, attrName)) { continue; } // we need a converter that produces a string representation only SingleValueConverter converter = mapper.getConverterFromAttribute(classDefiningField, attrName, field.getType()); Class type = field.getType(); if (converter != null) { Object value = converter.fromString(reader.getAttribute(attrAlias)); if (type.isPrimitive()) { type = Primitives.box(type); } if (value != null && !type.isAssignableFrom(value.getClass())) { throw new ConversionException( "Cannot convert type " + value.getClass().getName() + " to type " + type.getName()); } seenFields.add(new FastField(classDefiningField, attrName)); reflectionProvider.writeField(result, attrName, value, classDefiningField); } } } Map implicitCollectionsForCurrentObject = null; // Process the child nodes of the current node while (reader.hasMoreChildren()) { boolean validMatchFound = false; reader.moveDown(); String originalNodeName = reader.getNodeName(); log.trace("Reading processing node: {}", originalNodeName); Class explicitDeclaringClass = readDeclaringClass(reader); Class fieldDeclaringClass = explicitDeclaringClass == null ? resultType : explicitDeclaringClass; String fieldName = mapper.realMember(fieldDeclaringClass, originalNodeName); Mapper.ImplicitCollectionMapping implicitCollectionMapping = mapper .getImplicitCollectionDefForFieldName(fieldDeclaringClass, fieldName); final Object value; String implicitFieldName = null; Field field = null; Class type = null; // Handle standard field ie one not marked as implicit if (implicitCollectionMapping == null) { // no item of an implicit collection for this name ... do we have a // field? field = reflectionProvider.getFieldOrNull(fieldDeclaringClass, fieldName); if (field == null) { // it is not a field ... do we have a field alias? Class itemType = mapper.getItemTypeForItemFieldName(resultType, fieldName); if (itemType != null) { String classAttribute = HierarchicalStreams.readClassAttribute(reader, mapper); if (classAttribute != null) { type = mapper.realClass(classAttribute); } else { type = itemType; } } else { // it is not an alias ... do we have an element of an implicit // collection based on type only? try { type = mapper.realClass(originalNodeName); implicitFieldName = mapper.getFieldNameForItemTypeAndName(context.getRequiredType(), type, originalNodeName); // If there is an implicit collection with the // itemFieldName set, then we must bypass XStream's // implicit collection matching because it will not // match the subclass name to the itemFieldName! if (implicitFieldName == null) { implicitFieldName = XStreamUtils .getImplicitCollectionFieldNameForType(context.getRequiredType(), type); } } catch (CannotResolveClassException e) { // type stays null ... } if (type == null || implicitFieldName == null) { Field matchedField = XStreamUtils.getMatchedFieldFromClass(resultType, type, seenFields); if (matchedField != null) { field = matchedField; fieldName = matchedField.getName(); validMatchFound = true; } if (!validMatchFound) { // either not a type or element is a type alias, but does not // belong to an implicit field handleUnknownField(explicitDeclaringClass, fieldName, resultType, originalNodeName); // element is unknown in declaring class, ignore it now type = null; } } } if (validMatchFound) { value = unmarshallField(context, result, type, field); // value = reflectionProvider.newInstance(type); // TODO perhaps call // context.convertAnother(result, // type); } else if (type == null) { // no type, no value value = null; } else { if (Map.Entry.class.equals(type)) { // it is an element of an implicit map with two elements now forkey and value reader.moveDown(); final Object key = context.convertAnother(result, HierarchicalStreams.readClassType(reader, mapper)); reader.moveUp(); reader.moveDown(); final Object v = context.convertAnother(result, HierarchicalStreams.readClassType(reader, mapper)); reader.moveUp(); value = Collections.singletonMap(key, v).entrySet().iterator().next(); } else { // recurse info hierarchy value = context.convertAnother(result, type); } } } // Handle valid class field else { boolean fieldAlreadyChecked = false; // we have a field, but do we have to address a hidden one? if (explicitDeclaringClass == null) { while (field != null && !(fieldAlreadyChecked = shouldUnmarshalField(field) && mapper.shouldSerializeMember(field.getDeclaringClass(), fieldName))) { field = reflectionProvider.getFieldOrNull(field.getDeclaringClass().getSuperclass(), fieldName); } } if (field != null && (fieldAlreadyChecked || (shouldUnmarshalField(field) && mapper.shouldSerializeMember(field.getDeclaringClass(), fieldName)))) { String classAttribute = HierarchicalStreams.readClassAttribute(reader, mapper); if (classAttribute != null) log.trace("Reading class attribute: ", classAttribute); if (classAttribute != null) { type = mapper.realClass(classAttribute); } else { type = mapper.defaultImplementationOf(field.getType()); } // TODO the reflection provider should already return the proper field value = unmarshallField(context, result, type, field); Class definedType = field.getType(); if (!definedType.isPrimitive()) { type = definedType; } } else { value = null; } } } // Handle collection field with implicit mapping else { // we have an implicit collection with defined names implicitFieldName = implicitCollectionMapping.getFieldName(); type = implicitCollectionMapping.getItemType(); if (type == null) { String classAttribute = HierarchicalStreams.readClassAttribute(reader, mapper); type = mapper.realClass(classAttribute != null ? classAttribute : originalNodeName); } value = context.convertAnother(result, type); } if (value != null && !type.isAssignableFrom(value.getClass())) { throw new ConversionException( "Cannot convert type " + value.getClass().getName() + " to type " + type.getName()); } if (field != null) { log.trace("Updating field value for field: {}", fieldName); if (validMatchFound) { reflectionProvider.writeField(result, fieldName, value, field.getDeclaringClass()); } else { reflectionProvider.writeField(result, fieldName, value, field.getDeclaringClass()); } seenFields.add(new FastField(field.getDeclaringClass(), fieldName)); } else if (type != null) { if (implicitFieldName == null) { // look for implicit field implicitFieldName = mapper.getFieldNameForItemTypeAndName(context.getRequiredType(), value != null ? value.getClass() : Mapper.Null.class, originalNodeName); } if (implicitCollectionsForCurrentObject == null) { implicitCollectionsForCurrentObject = new HashMap(); } writeValueToImplicitCollection(value, implicitCollectionsForCurrentObject, result, implicitFieldName); } reader.moveUp(); } if (implicitCollectionsForCurrentObject != null) { for (Iterator iter = implicitCollectionsForCurrentObject.entrySet().iterator(); iter.hasNext();) { Map.Entry entry = (Map.Entry) iter.next(); Object value = entry.getValue(); if (value instanceof ArraysList) { Object array = ((ArraysList) value).toPhysicalArray(); reflectionProvider.writeField(result, (String) entry.getKey(), array, null); } } } return result; }
From source file:com.alibaba.citrus.service.requestcontext.support.ValueListSupport.java
/** ? */ public <T> T getValueOfType(Class<T> type, boolean isPrimitive, MethodParameter methodParameter, Object[] defaultValues) { // ??/*from www . j ava 2 s . co m*/ if (defaultValues == null || defaultValues.length == 1 && defaultValues[0] == null) { defaultValues = EMPTY_OBJECT_ARRAY; } // primitive? if (type.isPrimitive()) { isPrimitive = true; type = ClassUtil.getWrapperTypeIfPrimitive(type); } if (isPrimitive && isEmptyArray(defaultValues)) { Object defaultValue = getPrimitiveDefaultValue(type); if (defaultValue != null) { defaultValues = new Object[] { defaultValue }; } } // ????? Object[] values = getValues(defaultValues); // [""]? if (values.length == 1 && isEmptyObject(values[0])) { values = defaultValues; } try { return convert(type, methodParameter, values, defaultValues.length > 0 ? defaultValues[0] : null); } catch (TypeMismatchException e) { if (quiet) { return convert(type, methodParameter, defaultValues, null); } else { throw e; } } }
From source file:de.openknowledge.domain.SimpleValueObjectBuilder.java
private SimpleValueObjectBuilder(Class<V> valueObjectClass) { ParameterizedType valueObjectSuperclass = (ParameterizedType) valueObjectClass.getGenericSuperclass(); Class<?> simpleClass = (Class<?>) valueObjectSuperclass.getActualTypeArguments()[0]; try {/*from w w w. j av a 2 s. c om*/ valueObjectConstructor = valueObjectClass.getConstructor(simpleClass); } catch (NoSuchMethodException ex) { if (ClassUtils.isPrimitiveWrapper(simpleClass)) { try { valueObjectConstructor = valueObjectClass .getConstructor(ClassUtils.wrapperToPrimitive(simpleClass)); } catch (NoSuchMethodException e) { throw new IllegalStateException("Value Object " + valueObjectClass.getName() + " requires " + simpleClass.getSimpleName() + "-Constructor to be used with Converter/Adapter"); } } else { throw new IllegalStateException("Value Object " + valueObjectClass.getName() + " requires " + simpleClass.getSimpleName() + "-Constructor to be used with Converter/Adapter"); } } if (simpleClass.isPrimitive()) { simpleClass = ClassUtils.primitiveToWrapper(simpleClass); } try { simpleValueConstructor = simpleClass.getConstructor(String.class); } catch (NoSuchMethodException ex) { throw new IllegalStateException("Value Object simple type " + simpleClass.getName() + " requires String-Constructor to be used with JSF Converter"); } }
From source file:com.haulmont.cuba.core.sys.MetaModelLoader.java
protected Range.Cardinality getCardinality(Field field) { if (field.isAnnotationPresent(Column.class)) { return Range.Cardinality.NONE; } else if (field.isAnnotationPresent(OneToOne.class)) { return Range.Cardinality.ONE_TO_ONE; } else if (field.isAnnotationPresent(OneToMany.class)) { return Range.Cardinality.ONE_TO_MANY; } else if (field.isAnnotationPresent(ManyToOne.class)) { return Range.Cardinality.MANY_TO_ONE; } else if (field.isAnnotationPresent(ManyToMany.class)) { return Range.Cardinality.MANY_TO_MANY; } else if (field.isAnnotationPresent(Embedded.class)) { return Range.Cardinality.ONE_TO_ONE; } else {/*from w ww . j a v a 2s.c o m*/ Class<?> type = field.getType(); if (Collection.class.isAssignableFrom(type)) { return Range.Cardinality.ONE_TO_MANY; } else if (type.isPrimitive() || type.equals(String.class) || Number.class.isAssignableFrom(type) || Date.class.isAssignableFrom(type) || UUID.class.isAssignableFrom(type)) { return Range.Cardinality.NONE; } else return Range.Cardinality.MANY_TO_ONE; } }
From source file:com.silverwrist.dynamo.app.ApplicationContainer.java
private final RegisteredRenderer searchClassRenderers(Class klass) { if (klass.isPrimitive() || (klass == Object.class)) return null; // should have been picked up already // look at this level for the class member RegisteredRenderer rc = (RegisteredRenderer) (m_class_renderers.get(klass)); if (rc != null) return rc; if (klass.isArray()) { // for arrays, use the parallel function to search back over the component // class's hierarchy Class component = getUltimateComponent(klass); if (component.isPrimitive() || (component == Object.class)) return null; // no chance this should have been picked up String template = StringUtils.replace(klass.getName(), component.getName(), TEMPLATE_CLASSNAME); return searchArrayRenderers(component, template); } // end if/*from ww w . j av a2 s . c om*/ // Try all interfaces implemented by the object. Class[] ifaces = klass.getInterfaces(); for (int i = 0; i < ifaces.length; i++) { // look for interfaces implemented by the object rc = searchClassRenderers(ifaces[i]); if (rc != null) return rc; } // end for Class superclass = klass.getSuperclass(); if (superclass != null) { // try the superclass now rc = searchClassRenderers(superclass); if (rc != null) return rc; } // end if return null; // give up }
From source file:com.silverwrist.dynamo.app.ApplicationContainer.java
private final RegisteredRenderer searchArrayRenderers(Class klass, String template) { if (klass.isPrimitive() || (klass == Object.class)) return null; // should have been picked up already // look at this level for the class member RegisteredRenderer rc = null;//www . j a va2s . c o m try { // load the array class corresponding to the right depth, then check the renderer map Class tmp = Class.forName(StringUtils.replace(template, TEMPLATE_CLASSNAME, klass.getName())); rc = (RegisteredRenderer) (m_class_renderers.get(tmp)); if (rc != null) return rc; } // end try catch (ClassNotFoundException e) { // this class was not found, so it can't be present rc = null; } // end catch // Try all interfaces implemented by the object. Class[] ifaces = klass.getInterfaces(); for (int i = 0; i < ifaces.length; i++) { // look for interfaces implemented by the object rc = searchArrayRenderers(ifaces[i], template); if (rc != null) return rc; } // end for Class superclass = klass.getSuperclass(); if (superclass != null) { // try the superclass now rc = searchArrayRenderers(superclass, template); if (rc != null) return rc; } // end if return null; // give up }
From source file:mil.army.usace.data.nativequery.rdbms.NativeRdbmsQuery.java
protected Object convertType(Object obj, Field field) throws Exception { Class fieldParam = field.getType(); if (obj == null) return null; else {/* w ww. j a va 2 s. c o m*/ if (fieldParam.isPrimitive()) { return obj; } else { return convertType(obj, fieldParam); } } }