@SuppressWarnings("unchecked") @Test public void shouldFindEnumValues() throws IllegalAccessException { klass = modelPackage._getClass("Colour"); assertThat(klass.getClassType()).isEqualTo(ClassType.ENUM); Map<String,JEnumConstant> enums = (Map<String, JEnumConstant>) FieldUtils.readField(klass, "enumConstantsByName", true); assertThat(enums.keySet()).contains("LIGHT_BLUE", "RED"); JEnumConstant lightBlue = enums.get("LIGHT_BLUE"); assertThat(lightBlue.javadoc().get(0)).isEqualTo("Colour of the sky"); List<JExpression> args = (List<JExpression>) FieldUtils.readField(lightBlue, "args", true); assertThat(args).hasSize(1); assertThat(args.get(0)).isInstanceOf(JStringLiteral.class); String literal = (String) FieldUtils.readField(args.get(0), "str", true); assertThat(literal).isEqualTo("lightBlue"); }
private void assertPatternValue(JAnnotationUse jAnnotationUse, String expectedPattern) throws Exception { JAnnotationValue jAnnotationValue = jAnnotationUse.getAnnotationMembers().get("pattern"); Field value = jAnnotationValue.getClass().getDeclaredField("value"); value.setAccessible(true); JStringLiteral object = (JStringLiteral) value.get(jAnnotationValue); assertThat(object.str, is(expectedPattern)); }
public JExpression createConstant(Outline outline, XmlString lexical) { if (isCollection()) return null; if (adapter == null) return coreType.createConstant(outline, lexical); // [RESULT] new Adapter().unmarshal(CONSTANT); JExpression cons = coreType.createConstant(outline, lexical); @SuppressWarnings("unchecked") Class<? extends XmlAdapter<?, ?>> atype = (Class<? extends XmlAdapter<?, ?>>) adapter .getAdapterIfKnown(); // try to run the adapter now rather than later. if (cons instanceof JStringLiteral && atype != null) { JStringLiteral scons = (JStringLiteral) cons; @SuppressWarnings("unchecked") XmlAdapter<Object, String> a = (XmlAdapter<Object, String>) ClassFactory .create(atype); try { Object value = a.unmarshal(scons.str); if (value instanceof String) { return JExpr.lit((String) value); } } catch (Exception e) { // assume that we can't eagerly bind this } } return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal") .arg(cons); }
@Override public JExpression compute(Outline outline){ JExpression expression = computeInit(outline); if((expression instanceof JFieldRef) || (expression instanceof JStringLiteral)){ setField(null); return expression; } return JExpr.ref(getField()); }