Skip to content

Commit 3b191da

Browse files
committed
#14 Property name completion: handle multiple sources, handle @MappingTarget/@TargetType parameters, support enum constants
1 parent cfcfa1b commit 3b191da

File tree

4 files changed

+208
-113
lines changed

4 files changed

+208
-113
lines changed

org.mapstruct.eclipse/src/org/mapstruct/eclipse/internal/AbstractAnnotationCompletionProposalComputer.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
import org.eclipse.jdt.core.IAnnotation;
2727
import org.eclipse.jdt.core.ICompilationUnit;
2828
import org.eclipse.jdt.core.IJavaElement;
29-
import org.eclipse.jdt.core.dom.IMemberValuePairBinding;
30-
import org.eclipse.jdt.core.dom.IMethodBinding;
31-
import org.eclipse.jdt.core.dom.ITypeBinding;
3229
import org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext;
3330
import org.eclipse.jdt.ui.text.java.IJavaCompletionProposalComputer;
3431
import org.eclipse.jdt.ui.text.java.JavaContentAssistInvocationContext;
@@ -127,20 +124,4 @@ protected boolean isInRange(int offset, int rangeStartPosition, int rangeLength)
127124
}
128125
return false;
129126
}
130-
131-
/**
132-
* Returns the qualified name of the annotation which is associated with the given {@link IMemberValuePairBinding}.
133-
*/
134-
protected String getAnnotationQualifiedName(IMemberValuePairBinding binding) {
135-
IMethodBinding methodBinding = binding.getMethodBinding();
136-
if ( methodBinding == null ) {
137-
return null;
138-
}
139-
ITypeBinding declaringClass = methodBinding.getDeclaringClass();
140-
if ( declaringClass == null ) {
141-
return null;
142-
}
143-
return declaringClass.getQualifiedName();
144-
}
145-
146127
}

org.mapstruct.eclipse/src/org/mapstruct/eclipse/internal/Bindings.java

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@
1818
*/
1919
package org.mapstruct.eclipse.internal;
2020

21+
import java.util.ArrayList;
22+
import java.util.Collection;
2123
import java.util.HashSet;
2224
import java.util.Set;
2325

26+
import org.eclipse.jdt.core.dom.IAnnotationBinding;
2427
import org.eclipse.jdt.core.dom.IBinding;
28+
import org.eclipse.jdt.core.dom.IMemberValuePairBinding;
2529
import org.eclipse.jdt.core.dom.IMethodBinding;
2630
import org.eclipse.jdt.core.dom.ITypeBinding;
31+
import org.eclipse.jdt.core.dom.IVariableBinding;
2732

2833
/**
2934
* Helper class to inspect various {@link IBinding}s.
@@ -39,25 +44,26 @@ private Bindings() {
3944
* @param type the type
4045
* @return the method names declared in the class or a super type of it
4146
*/
42-
public static Set<String> findAllMethodNames(ITypeBinding type) {
43-
Set<String> result = new HashSet<String>();
47+
public static Collection<String> findAllMethodNames(ITypeBinding type) {
48+
Collection<String> result = new HashSet<String>();
4449

4550
collectMethodNames( type, new HashSet<ITypeBinding>(), result );
4651

4752
return result;
4853
}
4954

50-
private static void collectMethodNames(ITypeBinding curr, Set<ITypeBinding> visited, Set<String> methodNames) {
51-
if ( !isJavaLangObject( curr ) && visited.add( curr ) ) {
52-
for ( IMethodBinding methodBinding : curr.getDeclaredMethods() ) {
55+
private static void collectMethodNames(ITypeBinding type, Set<ITypeBinding> visited,
56+
Collection<String> methodNames) {
57+
if ( !isJavaLangObject( type ) && visited.add( type ) ) {
58+
for ( IMethodBinding methodBinding : type.getDeclaredMethods() ) {
5359
methodNames.add( methodBinding.getName() );
5460
}
5561

56-
for ( ITypeBinding ifc : curr.getInterfaces() ) {
62+
for ( ITypeBinding ifc : type.getInterfaces() ) {
5763
collectMethodNames( ifc, visited, methodNames );
5864
}
5965

60-
ITypeBinding superClass = curr.getSuperclass();
66+
ITypeBinding superClass = type.getSuperclass();
6167
if ( superClass != null ) {
6268
collectMethodNames( superClass, visited, methodNames );
6369
}
@@ -67,4 +73,49 @@ private static void collectMethodNames(ITypeBinding curr, Set<ITypeBinding> visi
6773
private static boolean isJavaLangObject(ITypeBinding curr) {
6874
return curr.getQualifiedName().equals( "java.lang.Object" );
6975
}
76+
77+
/**
78+
* @param annotations the annotations
79+
* @param annotationName the fully qualified name of the annotation to look for
80+
* @return {@code true}, iff the given array of annotations contains an annotation with the given name
81+
*/
82+
public static boolean containsAnnotation(IAnnotationBinding[] annotations, String annotationName) {
83+
for ( IAnnotationBinding annotation : annotations ) {
84+
if ( annotation.getAnnotationType().getQualifiedName().equals( annotationName ) ) {
85+
return true;
86+
}
87+
}
88+
return false;
89+
}
90+
91+
/**
92+
* @param type the enum type
93+
* @return the enum constant names of the given type
94+
*/
95+
public static Collection<String> findAllEnumConstants(ITypeBinding type) {
96+
IVariableBinding[] declaredFields = type.getDeclaredFields();
97+
98+
Collection<String> result = new ArrayList<String>( declaredFields.length );
99+
100+
for ( IVariableBinding field : declaredFields ) {
101+
result.add( field.getName() );
102+
}
103+
104+
return result;
105+
}
106+
107+
/**
108+
* Returns the qualified name of the annotation which is associated with the given {@link IMemberValuePairBinding}.
109+
*/
110+
public static String getAnnotationQualifiedName(IMemberValuePairBinding binding) {
111+
IMethodBinding methodBinding = binding.getMethodBinding();
112+
if ( methodBinding == null ) {
113+
return null;
114+
}
115+
ITypeBinding declaringClass = methodBinding.getDeclaringClass();
116+
if ( declaringClass == null ) {
117+
return null;
118+
}
119+
return declaringClass.getQualifiedName();
120+
}
70121
}

org.mapstruct.eclipse/src/org/mapstruct/eclipse/internal/MapStructAPIConstants.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,14 @@ private MapStructAPIConstants() {
7373
* Member name of Mapping#ignore()
7474
*/
7575
public static final String MAPPING_MEMBER_IGNORE = "ignore"; //$NON-NLS-1$
76+
77+
/**
78+
* Fully qualified name of the annotation TargetType
79+
*/
80+
public static final String TARGET_TYPE_FQ_NAME = "org.mapstruct.TargetType"; //$NON-NLS-1$
81+
82+
/**
83+
* Fully qualified name of the annotation MappingTarget
84+
*/
85+
public static final String MAPPING_TARGET_FQ_NAME = "org.mapstruct.MappingTarget"; //$NON-NLS-1$
7686
}

0 commit comments

Comments
 (0)