Skip to content

Commit 11cb434

Browse files
committed
#37 Add support for @ValueMapping annotation in code completion proposals
1 parent c01aaf3 commit 11cb434

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ Eclipse update site for latest snapshot: https://mapstruct.ci.cloudbees.com/job/
77

88
### Code-Completions
99

10-
* Completion of `target` and `source` properties in `@Mapping` annotation for bean mappings and for enum mappings
10+
* Completion of `target` and `source` properties in `@Mapping` annotation for bean mappings
11+
* Completion of `target` and `source` enum constants in `@ValueMapping` annotations for enum mappings
1112
* Completion of `componentModel` values in `@Mapper` annotation
1213

1314
### Quick-Fixes

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import java.util.ArrayList;
2222
import java.util.Collection;
23+
import java.util.Collections;
2324
import java.util.HashSet;
2425
import java.util.List;
2526
import java.util.Set;
@@ -94,6 +95,10 @@ public static boolean containsAnnotation(IAnnotationBinding[] annotations, Strin
9495
* @return the enum constant names of the given type
9596
*/
9697
public static List<String> findAllEnumConstants(ITypeBinding type) {
98+
if ( !type.isEnum() ) {
99+
return Collections.emptyList();
100+
}
101+
97102
IVariableBinding[] declaredFields = type.getDeclaredFields();
98103

99104
List<String> result = new ArrayList<String>( declaredFields.length );

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,27 @@ private MapStructAPIConstants() {
4545
* Simple name of the annotation Mapping
4646
*/
4747
public static final String MAPPING_SIMPLE_NAME = "Mapping"; //$NON-NLS-1$
48+
49+
/**
50+
* Simple name of the annotation ValueMapping
51+
*/
52+
public static final String VALUE_MAPPING_SIMPLE_NAME = "ValueMapping"; //$NON-NLS-1$
53+
4854
/**
4955
* Fully qualified name of the annotation Mapping
5056
*/
5157
public static final String MAPPING_FQ_NAME = ORG_MAPSTRUCT + MAPPING_SIMPLE_NAME;
5258

59+
/**
60+
* Fully qualified name of the annotation ValueMapping
61+
*/
62+
public static final String VALUE_MAPPING_FQ_NAME = ORG_MAPSTRUCT + VALUE_MAPPING_SIMPLE_NAME;
63+
5364
/**
5465
* Simple name of the annotation Mappings
5566
*/
5667
public static final String MAPPINGS_SIMPLE_NAME = "Mappings"; //$NON-NLS-1$
68+
5769
/**
5870
* Fully qualified name of the annotation Mappings
5971
*/

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

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@
1818
*/
1919
package org.mapstruct.eclipse.internal;
2020

21+
import static org.mapstruct.eclipse.internal.Bindings.containsAnnotation;
22+
import static org.mapstruct.eclipse.internal.Bindings.findAllMethodNames;
23+
import static org.mapstruct.eclipse.internal.Bindings.getAnnotationQualifiedName;
24+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPINGS_FQ_NAME;
25+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPINGS_SIMPLE_NAME;
26+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_FQ_NAME;
27+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_MEMBER_SOURCE;
28+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_MEMBER_TARGET;
29+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_SIMPLE_NAME;
30+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_TARGET_FQ_NAME;
31+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.TARGET_TYPE_FQ_NAME;
32+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.VALUE_MAPPING_FQ_NAME;
33+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.VALUE_MAPPING_SIMPLE_NAME;
34+
2135
import java.beans.Introspector;
2236
import java.util.ArrayList;
2337
import java.util.Arrays;
@@ -44,18 +58,6 @@
4458
import org.eclipse.jface.text.contentassist.CompletionProposal;
4559
import org.eclipse.jface.text.contentassist.ICompletionProposal;
4660

47-
import static org.mapstruct.eclipse.internal.Bindings.containsAnnotation;
48-
import static org.mapstruct.eclipse.internal.Bindings.findAllMethodNames;
49-
import static org.mapstruct.eclipse.internal.Bindings.getAnnotationQualifiedName;
50-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPINGS_FQ_NAME;
51-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPINGS_SIMPLE_NAME;
52-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_FQ_NAME;
53-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_MEMBER_SOURCE;
54-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_MEMBER_TARGET;
55-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_SIMPLE_NAME;
56-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_TARGET_FQ_NAME;
57-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.TARGET_TYPE_FQ_NAME;
58-
5961
/**
6062
* Computes MapStruct specific content assist completion proposals for the <code>@Mapping</code> annotation.
6163
*
@@ -86,20 +88,23 @@ private PropertyNameProposalCollector(int invocationOffset) {
8688
public boolean visit(MemberValuePair node) {
8789
String annotationQualifiedName = getAnnotationQualifiedName( node.resolveMemberValuePairBinding() );
8890

89-
if ( MAPPING_FQ_NAME.equals( annotationQualifiedName )
91+
if ( isSupportedAnnotation( annotationQualifiedName )
9092
&& isInRange( invocationOffset, node.getValue().getStartPosition(), node.getValue().getLength() )
91-
&& isMappingAnnotationMethod( node ) ) {
93+
&& ( isSourceNode( node ) || isTargetNode( node ) ) ) {
9294

9395
valid = true;
9496

95-
if ( MAPPING_MEMBER_SOURCE.equals( node.getName().toString() ) ) {
96-
source = true;
97-
}
97+
source = isSourceNode( node );
9898
}
9999

100100
return false;
101101
}
102102

103+
private boolean isSupportedAnnotation(String annotationQualifiedName) {
104+
return MAPPING_FQ_NAME.equals( annotationQualifiedName )
105+
|| VALUE_MAPPING_FQ_NAME.equals( annotationQualifiedName );
106+
}
107+
103108
@Override
104109
public boolean visit(MethodDeclaration node) {
105110
if ( isInRange( invocationOffset, node.getStartPosition(), node.getLength() ) ) {
@@ -156,8 +161,8 @@ public void endVisit(MethodDeclaration node) {
156161
}
157162

158163
private boolean isEnumMapping() {
159-
return ( sourceNameToType.size() == 1 && sourceNameToType.values().iterator().next().isEnum()
160-
&& resultType.isEnum() );
164+
return ( ( sourceNameToType.size() == 1 && sourceNameToType.values().iterator().next().isEnum() )
165+
|| resultType.isEnum() );
161166
}
162167

163168
public Collection<String> getProperties() {
@@ -178,7 +183,9 @@ public boolean isValidValue() {
178183
MAPPING_FQ_NAME,
179184
MAPPING_SIMPLE_NAME,
180185
MAPPINGS_SIMPLE_NAME,
181-
MAPPINGS_FQ_NAME );
186+
MAPPINGS_FQ_NAME,
187+
VALUE_MAPPING_FQ_NAME,
188+
VALUE_MAPPING_SIMPLE_NAME );
182189

183190
private static final String GET_PREFIX = "get"; //$NON-NLS-1$
184191
private static final String SET_PREFIX = "set"; //$NON-NLS-1$
@@ -255,15 +262,11 @@ private List<String> findProperties(Collection<String> methodNames, String metho
255262
return returnValue;
256263
}
257264

258-
/**
259-
* Decides whether the given {@link MemberValuePair} is a <code>Mapping</code> annotation method.
260-
*/
261-
private boolean isMappingAnnotationMethod(MemberValuePair node) {
262-
if ( MAPPING_MEMBER_SOURCE.equals( node.getName().toString() )
263-
|| MAPPING_MEMBER_TARGET.equals( node.getName().toString() ) ) {
264-
return true;
265-
}
266-
return false;
265+
private boolean isTargetNode(MemberValuePair node) {
266+
return MAPPING_MEMBER_TARGET.equals( node.getName().toString() );
267267
}
268268

269+
private boolean isSourceNode(MemberValuePair node) {
270+
return MAPPING_MEMBER_SOURCE.equals( node.getName().toString() );
271+
}
269272
}

0 commit comments

Comments
 (0)