Skip to content

Commit 64e722a

Browse files
committed
fixes #104 Auto-complete 'method' attribute of SQL provider annotations
1 parent 09915f5 commit 64e722a

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed

mybatipse/src/net/harawata/mybatipse/mybatis/JavaCompletionProposalComputer.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,14 @@
2525
import org.eclipse.jdt.core.ICompilationUnit;
2626
import org.eclipse.jdt.core.IJavaElement;
2727
import org.eclipse.jdt.core.IJavaProject;
28+
import org.eclipse.jdt.core.IMemberValuePair;
2829
import org.eclipse.jdt.core.IMethod;
2930
import org.eclipse.jdt.core.ISourceRange;
3031
import org.eclipse.jdt.core.IType;
32+
import org.eclipse.jdt.core.ITypeHierarchy;
3133
import org.eclipse.jdt.core.JavaModelException;
34+
import org.eclipse.jdt.core.Signature;
35+
import org.eclipse.jdt.core.compiler.CharOperation;
3236
import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
3337
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
3438
import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
@@ -37,6 +41,7 @@
3741

3842
import net.harawata.mybatipse.Activator;
3943
import net.harawata.mybatipse.MybatipseConstants;
44+
import net.harawata.mybatipse.bean.JavaCompletionProposal;
4045
import net.harawata.mybatipse.bean.SupertypeHierarchyCache;
4146
import net.harawata.mybatipse.mybatis.JavaMapperUtil.MethodNameMatcher;
4247
import net.harawata.mybatipse.mybatis.JavaMapperUtil.MethodParametersStore;
@@ -106,6 +111,10 @@ else if ("Options".equals(elementName) || "SelectKey".equals(elementName))
106111
{
107112
return proposeKeyProperty(project, mapperFqn, offset, annotation, method);
108113
}
114+
else if (MybatipseConstants.PROVIDER_ANNOTATION_NAMES.contains(elementName))
115+
{
116+
return proposeProviderMethod(project, offset, annotation, monitor);
117+
}
109118
}
110119
}
111120
catch (JavaModelException e)
@@ -116,6 +125,69 @@ else if ("Options".equals(elementName) || "SelectKey".equals(elementName))
116125
return Collections.emptyList();
117126
}
118127

128+
private List<ICompletionProposal> proposeProviderMethod(final IJavaProject project,
129+
int offset, IAnnotation annotation, IProgressMonitor monitor) throws JavaModelException
130+
{
131+
List<ICompletionProposal> proposals = new ArrayList<>();
132+
final AnnotationParser parser = new AnnotationParser(annotation, offset);
133+
String key = parser.getKey();
134+
if ("method".equals(key))
135+
{
136+
for (IMemberValuePair valuePair : annotation.getMemberValuePairs())
137+
{
138+
String pairName = valuePair.getMemberName();
139+
if ("type".equals(pairName) || "value".equals(pairName))
140+
{
141+
String pairValue = (String)valuePair.getValue();
142+
IType ancestor = (IType)annotation.getAncestor(IJavaElement.TYPE);
143+
String providerFqn = resolvedTypeToRawTypeFqn(ancestor.resolveType(pairValue));
144+
if (providerFqn == null)
145+
{
146+
Activator.log(Status.ERROR, "Couldn't resolve " + pairValue + "Z.");
147+
break;
148+
}
149+
IType providerType = project.findType(providerFqn);
150+
IType charSequence = project.findType(CharSequence.class.getName());
151+
for (IMethod targetMethod : providerType.getMethods())
152+
{
153+
String targetNameStr = targetMethod.getElementName();
154+
char[] targetName = targetNameStr.toCharArray();
155+
char[] input = parser.getValue().toCharArray();
156+
if (CharOperation.prefixEquals(input, targetName)
157+
|| CharOperation.camelCaseMatch(input, targetName))
158+
{
159+
String returnTypeFqn = resolvedTypeToRawTypeFqn(
160+
ancestor.resolveType(Signature.toString(targetMethod.getReturnType())));
161+
if (returnTypeFqn == null)
162+
continue;
163+
IType returnType = project.findType(returnTypeFqn);
164+
if (returnType == null)
165+
continue;
166+
ITypeHierarchy returnTypeHierarchy = returnType.newSupertypeHierarchy(monitor);
167+
if (returnTypeHierarchy.contains(charSequence))
168+
{
169+
proposals.add(new JavaCompletionProposal(targetNameStr, offset - input.length,
170+
parser.getValueLength(), targetName.length, Activator.getIcon(),
171+
targetNameStr, null, null, 100));
172+
}
173+
}
174+
}
175+
}
176+
}
177+
}
178+
return proposals;
179+
}
180+
181+
private String resolvedTypeToRawTypeFqn(String[][] resolvedType)
182+
{
183+
if (resolvedType == null || resolvedType.length == 0)
184+
{
185+
return null;
186+
}
187+
String[] arr = resolvedType[0];
188+
return arr[0] + "." + arr[1];
189+
}
190+
119191
private List<ICompletionProposal> proposeConstructorArgs(final IJavaProject project,
120192
final String mapperFqn, int offset, IAnnotation annotation, final IMethod method)
121193
throws JavaModelException

0 commit comments

Comments
 (0)