private static Set<String> getPackageNamespace(PackageOutline packageOutline) { final Map<String, Integer> uriCountMap = getUriCountMap(packageOutline); final Set<String> result = new HashSet<String>(); if (uriCountMap != null) { result.addAll(uriCountMap.keySet()); } // // Find annotated methods in the ObjectFactory that create elements, and extract their namespace URIs. // final Collection<JMethod> methods = packageOutline.objectFactory().methods(); if (methods != null) { for (JMethod method : methods) { final List<JAnnotationUse> annotations = getAnnotations(method); if (annotations != null) { for (JAnnotationUse annotation : annotations) { final Map<String, JAnnotationValue> map = getAnnotationMemberValues(annotation); if (map != null) { for (Map.Entry<String, JAnnotationValue> entry : map.entrySet()) { if (entry.getKey().equals("namespace")) { final String ns = getStringAnnotationValue(entry.getValue()); result.add(ns); } } } } } } } return result; }
protected void assertSimpleAnnotation(JMethod method, String annotation, String argument) { JAnnotationUse annotationUse = method.annotations().stream() .filter(a -> a.getAnnotationClass().name().equals(annotation)).findFirst().get(); JAnnotationValue value = annotationUse.getAnnotationMembers().get("value"); StringWriter writer = new StringWriter(); JFormatter formatter = new JFormatter(writer); value.generate(formatter); assertThat(writer.toString()).isEqualTo(argument); }
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)); }
/** * Handles the extraction of the schema type from the XmlElement * annotation. This was surprisingly difficult. Apparently the * model doesn't provide access to the annotation we're referring to * so I need to print it and read the string back. Even the formatter * itself is final! * @param outline root of the generated code * @param directClasses set of classes to append to * @param type annotation we're analysing */ private static void handleXmlElement(Outline outline, Set<String> directClasses, JAnnotationValue type) { StringWriter sw = new StringWriter(); JFormatter jf = new JFormatter(new PrintWriter(sw)); type.generate(jf); String s = sw.toString(); s = s.substring(0, s.length()-".class".length()); if (!s.startsWith("java") && outline.getCodeModel()._getClass(s) == null && !foundWithinOutline(s, outline)) { directClasses.add(s); } }
private String annotationValueToString(JAnnotationValue ns) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); JFormatter jf = new JFormatter(pw, ""); ns.generate(jf); pw.flush(); String s = sw.toString(); return s.substring(1, s.length()-1); }
private void checkIfAnnotationHasParameter(JDefinedClass classToCheck, Class<?> annotationClass, String field, String param) { JAnnotationUse annotation = getAnnotationForField(classToCheck, annotationClass, field); assertThat(annotation, is(notNullValue())); JAnnotationValue annotationParam = annotation.getAnnotationMembers().get(param); assertThat(annotationParam, is(notNullValue())); }