use of java.lang.reflect.Type in project sonarqube by SonarSource.
the class ItUtils method jsonToMap.
public static Map<String, Object> jsonToMap(String json) {
Gson gson = new Gson();
Type type = new TypeToken<Map<String, Object>>() {
}.getType();
return gson.fromJson(json, type);
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method coercingStringMapOfIntListsShouldBeIdentity.
@Test
public void coercingStringMapOfIntListsShouldBeIdentity() throws CoerceFailedException, NoSuchFieldException {
Type type = TestFields.class.getField("stringMapOfLists").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
ImmutableMap<String, ImmutableList<Integer>> input = ImmutableMap.of("foo", ImmutableList.of(4, 5), "bar", ImmutableList.of(6, 7));
Object result = coercer.coerce(cellRoots, filesystem, Paths.get(""), input);
assertEquals(input, result);
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method hasElementTypesForContainers.
@Test
public void hasElementTypesForContainers() throws NoSuchFieldException {
Type type = TestFields.class.getField("stringMapOfLists").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
assertTrue(coercer.hasElementClass(String.class));
assertTrue(coercer.hasElementClass(Integer.class));
assertTrue(coercer.hasElementClass(Integer.class, String.class));
assertTrue(coercer.hasElementClass(Integer.class, SourcePath.class));
assertFalse(coercer.hasElementClass(SourcePath.class));
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method coerceFromTurkishIsShouldWork.
@Test
public void coerceFromTurkishIsShouldWork() throws NoSuchFieldException, CoerceFailedException {
Type type = TestFields.class.getField("listOfTestEnums").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
String pinkWithLowercaseTurkishI = "pınk";
String pinkWithUppercaseTurkishI = "PİNK";
String whiteWithLowercaseTurkishI = "whıte";
String whiteWithUppercaseTurkishI = "WHİTE";
ImmutableList<String> input = ImmutableList.of(pinkWithLowercaseTurkishI, pinkWithUppercaseTurkishI, whiteWithLowercaseTurkishI, whiteWithUppercaseTurkishI);
ImmutableList<TestEnum> expected = ImmutableList.of(TestEnum.PINK, TestEnum.PINK, TestEnum.white, TestEnum.white);
Object result = coercer.coerce(cellRoots, filesystem, Paths.get(""), input);
assertEquals(expected, result);
}
use of java.lang.reflect.Type in project buck by facebook.
the class TypeCoercerTest method invalidSourcePathShouldGiveSpecificErrorMsg.
@Test
public void invalidSourcePathShouldGiveSpecificErrorMsg() throws NoSuchFieldException, IOException {
Type type = TestFields.class.getField("setOfSourcePaths").getGenericType();
TypeCoercer<?> coercer = typeCoercerFactory.typeCoercerForType(type);
Path baratheon = Paths.get("Baratheon.java");
Path lannister = Paths.get("Lannister.java");
Path stark = Paths.get("Stark.java");
Path targaryen = Paths.get("Targaryen.java");
ImmutableList<Path> input = ImmutableList.of(baratheon, lannister, stark, targaryen);
for (Path p : input) {
if (!p.equals(baratheon)) {
filesystem.touch(p);
}
}
try {
coercer.coerce(cellRoots, filesystem, Paths.get(""), input);
} catch (CoerceFailedException e) {
String result = e.getMessage();
String expected = "cannot coerce 'Baratheon.java'";
for (Path p : input) {
if (!p.equals(baratheon)) {
assertFalse(result.contains(p.toString()));
}
}
assertTrue(result.contains(expected));
}
}
Aggregations