3030import javax .lang .model .element .TypeElement ;
3131import javax .tools .Diagnostic .Kind ;
3232
33+ import org .eclipse .core .runtime .NullProgressMonitor ;
34+ import org .eclipse .core .runtime .Status ;
3335import org .eclipse .jdt .core .IJavaProject ;
36+ import org .eclipse .jdt .core .IMethod ;
37+ import org .eclipse .jdt .core .IType ;
38+ import org .eclipse .jdt .core .JavaModelException ;
39+ import org .eclipse .jdt .core .Signature ;
3440import org .eclipse .jdt .internal .apt .pluggable .core .dispatch .IdeProcessingEnvImpl ;
3541
42+ import net .harawata .mybatipse .Activator ;
3643import net .harawata .mybatipse .MybatipseConstants ;
3744import net .harawata .mybatipse .bean .BeanPropertyCache ;
3845import net .harawata .mybatipse .mybatis .ValidatorHelper ;
4451@ SuppressWarnings ("restriction" )
4552@ SupportedSourceVersion (SourceVersion .RELEASE_6 )
4653@ SupportedAnnotationTypes ({
47- MybatipseConstants .ANNOTATION_RESULT_MAP , MybatipseConstants .ANNOTATION_RESULTS
54+ MybatipseConstants .ANNOTATION_RESULT_MAP , MybatipseConstants .ANNOTATION_RESULTS ,
55+ MybatipseConstants .ANNOTATION_SELECT_PROVIDER , MybatipseConstants .ANNOTATION_INSERT_PROVIDER ,
56+ MybatipseConstants .ANNOTATION_UPDATE_PROVIDER , MybatipseConstants .ANNOTATION_DELETE_PROVIDER
4857})
4958public class MybatipseAnnotationProcessorFactory extends AbstractProcessor
5059{
@@ -71,13 +80,88 @@ else if (MybatipseConstants.ANNOTATION_RESULTS.equals(annotationType))
7180 {
7281 validateResultsAnnotation (element , annotationMirror , messager );
7382 }
83+ else if (MybatipseConstants .PROVIDER_ANNOTATIONS .contains (annotationType ))
84+ {
85+ validateProviderAnnotation (element , annotationMirror , messager );
86+ }
7487 }
7588 }
7689 }
7790 }
7891 return false ;
7992 }
8093
94+ private void validateProviderAnnotation (Element element , AnnotationMirror annotationMirror ,
95+ Messager messager )
96+ {
97+ AnnotationValue annotationValue = null ;
98+ String providerFqn = null ;
99+ String methodName = null ;
100+ Map <? extends ExecutableElement , ? extends AnnotationValue > elementValues = annotationMirror
101+ .getElementValues ();
102+ for (Entry <? extends ExecutableElement , ? extends AnnotationValue > entry : elementValues
103+ .entrySet ())
104+ {
105+ String memberName = entry .getKey ().getSimpleName ().toString ();
106+ if ("type" .equals (memberName ) || "value" .equals (memberName ))
107+ {
108+ providerFqn = entry .getValue ().getValue ().toString ();
109+ }
110+ else if ("method" .equals (memberName ))
111+ {
112+ annotationValue = entry .getValue ();
113+ methodName = (String )annotationValue .getValue ();
114+ }
115+ }
116+ if (providerFqn != null && methodName != null )
117+ {
118+ try
119+ {
120+ IJavaProject project = getJavaProject ();
121+ IType providerType = project .findType (providerFqn );
122+ boolean found = false ;
123+ for (IMethod method : providerType .getMethods ())
124+ {
125+ if (methodName .equals (method .getElementName ()))
126+ {
127+ found = true ;
128+ String returnTypeFqn = resolvedTypeToRawTypeFqn (
129+ providerType .resolveType (Signature .toString (method .getReturnType ())));
130+ if (returnTypeFqn == null || !project .findType (returnTypeFqn )
131+ .newSupertypeHierarchy (new NullProgressMonitor ())
132+ .contains (project .findType (CharSequence .class .getName ())))
133+ {
134+ messager .printMessage (Kind .ERROR ,
135+ "The return type must be assignable to CharSequence." , element ,
136+ annotationMirror , annotationValue );
137+ }
138+ break ;
139+ }
140+ }
141+ if (!found )
142+ {
143+ messager .printMessage (Kind .ERROR ,
144+ "Method '" + providerFqn + "." + methodName + "' not found." , element ,
145+ annotationMirror , annotationValue );
146+ }
147+ }
148+ catch (JavaModelException e )
149+ {
150+ Activator .log (Status .ERROR , "Could not find provider class: " + providerFqn , e );
151+ }
152+ }
153+ }
154+
155+ private String resolvedTypeToRawTypeFqn (String [][] resolvedType )
156+ {
157+ if (resolvedType == null || resolvedType .length == 0 )
158+ {
159+ return null ;
160+ }
161+ String [] arr = resolvedType [0 ];
162+ return arr [0 ] + "." + arr [1 ];
163+ }
164+
81165 protected void validateResultsAnnotation (Element element , AnnotationMirror annotationMirror ,
82166 Messager messager )
83167 {
0 commit comments