diff --git a/plugins/org.springframework.ide.eclipse.data.core/plugin.properties b/plugins/org.springframework.ide.eclipse.data.core/plugin.properties index 4babd922d2..4f16460d0d 100644 --- a/plugins/org.springframework.ide.eclipse.data.core/plugin.properties +++ b/plugins/org.springframework.ide.eclipse.data.core/plugin.properties @@ -6,4 +6,8 @@ dataValidator.name=Data Validator dataValidator.rule.invalidDerivedQuery.name=Invalid Derived Query dataValidator.rule.invalidDerivedQuery.description=Checks for an invalid Spring Data query method. -dataValidator.rule.invalidDerivedQuery.message=Invalid Derived Query \ No newline at end of file +dataValidator.rule.invalidDerivedQuery.message=Invalid Derived Query + +dataValidator.rule.invalidParameterType.name=Invalid Parameter Type +dataValidator.rule.invalidParameterType.description=Checks for invalid parameter type for find by method. +dataValidator.rule.invalidParameterType.message=Invalid Parameter Type \ No newline at end of file diff --git a/plugins/org.springframework.ide.eclipse.data.core/plugin.xml b/plugins/org.springframework.ide.eclipse.data.core/plugin.xml index 0b190aa673..93a976f5ae 100644 --- a/plugins/org.springframework.ide.eclipse.data.core/plugin.xml +++ b/plugins/org.springframework.ide.eclipse.data.core/plugin.xml @@ -181,6 +181,14 @@ name="%dataValidator.rule.invalidDerivedQuery.name"> + + + diff --git a/plugins/org.springframework.ide.eclipse.data.core/src/main/java/org/springframework/ide/eclipse/data/internal/validation/InvalidParameterTypeRule.java b/plugins/org.springframework.ide.eclipse.data.core/src/main/java/org/springframework/ide/eclipse/data/internal/validation/InvalidParameterTypeRule.java new file mode 100644 index 0000000000..f8417f2dd8 --- /dev/null +++ b/plugins/org.springframework.ide.eclipse.data.core/src/main/java/org/springframework/ide/eclipse/data/internal/validation/InvalidParameterTypeRule.java @@ -0,0 +1,136 @@ +package org.springframework.ide.eclipse.data.internal.validation; + +import java.lang.reflect.Method; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.jdt.core.ILocalVariable; +import org.eclipse.jdt.core.IMethod; +import org.eclipse.jdt.core.ISourceRange; +import org.eclipse.jdt.core.IType; +import org.eclipse.jdt.core.ITypeRoot; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.core.Signature; +import org.springframework.ide.eclipse.core.SpringCore; +import org.springframework.ide.eclipse.core.StringUtils; +import org.springframework.ide.eclipse.core.model.IModelElement; +import org.springframework.ide.eclipse.core.model.java.JavaModelSourceLocation; +import org.springframework.ide.eclipse.core.model.validation.IValidationContext; +import org.springframework.ide.eclipse.core.model.validation.IValidationRule; +import org.springframework.ide.eclipse.core.model.validation.ValidationProblemAttribute; +import org.springframework.ide.eclipse.data.jdt.core.RepositoryInformation; + +/** + * @author Terry Denney + * @since 3.2.0 + * + */ +public class InvalidParameterTypeRule implements + IValidationRule { + + public boolean supports(IModelElement element, IValidationContext context) { + if (!(context instanceof SpringDataValidationContext)) { + return false; + } + + if (element instanceof CompilationUnit) { + CompilationUnit cu = (CompilationUnit) element; + ITypeRoot typeRoot = cu.getTypeRoot(); + + if (typeRoot == null) { + return false; + } + + IType type = typeRoot.findPrimaryType(); + if (type == null) { + return false; + } + + // Skip non-interfaces + try { + if (type == null || !type.isInterface() || type.isAnnotation()) { + return false; + } + } catch (JavaModelException e) { + SpringCore.log(e); + return false; + } + + // Skip non-spring-data repositories + if (!RepositoryInformation.isSpringDataRepository(type)) { + return false; + } + + // resolve repository information and generate problem markers + RepositoryInformation information = new RepositoryInformation(type); + + Class domainClass = information.getManagedDomainClass(); + if (domainClass == null) { + return false; + } + return true; + } + return false; + } + + public void validate(CompilationUnit element, + SpringDataValidationContext context, IProgressMonitor monitor) { + + ITypeRoot typeRoot = element.getTypeRoot(); + IType type = typeRoot.findPrimaryType(); + + // resolve repository information and generate problem markers + RepositoryInformation information = new RepositoryInformation(type); + + Class domainClass = information.getManagedDomainClass(); + if (domainClass == null) { + return; + } + + try { + for (IMethod method : type.getMethods()) { + String methodName = method.getElementName(); + if (methodName.startsWith("findBy")) { + String propertyName = StringUtils.uncapitalize(methodName + .substring("findBy".length())); + + ILocalVariable[] params = method.getParameters(); + + if (params.length == 1) { + String paramTypeSignature = params[0].getTypeSignature(); + Method propertyMethod = null; + try { + propertyMethod = domainClass.getMethod("get" + + StringUtils.capitalize(propertyName)); + } catch (NoSuchMethodException e) { + // not a property method... ignore + continue; + } + + if (propertyMethod != null) { + Class propertyReturnType = propertyMethod.getReturnType(); + String propertySimpleType = propertyReturnType.getSimpleName(); + String paramSimpleType = Signature.getSignatureSimpleName(paramTypeSignature); + if (propertySimpleType != null && !(propertySimpleType.equals(paramSimpleType))) { + element.setElementSourceLocation(new JavaModelSourceLocation(params[0])); + ISourceRange paramSourceRange = params[0].getSourceRange(); + ValidationProblemAttribute start = new ValidationProblemAttribute( + IMarker.CHAR_START, paramSourceRange.getOffset()); + ValidationProblemAttribute end = new ValidationProblemAttribute( + IMarker.CHAR_END, paramSourceRange.getOffset() + paramSourceRange.getLength()); + context.warning(element, "INVALID_PARAMETER_TYPE", + "Parameter type (" + paramSimpleType + ") does not match domain class property definition (" + propertySimpleType + ").", + new ValidationProblemAttribute[] {start, end }); + } + } + } + } + } + } catch (JavaModelException e) { + SpringCore.log(e); + } catch (SecurityException e) { + SpringCore.log(e); + } + } + +}