/** * Gets `mappedBy` attribute value from JPA relationship annotation * * @param jpaAnnotation * @return */ private String getMappedByValue(AnnotationMetadata jpaAnnotation) { AnnotationAttributeValue<?> mappedByValue = jpaAnnotation.getAttribute(MAPPEDBY_ATTRIBUTE); if (mappedByValue == null) { return null; } return ((StringAttributeValue) mappedByValue).getValue(); }
private Map<String, Object> getAttributeMap(final FieldMetadata field) { final Map<String, Object> map = new HashMap<String, Object>(); final AnnotationMetadata annotationMetadata = getMatchingAnnotation(field); if (annotationMetadata != null) { for (final JavaSymbolName attributeName : annotationMetadata.getAttributeNames()) { map.put(attributeName.getSymbolName(), annotationMetadata.getAttribute(attributeName) .getValue()); } } return map; }
/** * Return _mappedBy_ annotation attribute value of Jpa-relation-definition annotation * * @param parentField * @return */ private String getFieldMappedByAnnotationValue(FieldMetadata parentField) { AnnotationMetadata annotation = null; for (JavaType jpaAnnotation : Arrays.asList(JpaJavaType.ONE_TO_MANY, JpaJavaType.ONE_TO_ONE, JpaJavaType.MANY_TO_MANY)) { annotation = parentField.getAnnotation(jpaAnnotation); if (annotation != null) { break; } } if (annotation != null) { return (String) annotation.getAttribute("mappedBy").getValue(); } return null; }
/** * Constructor for using an existing {@link AnnotationMetadata} as a * baseline for building a new instance. * * @param existing required */ public AnnotationMetadataBuilder(final AnnotationMetadata existing) { Validate.notNull(existing); annotationType = existing.getAnnotationType(); for (final JavaSymbolName attributeName : existing.getAttributeNames()) { attributeValues.put(attributeName.getSymbolName(), existing.getAttribute(attributeName)); } this.setCommentStructure(existing.getCommentStructure()); }
/** * Gets value of a nested annotation attribute value * * @param newstedAnnotationAttr * @param attributeName * @return */ private <T> T getNestedAttributeValue(NestedAnnotationAttributeValue nestedAnnotationAttr, String attributeName) { AnnotationMetadata annotationValue = nestedAnnotationAttr.getValue(); if (annotationValue == null) { return null; } AnnotationAttributeValue<?> attribute = annotationValue.getAttribute(attributeName); if (attribute == null) { return null; } return (T) attribute.getValue(); }
@Override public Object getTagValue(final MemberHoldingTypeDetails type) { final AnnotationMetadata layerAnnotation = MemberFindingUtils.getAnnotationOfType(type.getAnnotations(), layerAnnotationType); if (layerAnnotation == null || layerAnnotation.getAttribute(domainTypesAttribute) == null) { return null; } final AnnotationAttributeValue<?> value = layerAnnotation.getAttribute(domainTypesAttribute); final List<JavaType> domainTypes = new ArrayList<JavaType>(); if (value instanceof ClassAttributeValue) { domainTypes.add(((ClassAttributeValue) value).getValue()); } else if (value instanceof ArrayAttributeValue<?>) { final ArrayAttributeValue<?> castValue = (ArrayAttributeValue<?>) value; for (final AnnotationAttributeValue<?> val : castValue.getValue()) { if (val instanceof ClassAttributeValue) { domainTypes.add(((ClassAttributeValue) val).getValue()); } } } return domainTypes; } }
/** * Gets attributo of a nested annotation attribute value * * @param newstedAnnotationAttr * @param attributeName * @return */ private <T> T getNestedAttributeValue(NestedAnnotationAttributeValue newstedAnnotationAttr, String attributeName) { AnnotationMetadata annotationValue = newstedAnnotationAttr.getValue(); if (annotationValue == null) { return null; } AnnotationAttributeValue<?> attribute = annotationValue.getAttribute(attributeName); if (attribute == null) { return null; } return (T) attribute.getValue(); }
private boolean isNullableJoinColumn(final FieldMetadata field) { final AnnotationMetadata joinColumnAnnotation = field.getAnnotation(JOIN_COLUMN); if (joinColumnAnnotation == null) { return true; } final AnnotationAttributeValue<?> nullableAttr = joinColumnAnnotation.getAttribute(new JavaSymbolName("nullable")); return nullableAttr == null || (Boolean) nullableAttr.getValue(); }
@Override protected ItdTypeDetailsProvidingMetadataItem getMetadata( final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) { AnnotationMetadata annotation = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation( RooJavaType.ROO_WEB_MVC_CONFIGURATION); // Getting language attribute AnnotationAttributeValue<String> defaultLanguageAttr = annotation.getAttribute("defaultLanguage"); String defaultLanguage = ""; if (defaultLanguageAttr != null) { defaultLanguage = defaultLanguageAttr.getValue(); } return new WebMvcConfigurationMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, defaultLanguage); }
private String getSuffix(final List<MemberHoldingTypeDetails> memberHoldingTypeDetailsList, final boolean singular, final Map<String, String> pluralMap) { final ClassOrInterfaceTypeDetails cid = getMostConcreteClassOrInterfaceTypeDetails(memberHoldingTypeDetailsList); if (singular) { return cid.getName().getSimpleTypeName(); } String plural = pluralMap.get(cid.getDeclaredByMetadataId()); for (final AnnotationMetadata annotationMetadata : cid.getAnnotations()) { if (annotationMetadata.getAnnotationType().getFullyQualifiedTypeName() .equals(ROO_PLURAL.getFullyQualifiedTypeName())) { final AnnotationAttributeValue<?> annotationAttributeValue = annotationMetadata.getAttribute(new JavaSymbolName("value")); if (annotationAttributeValue != null) { plural = annotationAttributeValue.getValue().toString(); } break; } } if (StringUtils.isNotBlank(plural)) { plural = StringUtils.capitalize(plural); } return plural; }
@Override public JavaType getEntityFactory(JavaType entity) { Set<ClassOrInterfaceTypeDetails> dataOnDemandCids = typeLocationService .findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY); JavaType typeToReturn = null; for (ClassOrInterfaceTypeDetails cid : dataOnDemandCids) { if (entity.equals((JavaType) cid.getAnnotation(RooJavaType.ROO_JPA_ENTITY_FACTORY) .getAttribute("entity").getValue())) { typeToReturn = cid.getName(); break; } } return typeToReturn; }
/** * This indicator says if --defaultReturnType parameter should be visible or not. * * @param context ShellContext * @return false if domain entity specified in --entity parameter has no associated Projections. */ @CliOptionVisibilityIndicator( params = "defaultReturnType", command = "repository jpa", help = "--defaultReturnType parameter is not visible if domain entity specified in --entity parameter has no associated Projections.") public boolean isDefaultReturnTypeParameterVisible(ShellContext shellContext) { // Get current value of 'entity' JavaType entity = getTypeFromEntityParam(shellContext); if (entity == null) { return false; } Set<ClassOrInterfaceTypeDetails> projectionsInProject = typeLocationService .findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_ENTITY_PROJECTION); boolean visible = false; for (ClassOrInterfaceTypeDetails projection : projectionsInProject) { // Add only projections associated to the entity specified in the command if (projection.getAnnotation(RooJavaType.ROO_ENTITY_PROJECTION).getAttribute("entity") .getValue().equals(entity)) { visible = true; break; } } return visible; }
private JavaSymbolName getUserDefinedMethod( final List<MemberHoldingTypeDetails> memberHoldingTypeDetailsList, final Map<String, String> pluralMap) { if (catalystAnnotationType == null || userDefinedNameAttribute == null) { return null; } final String suffix = suffixPlural || suffixSingular ? getSuffix(memberHoldingTypeDetailsList, suffixSingular, pluralMap) : ""; final ClassOrInterfaceTypeDetails cid = getMostConcreteClassOrInterfaceTypeDetails(memberHoldingTypeDetailsList); for (final AnnotationMetadata annotationMetadata : cid.getAnnotations()) { if (annotationMetadata.getAnnotationType().getFullyQualifiedTypeName() .equals(catalystAnnotationType.getFullyQualifiedTypeName())) { final AnnotationAttributeValue<?> annotationAttributeValue = annotationMetadata.getAttribute(userDefinedNameAttribute); if (annotationAttributeValue != null && StringUtils.isNotBlank(annotationAttributeValue.getValue().toString())) { return new JavaSymbolName(annotationAttributeValue.getValue().toString() + suffix); } break; } } return defaultName == null ? null : new JavaSymbolName(defaultName + suffix); }
@Override public JavaType getDataOnDemand(JavaType entity) { Set<ClassOrInterfaceTypeDetails> dataOnDemandCids = typeLocationService .findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_JPA_DATA_ON_DEMAND); JavaType typeToReturn = null; for (ClassOrInterfaceTypeDetails cid : dataOnDemandCids) { if (entity.equals((JavaType) cid.getAnnotation(RooJavaType.ROO_JPA_DATA_ON_DEMAND) .getAttribute("entity").getValue())) { typeToReturn = cid.getName(); break; } } return typeToReturn; }
private JavaType getEntityDeserializerFor(JavaType entity) { Set<ClassOrInterfaceTypeDetails> deserializers = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation( RooJavaType.ROO_DESERIALIZER); for (ClassOrInterfaceTypeDetails deserializer : deserializers) { AnnotationMetadata annotation = deserializer.getAnnotation(RooJavaType.ROO_DESERIALIZER); AnnotationAttributeValue<JavaType> annotationValue = annotation.getAttribute("entity"); if (entity.equals(annotationValue.getValue())) { return deserializer.getType(); } } return null; }
@Override protected ItdTypeDetailsProvidingMetadataItem getMetadata( final String metadataIdentificationString, final JavaType aspectName, final PhysicalTypeMetadata governorPhysicalTypeMetadata, final String itdFilename) { final JsonControllerIntegrationTestAnnotationValues annotationValues = new JsonControllerIntegrationTestAnnotationValues(governorPhysicalTypeMetadata); // Get JSON controller target class final JavaType jsonController = annotationValues.getTargetClass(); // Get the controller managed entity ClassOrInterfaceTypeDetails controllerCid = getTypeLocationService().getTypeDetails(jsonController); AnnotationMetadata rooControllerAnnotation = controllerCid.getAnnotation(RooJavaType.ROO_CONTROLLER); Validate.notNull(rooControllerAnnotation, "The target class must be annotated with @RooController."); Validate.notNull(rooControllerAnnotation.getAttribute("entity"), "The @RooController must have an 'entity' attribute, targeting managed entity."); final JavaType managedEntity = (JavaType) rooControllerAnnotation.getAttribute("entity").getValue(); // Get the entity factory of managed entity final JavaType entityFactory = getJpaEntityFactoryLocator().getFirstJpaEntityFactoryForEntity(managedEntity); // Get the service related to managed entity final JavaType entityService = getServiceLocator().getFirstService(managedEntity).getType(); return new JsonControllerIntegrationTestMetadata(metadataIdentificationString, aspectName, governorPhysicalTypeMetadata, annotationValues, jsonController, managedEntity, entityFactory, entityService); }
/** * This indicator says if --returnType parameter should be visible or not. * * @param context ShellContext * @return false if domain entity specified in --entity parameter has no associated Projections. */ @CliOptionVisibilityIndicator(params = "returnType", command = "finder add", help = "--returnType parameter is not visible if --entity parameter hasn't " + "been specified before or if there aren't exist any Projection class associated " + "to the current entity.") public boolean isReturnTypeParameterVisible(ShellContext shellContext) { // Get current value of 'entity' JavaType entity = getTypeFromEntityParam(shellContext); if (entity == null) { return false; } Set<ClassOrInterfaceTypeDetails> projectionsInProject = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation( RooJavaType.ROO_ENTITY_PROJECTION); for (ClassOrInterfaceTypeDetails projection : projectionsInProject) { // Add only projections associated to the entity specified in the command if (projection.getAnnotation(RooJavaType.ROO_ENTITY_PROJECTION).getAttribute("entity") .getValue().equals(entity)) { return true; } } return false; }
/** * Creates the Factory class of the child entity of relationship, only if parent entity has JPA unit tests. * * @param childType * @param parentType */ private void createChildEntityFactory(JavaType childType, JavaType parentType) { // Find current JPA unit tests Set<JavaType> unitTestTypes = typeLocationService.findTypesWithAnnotation(RooJavaType.ROO_JPA_UNIT_TEST); for (JavaType unitTestType : unitTestTypes) { // Get the annotation @RooJpaUnitTest ClassOrInterfaceTypeDetails cid = typeLocationService.getTypeDetails(unitTestType); AnnotationMetadata rooUnitTestAnnotation = cid.getAnnotation(RooJavaType.ROO_JPA_UNIT_TEST); // Check if parent entity has JPA unit test class AnnotationAttributeValue<Object> targetClass = rooUnitTestAnnotation.getAttribute("targetClass"); Validate.notNull(targetClass, String.format( "'targetClass' attribute can't be found for annotation @RooJpaUnitTest in class %s", unitTestType.getSimpleTypeName())); if (parentType.equals(targetClass.getValue())) { if (jpaEntityFactoryLocator.getFirstJpaEntityFactoryForEntity(childType) == null) { // Create factory class for child entity if doesn't exist dataOnDemandCreatorProvider.createEntityFactory(childType); } break; } } }
private String getEntityFactoryMetadataId(final JavaType javaType, final Iterable<ClassOrInterfaceTypeDetails> entityFactoryTypes) { for (final ClassOrInterfaceTypeDetails cid : entityFactoryTypes) { final AnnotationMetadata entityFactoryAnnotation = cid.getAnnotation(ROO_JPA_ENTITY_FACTORY); final AnnotationAttributeValue<JavaType> entityAttribute = entityFactoryAnnotation.getAttribute("entity"); if (entityAttribute != null && entityAttribute.getValue().equals(javaType)) { // Found the DoD type for the given field's type return JpaDataOnDemandMetadata.createIdentifier(cid.getName(), PhysicalTypeIdentifier.getPath(cid.getDeclaredByMetadataId())); } } return null; }
private String getDigitsBody(final FieldMetadata field, final AnnotationMetadata digitsAnnotation, final String suffix) { final String fieldName = field.getFieldName().getSymbolName(); final JavaType fieldType = field.getFieldType(); final Integer integerValue = (Integer) digitsAnnotation.getAttribute(new JavaSymbolName("integer")).getValue(); final Integer fractionValue = (Integer) digitsAnnotation.getAttribute(new JavaSymbolName("fraction")).getValue(); final BigDecimal maxValue = new BigDecimal(StringUtils.rightPad("9", integerValue, '9') + "." + StringUtils.rightPad("9", fractionValue, '9')); final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder(); if (fieldType.equals(BIG_DECIMAL)) { bodyBuilder.appendFormalLine("if (" + fieldName + ".compareTo(new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\")) == 1) {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(fieldName + " = new " + BIG_DECIMAL.getSimpleTypeName() + "(\"" + maxValue + "\");"); } else { bodyBuilder.appendFormalLine("if (" + fieldName + " > " + maxValue.doubleValue() + suffix + ") {"); bodyBuilder.indent(); bodyBuilder.appendFormalLine(fieldName + " = " + maxValue.doubleValue() + suffix + ";"); } bodyBuilder.indentRemove(); bodyBuilder.appendFormalLine("}"); return bodyBuilder.getOutput(); }