@NotNull public Instantiator<?> findInstantiator(@NotNull Type type, @NotNull NamedTypeList types) { // First check if we have an immediate conversion registered. If so, we'll just use that. if (types.size() == 1) { TypeConversion conversion = findConversionFromDbValue(types.getType(0), type).orElse(null); if (conversion != null) return args -> conversion.convert(args.getSingleValue()); } Class<?> cl = rawType(type); Instantiator<?> instantiator = findExplicitInstantiatorFor(cl, types).orElse(null); if (instantiator != null) return instantiator; if (!isPublic(cl.getModifiers())) throw new InstantiationFailureException( type + " can't be instantiated reflectively because it is not public or missing a @DalesbredConstructor-annotation"); return candidateConstructorsSortedByDescendingParameterCount(cl) .map(ctor -> implicitInstantiatorFrom(ctor, types).orElse(null)) .filter(Objects::nonNull) .findFirst() .orElseThrow( () -> new InstantiationFailureException( "could not find a way to instantiate " + type + " with parameters " + types)); }
@Nullable public Object valueToDatabase(@Nullable Object value) { if (value == null) return null; TypeConversion conversion = typeConversionRegistry.findConversionToDb(value.getClass()).orElse(null); if (conversion != null) return conversion.convert(value); else if (value instanceof Enum<?>) return dialect.valueToDatabase(((Enum<?>) value).name()); else return dialect.valueToDatabase(value); }
@Test public void testBasic() throws Exception { Assert.assertEquals(true, TypeConversion.convert("true", Boolean.class).booleanValue()); Assert.assertEquals(true, TypeConversion.convert("1", Boolean.class).booleanValue()); Assert.assertEquals(1234567890, TypeConversion.convert("1234567890", Integer.class).intValue()); Assert.assertEquals( 12345678901234L, TypeConversion.convert("12345678901234", Long.class).longValue()); Assert.assertEquals( TypeConversionTest.class, TypeConversion.convert( "com.avast.syringe.config.internal.TypeConversionTest", Class.class)); }
@Override public void visitFunctionSignature(FunctionSignature functionSignature) { super.visitFunctionSignature(functionSignature); Function function = functionSignature.getFunction(); if (function != null) { getList(callbacksByLibrary, getLibrary(functionSignature)).add(functionSignature); Identifier name = typeConverter.inferCallBackName(functionSignature, false, false, null); if (name != null) { callbacksByName.put(name, functionSignature); Identifier identifier = typeConverter.inferCallBackName(functionSignature, true, true, null); if (identifier != null) callbacksFullNames.add(identifier); } } }
public Identifier getTaggedTypeIdentifierInJava(TaggedTypeRef s) { Identifier tag = s.getTag(); if (tag != null) { TaggedTypeRef rep = null; if (s instanceof Struct) { rep = structsByName.get(tag); } else if (s instanceof Enum) { rep = enumsByName.get(tag); } if (rep != null) s = rep; } Identifier name = declarationsConverter.getActualTaggedTypeName(s); if (name == null) return null; String library = getLibrary(s); if (library == null) return null; name = name.clone(); Struct parentStruct = s.findParentOfType(Struct.class); if (parentStruct != null && parentStruct != s) return ident(getTaggedTypeIdentifierInJava(parentStruct), name); else if ((s instanceof Struct) && (config.putTopStructsInSeparateFiles || config.runtime == JNAeratorConfig.Runtime.BridJ)) return ident(getLibraryPackage(library), name); else return typeConverter.libMember(getLibraryClassFullName(library), null, name); }
@NotNull private static Optional<TypeConversion> findEnumConversion(@NotNull Type target) { if (isEnum(target)) { @SuppressWarnings("rawtypes") Class<? extends Enum> cl = rawType(target).asSubclass(Enum.class); return Optional.ofNullable( TypeConversion.fromNonNullFunction(value -> Enum.valueOf(cl, value.toString()))); } return Optional.empty(); }
/** * Returns conversion for converting value of source to target, or returns null if there's no such * conversion. */ @NotNull private Optional<TypeConversion> findConversionFromDbValue( @NotNull Type source, @NotNull Type target) { if (isAssignable(target, source)) return Optional.of(TypeConversion.identity()); Optional<TypeConversion> directConversion = typeConversionRegistry.findConversionFromDbValue(source, target); if (directConversion.isPresent()) return directConversion; Optional<TypeConversion> arrayConversion = findArrayConversion(source, target); if (arrayConversion.isPresent()) return arrayConversion; Optional<TypeConversion> optionalConversion = findOptionalConversion(source, target); if (optionalConversion.isPresent()) return optionalConversion; Optional<TypeConversion> enumConversion = findEnumConversion(target); if (enumConversion.isPresent()) return enumConversion; return Optional.empty(); }
@Override public String toString() { if (value instanceof CharSequence) return "'" + value + "'"; else return TypeConversion.toString(value); }
int readLH(int ireg) { byte[] bytes = this.i2cDeviceClient.read(ireg, 2); return TypeConversion.byteArrayToInt(bytes, ByteOrder.LITTLE_ENDIAN); }
int read8(int ireg) { byte b = this.i2cDeviceClient.read8(ireg); return TypeConversion.unsignedByteToInt(b); }