예제 #1
0
 public void testStructureOptionsInference() {
   Structure s = new OptionsBase.TypeMappedStructure();
   assertEquals(
       "Wrong structure alignment for base structure",
       Structure.ALIGN_NONE,
       Native.getStructureAlignment(s.getClass()));
   assertEquals(
       "Wrong type mapper for base structure", OptionsBase.TYPE_MAPPER, s.getTypeMapper());
 }
예제 #2
0
 /** Tests a structure (segment or group) against the corresponding part of a profile. */
 protected List<ValidationException> testStructure(Structure s, Object profile) {
   List<ValidationException> exList = new ArrayList<>();
   if (profile instanceof SegmentType) {
     if (Segment.class.isAssignableFrom(s.getClass())) {
       exList.addAll(testSegment((Segment) s, (SegmentType) profile));
     } else {
       profileNotHL7Compliant(
           exList, PROFILE_STRUCTURE_MISMATCH, "segment", s.getClass().getName());
     }
   } else if (profile instanceof HL7V2XStaticDef.SegGroup) {
     if (Group.class.isAssignableFrom(s.getClass())) {
       exList.addAll(
           testGroup((Group) s, ((HL7V2XStaticDef.SegGroup) profile).getSegGroupsAndSegments()));
     } else {
       profileNotHL7Compliant(exList, PROFILE_STRUCTURE_MISMATCH, "group", s.getClass().getName());
     }
   }
   return exList;
 }
예제 #3
0
파일: Function.java 프로젝트: suyimin/jna
 private Object convertArgument(
     Object[] args,
     int index,
     Method invokingMethod,
     TypeMapper mapper,
     boolean allowObjects,
     Class expectedType) {
   Object arg = args[index];
   if (arg != null) {
     Class type = arg.getClass();
     ToNativeConverter converter = null;
     if (NativeMapped.class.isAssignableFrom(type)) {
       converter = NativeMappedConverter.getInstance(type);
     } else if (mapper != null) {
       converter = mapper.getToNativeConverter(type);
     }
     if (converter != null) {
       ToNativeContext context;
       if (invokingMethod != null) {
         context = new MethodParameterContext(this, args, index, invokingMethod);
       } else {
         context = new FunctionParameterContext(this, args, index);
       }
       arg = converter.toNative(arg, context);
     }
   }
   if (arg == null || isPrimitiveArray(arg.getClass())) {
     return arg;
   }
   Class argClass = arg.getClass();
   // Convert Structures to native pointers
   if (arg instanceof Structure) {
     Structure struct = (Structure) arg;
     struct.autoWrite();
     if (struct instanceof Structure.ByValue) {
       // Double-check against the method signature, if available
       Class ptype = struct.getClass();
       if (invokingMethod != null) {
         Class[] ptypes = invokingMethod.getParameterTypes();
         if (isVarArgs(invokingMethod)) {
           if (index < ptypes.length - 1) {
             ptype = ptypes[index];
           } else {
             Class etype = ptypes[ptypes.length - 1].getComponentType();
             if (etype != Object.class) {
               ptype = etype;
             }
           }
         } else {
           ptype = ptypes[index];
         }
       }
       if (Structure.ByValue.class.isAssignableFrom(ptype)) {
         return struct;
       }
     }
     return struct.getPointer();
   }
   // Convert Callback to Pointer
   else if (arg instanceof Callback) {
     return CallbackReference.getFunctionPointer((Callback) arg);
   }
   // String arguments are converted to native pointers here rather
   // than in native code so that the values will be valid until
   // this method returns.
   // Convert String to native pointer (const)
   else if (arg instanceof String) {
     return new NativeString((String) arg, false).getPointer();
   }
   // Convert WString to native pointer (const)
   else if (arg instanceof WString) {
     return new NativeString(arg.toString(), true).getPointer();
   }
   // Default conversion of boolean to int; if you want something
   // different, use a ToNativeConverter
   else if (arg instanceof Boolean) {
     return Boolean.TRUE.equals(arg) ? INTEGER_TRUE : INTEGER_FALSE;
   } else if (String[].class == argClass) {
     return new StringArray((String[]) arg, encoding);
   } else if (WString[].class == argClass) {
     return new StringArray((WString[]) arg);
   } else if (Pointer[].class == argClass) {
     return new PointerArray((Pointer[]) arg);
   } else if (NativeMapped[].class.isAssignableFrom(argClass)) {
     return new NativeMappedArray((NativeMapped[]) arg);
   } else if (Structure[].class.isAssignableFrom(argClass)) {
     // If the signature is Structure[], disallow
     // Structure.ByReference[] and Structure.ByReference elements
     Structure[] ss = (Structure[]) arg;
     Class type = argClass.getComponentType();
     boolean byRef = Structure.ByReference.class.isAssignableFrom(type);
     if (expectedType != null) {
       if (!Structure.ByReference[].class.isAssignableFrom(expectedType)) {
         if (byRef) {
           throw new IllegalArgumentException(
               "Function "
                   + getName()
                   + " declared Structure[] at parameter "
                   + index
                   + " but array of "
                   + type
                   + " was passed");
         }
         for (int i = 0; i < ss.length; i++) {
           if (ss[i] instanceof Structure.ByReference) {
             throw new IllegalArgumentException(
                 "Function "
                     + getName()
                     + " declared Structure[] at parameter "
                     + index
                     + " but element "
                     + i
                     + " is of Structure.ByReference type");
           }
         }
       }
     }
     if (byRef) {
       Structure.autoWrite(ss);
       Pointer[] pointers = new Pointer[ss.length + 1];
       for (int i = 0; i < ss.length; i++) {
         pointers[i] = ss[i] != null ? ss[i].getPointer() : null;
       }
       return new PointerArray(pointers);
     } else if (ss.length == 0) {
       throw new IllegalArgumentException("Structure array must have non-zero length");
     } else if (ss[0] == null) {
       Structure.newInstance(type).toArray(ss);
       return ss[0].getPointer();
     } else {
       Structure.autoWrite(ss);
       return ss[0].getPointer();
     }
   } else if (argClass.isArray()) {
     throw new IllegalArgumentException(
         "Unsupported array argument type: " + argClass.getComponentType());
   } else if (allowObjects) {
     return arg;
   } else if (!Native.isSupportedNativeType(arg.getClass())) {
     throw new IllegalArgumentException(
         "Unsupported argument type "
             + arg.getClass().getName()
             + " at parameter "
             + index
             + " of function "
             + getName());
   }
   return arg;
 }