com.sun.codemodel.JAnnotationUse Java Examples
The following examples show how to use
com.sun.codemodel.JAnnotationUse.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ImplementationBuilder.java From aem-component-generator with Apache License 2.0 | 6 votes |
private void addSlingAnnotations(JDefinedClass jDefinedClass, JClass adapterClass, String resourceType) { JAnnotationUse jAUse = jDefinedClass.annotate(codeModel.ref(Model.class)); JAnnotationArrayMember adaptablesArray = jAUse.paramArray("adaptables"); for (String adaptable : adaptables) { if ("resource".equalsIgnoreCase(adaptable)) { adaptablesArray.param(codeModel.ref(Resource.class)); } if ("request".equalsIgnoreCase(adaptable)) { adaptablesArray.param(codeModel.ref(SlingHttpServletRequest.class)); } } if (this.isAllowExporting) { jAUse.paramArray("adapters").param(adapterClass).param(codeModel.ref(ComponentExporter.class)); } else { jAUse.param("adapters", adapterClass); } if (StringUtils.isNotBlank(resourceType)) { jAUse.param("resourceType", resourceType); } if (this.isAllowExporting) { jAUse = jDefinedClass.annotate(codeModel.ref(Exporter.class)); jAUse.param("name", codeModel.ref(ExporterConstants.class).staticRef(SLING_MODEL_EXPORTER_NAME)); jAUse.param("extensions", codeModel.ref(ExporterConstants.class).staticRef(SLING_MODEL_EXTENSION)); } }
Example #2
Source File: AnnotatePropertyVisitor.java From jaxb2-basics with BSD 2-Clause "Simplified" License | 6 votes |
public Void visitAttributePropertyInfo( MAttributePropertyInfo<NType, NClass> info) { JAnnotationUse annotation = this.annotatable .annotate(XmlAttribute.class); final String name = info.getAttributeName().getLocalPart(); final String namespace = info.getAttributeName().getNamespaceURI(); annotation.param("name", name); // generate namespace property? if (!namespace.equals("")) { // assume attributeFormDefault == // unqualified annotation.param("namespace", namespace); } // TODO // if(info.isRequired()) { // xaw.required(true); // } return null; }
Example #3
Source File: AnnotationInspectorTest.java From jpa-unit with Apache License 2.0 | 6 votes |
@BeforeClass public static void generateModel() throws Exception { final JCodeModel jCodeModel = new JCodeModel(); final JPackage jp = jCodeModel.rootPackage(); final JDefinedClass jClass = jp._class(JMod.PUBLIC, "ClassUnderTest"); JAnnotationUse jAnnotationUse = jClass.annotate(InitialDataSets.class); jAnnotationUse.param("value", "Script.file"); jClass.annotate(Cleanup.class); final JFieldVar jField = jClass.field(JMod.PRIVATE, String.class, "testField"); jField.annotate(PersistenceContext.class); final JMethod jMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "testMethod"); jAnnotationUse = jMethod.annotate(InitialDataSets.class); jAnnotationUse.param("value", "InitialDataSets.file"); jAnnotationUse = jMethod.annotate(ApplyScriptsAfter.class); jAnnotationUse.param("value", "ApplyScriptsAfter.file"); buildModel(testFolder.getRoot(), jCodeModel); compileModel(testFolder.getRoot()); cut = loadClass(testFolder.getRoot(), jClass.name()); }
Example #4
Source File: AbstractBuilder.java From springmvc-raml-plugin with Apache License 2.0 | 6 votes |
protected void implementsGeneratedAnnotation() { if (!Config.isGeneratedAnnotation()) { return; } // check if POJO already has the annotation Iterator<JAnnotationUse> iterator = this.pojo.annotations().iterator(); while (iterator.hasNext()) { String fullName = iterator.next().getAnnotationClass().fullName(); if (Generated.class.getName().equals(fullName)) { return; } } this.pojo.annotate(Generated.class).param("value", Config.DEFAULT_GENERATED_ANNOTATION_VALUE); }
Example #5
Source File: RamlInterpreterTest.java From springmvc-raml-plugin with Apache License 2.0 | 6 votes |
private void checkIfGetterContainsAnnotation(boolean expected, JDefinedClass classToCheck, Class<?> annotationClass, String... fields) { List<String> expectedMethodNames = Arrays.asList(fields).stream().map(field -> "get" + NamingHelper.convertToClassName(field)) .collect(Collectors.toList()); Map<String, JMethod> actualMethods = classToCheck.methods().stream() .collect(Collectors.toMap(method -> method.name(), method -> method)); for (String expectedMethodName : expectedMethodNames) { if (actualMethods.keySet().contains(expectedMethodName)) { boolean found = false; for (JAnnotationUse annotation : actualMethods.get(expectedMethodName).annotations()) { if (annotation.getAnnotationClass().name().equals(annotationClass.getSimpleName())) { found = true; } } assertThat(found, is(expected)); } else { fail(); } } }
Example #6
Source File: PluginImpl.java From immutable-xjc with MIT License | 6 votes |
private JExpression defaultValue(JFieldVar field) { JType javaType = field.type(); if (setDefaultValuesInConstructor) { Optional<JAnnotationUse> xmlElementAnnotation = getAnnotation(field.annotations(), javax.xml.bind.annotation.XmlElement.class.getCanonicalName()); if (xmlElementAnnotation.isPresent()) { JAnnotationValue annotationValue = xmlElementAnnotation.get().getAnnotationMembers().get("defaultValue"); if (annotationValue != null) { StringWriter sw = new StringWriter(); JFormatter f = new JFormatter(sw); annotationValue.generate(f); return JExpr.lit(sw.toString().replaceAll("\"", "")); } } } if (javaType.isPrimitive()) { if (field.type().owner().BOOLEAN.equals(javaType)) { return JExpr.lit(false); } else if (javaType.owner().SHORT.equals(javaType)) { return JExpr.cast(javaType.owner().SHORT, JExpr.lit(0)); } else { return JExpr.lit(0); } } return JExpr._null(); }
Example #7
Source File: JaxbValidationsPlugins.java From krasa-jaxb-tools with Apache License 2.0 | 5 votes |
private void annotateMultiplePattern(final List<XSFacet> patternList, final JAnnotationUse patternAnnotation) { StringBuilder sb = new StringBuilder(); for (XSFacet xsFacet : patternList) { final String value = xsFacet.getValue().value; // cxf-codegen fix if (!"\\c+".equals(value)) { sb.append("(").append(replaceXmlProprietals(value)).append(")|"); } } patternAnnotation.param("regexp", sb.substring(0, sb.length() - 1)); }
Example #8
Source File: ImmutableJaxbGenerator.java From rice with Educational Community License v2.0 | 5 votes |
private void renderClassLevelAnnotations(JDefinedClass classModel, List<FieldModel> fields) throws Exception { JFieldRef constantsClass = classModel.staticRef(Util.CONSTANTS_CLASS_NAME); JFieldRef elementsClass = classModel.staticRef(Util.ELEMENTS_CLASS_NAME); JClass coreConstants = codeModel.ref(CoreConstants.class); JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements"); // XmlRootElement JAnnotationUse rootElementAnnotation = classModel.annotate(XmlRootElement.class); rootElementAnnotation.param("name", constantsClass.ref(Util.ROOT_ELEMENT_NAME_FIELD)); // XmlAccessorType JAnnotationUse xmlAccessorTypeAnnotation = classModel.annotate(XmlAccessorType.class); xmlAccessorTypeAnnotation.param("value", XmlAccessType.NONE); // XmlType JAnnotationUse xmlTypeAnnotation = classModel.annotate(XmlType.class); xmlTypeAnnotation.param("name", constantsClass.ref(Util.TYPE_NAME_FIELD)); JAnnotationArrayMember propOrderMember = xmlTypeAnnotation.paramArray("propOrder"); for (FieldModel field : fields) { if (Util.isCommonElement(field.fieldName)) { propOrderMember.param(commonElementsRef.ref(Util.toConstantsVariable(field.fieldName))); } else { propOrderMember.param(elementsClass.ref(Util.toConstantsVariable(field.fieldName))); } } propOrderMember.param(commonElementsRef.ref("FUTURE_ELEMENTS")); }
Example #9
Source File: PMMLPlugin.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 5 votes |
static private List<JAnnotationUse> getAnnotations(JAnnotatable annotatable){ try { Class<?> clazz = annotatable.getClass(); Field field; while(true){ try { field = clazz.getDeclaredField("annotations"); break; } catch(NoSuchFieldException nsfe){ clazz = clazz.getSuperclass(); if(clazz == null){ throw nsfe; } } } if(!field.isAccessible()){ field.setAccessible(true); } return (List)field.get(annotatable); } catch(ReflectiveOperationException roe){ throw new RuntimeException(roe); } }
Example #10
Source File: AnnotatingVisitor.java From hyperjaxb3 with BSD 2-Clause "Simplified" License | 5 votes |
public JAnnotationUse visitSingleAnnotationField( XSingleAnnotationField<?> field) { final XAnnotationValue<?> annotationValue = field.getAnnotationValue(); annotationValue.accept(new AnnotatingSingleValueVisitor(this.codeModel, field.getName(), this.annotationUse)); return this.annotationUse; }
Example #11
Source File: CreateDepthFirstTraverserClass.java From jaxb-visitor with Apache License 2.0 | 5 votes |
private boolean isIdrefField(FieldOutline fieldOutline) { JFieldVar field = ClassDiscoverer.field(fieldOutline); if(field == null) { return false; } for(JAnnotationUse use : field.annotations()) { if(use.getAnnotationClass().fullName().equals("javax.xml.bind.annotation.XmlIDREF")) { return true; } } return false; }
Example #12
Source File: ClassDiscoverer.java From jaxb-visitor with Apache License 2.0 | 5 votes |
/** * Parse the annotations on the field to see if there is an XmlElements * annotation on it. If so, we'll check this annotation to see if it * refers to any classes that are external from our code schema compile. * If we find any, then we'll add them to our visitor. * @param outline root of the generated code * @param field parses the xml annotations looking for an external class * @param directClasses set of direct classes to append to * @throws IllegalAccessException throw if there's an error introspecting the annotations */ private static void parseXmlAnnotations(Outline outline, FieldOutline field, Set<String> directClasses) throws IllegalAccessException { if (field instanceof UntypedListField) { JFieldVar jfv = (JFieldVar) FieldHack.listField.get(field); for(JAnnotationUse jau : jfv.annotations()) { JClass jc = jau.getAnnotationClass(); if (jc.fullName().equals(XmlElements.class.getName())) { JAnnotationArrayMember value = (JAnnotationArrayMember) jau.getAnnotationMembers().get("value"); for(JAnnotationUse anno : value.annotations()) { handleXmlElement(outline, directClasses, anno.getAnnotationMembers().get("type")); } } } } }
Example #13
Source File: AnnotatingVisitor.java From hyperjaxb3 with BSD 2-Clause "Simplified" License | 5 votes |
public JAnnotationUse visitArrayAnnotationField( XArrayAnnotationField<?> field) { String fieldName = field.getName(); final JAnnotationArrayMember annotationArrayMember = this.annotationUse .paramArray(fieldName); for (final XAnnotationValue<?> annotationValue : field .getAnnotationValues()) { annotationValue.accept(new AnnotatingArrayValueVisitor( this.codeModel, annotationArrayMember)); } return this.annotationUse; }
Example #14
Source File: PMMLPlugin.java From jpmml-model with BSD 3-Clause "New" or "Revised" License | 5 votes |
static private JAnnotationUse findAnnotation(Collection<JAnnotationUse> annotations, Class<?> clazz){ String fullName = clazz.getName(); for(JAnnotationUse annotation : annotations){ JClass type = annotation.getAnnotationClass(); if(checkType(type, fullName)){ return annotation; } } return null; }
Example #15
Source File: ImmutableJaxbGenerator.java From rice with Educational Community License v2.0 | 5 votes |
private void renderFutureElementsField(JDefinedClass classModel) throws Exception { JType collectionType = codeModel.parseType("java.util.Collection<org.w3c.dom.Element>"); JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, collectionType, "_futureElements"); field.init(JExpr._null()); JAnnotationUse annotation = field.annotate(SuppressWarnings.class); annotation.param("value", "unused"); field.annotate(XmlAnyElement.class); }
Example #16
Source File: PluginImpl.java From immutable-xjc with MIT License | 5 votes |
private JMethod getGetterProperty(final JFieldVar field, final JDefinedClass clazz) { JMethod getter = clazz.getMethod("get" + StringUtils.capitalize(field.name()), NO_ARGS); if (getter == null) { getter = clazz.getMethod("is" + StringUtils.capitalize(field.name()), NO_ARGS); } if (getter == null) { List<JDefinedClass> superClasses = getSuperClasses(clazz); for (JDefinedClass definedClass : superClasses) { getter = getGetterProperty(field, definedClass); if (getter != null) { break; } } } if (getter == null) { //XJC does not work conform Introspector.decapitalize when multiple upper-case letter are in field name Optional<JAnnotationUse> xmlElementAnnotation = getAnnotation(field.annotations(), javax.xml.bind.annotation.XmlElement.class.getCanonicalName()); if (xmlElementAnnotation.isPresent()) { JAnnotationValue annotationValue = xmlElementAnnotation.get().getAnnotationMembers().get("name"); if (annotationValue != null) { StringWriter sw = new StringWriter(); JFormatter f = new JFormatter(sw); annotationValue.generate(f); getter = clazz.getMethod("get" + sw.toString().replaceAll("\"", ""), NO_ARGS); } } } return getter; }
Example #17
Source File: PluginImpl.java From immutable-xjc with MIT License | 5 votes |
private boolean isRequired(JFieldVar field) { return Stream.of(XmlElement.class, XmlAttribute.class) .map(annotationType -> getAnnotation(field.annotations(), annotationType.getCanonicalName()) .map(JAnnotationUse::getAnnotationMembers) .map(annotationValues -> annotationValues.get("required")) .filter(annotationValue -> { StringWriter sw = new StringWriter(); JFormatter f = new JFormatter(sw); annotationValue.generate(f); return sw.toString().equals("true"); }) ).anyMatch(Optional::isPresent); }
Example #18
Source File: JaxbValidationsPlugins.java From krasa-jaxb-tools with Apache License 2.0 | 5 votes |
private void processNotNull(ClassOutline co, JFieldVar field) { if (notNullAnnotations) { log("@NotNull: " + field.name() + " added to class " + co.implClass.name()); JAnnotationUse annotation = field.annotate(NotNull.class); if (notNullPrefixClassName) { annotation.param("message", String.format("%s.%s {%s.message}", co.implClass.name(), field.name(), NotNull.class.getName())); } else if (notNullPrefixFieldName) { annotation.param("message", String.format("%s {%s.message}", field.name(), NotNull.class.getName())); } else if (notNullCustomMessages) { annotation.param("message", String.format("{%s.message}", NotNull.class.getName())); } else if (notNullCustomMessage != null) { annotation.param("message", notNullCustomMessage.replace("{ClassName}", co.implClass.name()).replace("{FieldName}", field.name())); } } }
Example #19
Source File: RamlInterpreterTest.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
private void checkIfFieldContainsAnnotation(boolean expected, JDefinedClass classToCheck, Class<?> annotationClass, String... fields) { for (JFieldVar field : classToCheck.fields().values()) { if ((fields == null || fields.length == 0 || ArrayUtils.contains(fields, field.name())) && !field.name().equals("serialVersionUID")) { boolean found = false; for (JAnnotationUse annotation : field.annotations()) { if (annotation.getAnnotationClass().name().equals(annotationClass.getSimpleName())) { found = true; } } assertThat(found, is(expected)); } } }
Example #20
Source File: JaxbValidationsPlugins.java From krasa-jaxb-tools with Apache License 2.0 | 5 votes |
@SuppressWarnings({"unchecked", "rawtypes"}) public boolean hasAnnotation(JFieldVar var, Class annotationClass) { List<JAnnotationUse> list = (List<JAnnotationUse>) Utils.getField("annotations", var); if (list != null) { for (JAnnotationUse annotationUse : list) { if (((Class) Utils.getField("clazz._class", annotationUse)).getCanonicalName().equals( annotationClass.getCanonicalName())) { return true; } } } return false; }
Example #21
Source File: ImmutableJaxbGenerator.java From rice with Educational Community License v2.0 | 5 votes |
private void renderField(JDefinedClass classModel, FieldModel fieldModel) { JFieldVar field = classModel.field(JMod.PRIVATE | JMod.FINAL, fieldModel.fieldType, fieldModel.fieldName); JAnnotationUse annotation = field.annotate(XmlElement.class); if (Util.isCommonElement(fieldModel.fieldName)) { JClass coreConstants = codeModel.ref(CoreConstants.class); JFieldRef commonElementsRef = coreConstants.staticRef("CommonElements"); annotation.param("name", commonElementsRef.ref(Util.toConstantsVariable(fieldModel.fieldName))); } else { JClass elementsClass = codeModel.ref(Util.ELEMENTS_CLASS_NAME); JFieldRef fieldXmlNameRef = elementsClass.staticRef(Util.toConstantsVariable(fieldModel.fieldName)); annotation.param("name", fieldXmlNameRef); } annotation.param("required", false); }
Example #22
Source File: SpringFeignClientClassAnnotationRule.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
@Override public JAnnotationUse apply(ApiResourceMetadata controllerMetadata, JDefinedClass generatableType) { JAnnotationUse feignClient = generatableType.annotate(FeignClient.class); feignClient.param("path", controllerMetadata.getControllerUrl()); feignClient.param("name", getClientName(controllerMetadata)); return feignClient; }
Example #23
Source File: RamlInterpreterTest.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
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)); }
Example #24
Source File: ConnectPlugin.java From kafka-connect-transform-xml with Apache License 2.0 | 5 votes |
Map<String, Object> attributes(JFieldVar field, Class<?> annotationClass) { for (JAnnotationUse annotationUse : field.annotations()) { log.trace("isRequired() - name = '{}' getAnnotationClass = '{}'", field.name(), annotationUse.getAnnotationClass().fullName()); if (annotationUse.getAnnotationClass().fullName().equals(annotationClass.getName())) { Map<String, Object> result = new LinkedHashMap<>(); return result; } } return null; }
Example #25
Source File: RamlInterpreterTest.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
private void assertDateFormatAnnotation(JFieldVar field, String expectedPattern) throws Exception { assertEquals(1, field.annotations().size()); Optional<JAnnotationUse> optionalAnnotation = field.annotations().stream().findFirst(); if (optionalAnnotation.isPresent() && JsonFormat.class.getName().equals(optionalAnnotation.get().getAnnotationClass().fullName())) { assertPatternValue(optionalAnnotation.get(), expectedPattern); } else { fail(); } }
Example #26
Source File: RamlInterpreterTest.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
private void assertAnnotations(JFieldVar field, String expectedPattern) throws Exception { Iterator<JAnnotationUse> iterator = field.annotations().iterator(); assertEquals(2, field.annotations().size()); while (iterator.hasNext()) { JAnnotationUse jAnnotationUse = iterator.next(); if (NotNull.class.getName().equals(jAnnotationUse.getAnnotationClass().fullName())) { // do nothing } else if (JsonFormat.class.getName().equals(jAnnotationUse.getAnnotationClass().fullName())) { assertPatternValue(jAnnotationUse, expectedPattern); } else { fail(); } } }
Example #27
Source File: SpringValidatedClassAnnotationRule.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
@Override public JAnnotationUse apply(ApiResourceMetadata controllerMetadata, JDefinedClass generatableType) { if (Config.getPojoConfig().isIncludeJsr303Annotations()) { return generatableType.annotate(Validated.class); } return null; }
Example #28
Source File: RamlInterpreterTest.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
private JAnnotationUse getAnnotationForField(JDefinedClass classToCheck, Class<?> annotationClass, String field) { for (JFieldVar fieldVar : classToCheck.fields().values()) { if (fieldVar.name().equals(field)) { for (JAnnotationUse annotation : fieldVar.annotations()) { if (annotation.getAnnotationClass().name().equals(annotationClass.getSimpleName())) { return annotation; } } } } return null; }
Example #29
Source File: SpringShortcutMappingMethodAnnotationRule.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
@Override public JAnnotationUse apply(ApiActionMetadata endpointMetadata, JMethod generatableType) { JAnnotationUse requestMappingAnnotation; switch (RequestMethod.valueOf(endpointMetadata.getActionType().name())) { case GET: requestMappingAnnotation = generatableType.annotate(GetMapping.class); break; case POST: requestMappingAnnotation = generatableType.annotate(PostMapping.class); break; case PUT: requestMappingAnnotation = generatableType.annotate(PutMapping.class); break; case PATCH: requestMappingAnnotation = generatableType.annotate(PatchMapping.class); break; case DELETE: requestMappingAnnotation = generatableType.annotate(DeleteMapping.class); break; default: requestMappingAnnotation = generatableType.annotate(RequestMapping.class); requestMappingAnnotation.param("method", RequestMethod.valueOf(endpointMetadata.getActionType().name())); } if (StringUtils.isNotBlank(endpointMetadata.getUrl())) { requestMappingAnnotation.param("value", endpointMetadata.getUrl()); } return requestMappingAnnotation; }
Example #30
Source File: GeneratedClassAnnotationRule.java From springmvc-raml-plugin with Apache License 2.0 | 5 votes |
@Override public JAnnotationUse apply(ApiResourceMetadata controllerMetadata, JDefinedClass generatableType) { if (Config.isGeneratedAnnotation()) { return generatableType.annotate(Generated.class).param("value", Config.DEFAULT_GENERATED_ANNOTATION_VALUE); } return null; }