Skip to content

Commit 44b8d7f

Browse files
committed
fixes #105 Validate SQL provider method name
Reports error when 1) the specified method is not found or 2) the return type of the method is not assignable to CharSequence
1 parent 64e722a commit 44b8d7f

File tree

2 files changed

+96
-4
lines changed

2 files changed

+96
-4
lines changed

mybatipse/src/net/harawata/mybatipse/MybatipseConstants.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public class MybatipseConstants
4141

4242
public static final String ANNOTATION_SELECT_PROVIDER = "org.apache.ibatis.annotations.SelectProvider";
4343

44+
public static final String ANNOTATION_INSERT_PROVIDER = "org.apache.ibatis.annotations.InsertProvider";
45+
46+
public static final String ANNOTATION_UPDATE_PROVIDER = "org.apache.ibatis.annotations.UpdateProvider";
47+
48+
public static final String ANNOTATION_DELETE_PROVIDER = "org.apache.ibatis.annotations.DeleteProvider";
49+
4450
public static final String ANNOTATION_UPDATE = "org.apache.ibatis.annotations.Update";
4551

4652
public static final String ANNOTATION_INSERT = "org.apache.ibatis.annotations.Insert";
@@ -63,9 +69,11 @@ public class MybatipseConstants
6369

6470
public static final List<String> STATEMENT_ANNOTATIONS = Arrays.asList(ANNOTATION_SELECT,
6571
ANNOTATION_INSERT, ANNOTATION_UPDATE, ANNOTATION_DELETE, ANNOTATION_SELECT_PROVIDER,
66-
"org.apache.ibatis.annotations.InsertProvider",
67-
"org.apache.ibatis.annotations.UpdateProvider",
68-
"org.apache.ibatis.annotations.DeleteProvider");
72+
ANNOTATION_INSERT_PROVIDER, ANNOTATION_UPDATE_PROVIDER, ANNOTATION_DELETE_PROVIDER);
73+
74+
public static final List<String> PROVIDER_ANNOTATIONS = Arrays.asList(
75+
ANNOTATION_SELECT_PROVIDER, ANNOTATION_INSERT_PROVIDER, ANNOTATION_UPDATE_PROVIDER,
76+
ANNOTATION_DELETE_PROVIDER);
6977

7078
public static final List<String> PROVIDER_ANNOTATION_NAMES = Arrays.asList("SelectProvider",
7179
"InsertProvider", "UpdateProvider", "DeleteProvider");

mybatipse/src/net/harawata/mybatipse/apt/MybatipseAnnotationProcessorFactory.java

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@
3030
import javax.lang.model.element.TypeElement;
3131
import javax.tools.Diagnostic.Kind;
3232

33+
import org.eclipse.core.runtime.NullProgressMonitor;
34+
import org.eclipse.core.runtime.Status;
3335
import 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;
3440
import org.eclipse.jdt.internal.apt.pluggable.core.dispatch.IdeProcessingEnvImpl;
3541

42+
import net.harawata.mybatipse.Activator;
3643
import net.harawata.mybatipse.MybatipseConstants;
3744
import net.harawata.mybatipse.bean.BeanPropertyCache;
3845
import net.harawata.mybatipse.mybatis.ValidatorHelper;
@@ -44,7 +51,9 @@
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
})
4958
public 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

Comments
 (0)