Skip to content

Commit 9e9b343

Browse files
committed
#30 Use factories to create quick fix instances
1 parent 9e74420 commit 9e9b343

File tree

5 files changed

+150
-67
lines changed

5 files changed

+150
-67
lines changed

org.mapstruct.eclipse/src/org/mapstruct/eclipse/internal/quickfix/MapStructMarkerResolutionGenerator.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.eclipse.core.runtime.CoreException;
2828
import org.eclipse.ui.IMarkerResolution;
2929
import org.eclipse.ui.IMarkerResolutionGenerator;
30-
import org.mapstruct.eclipse.internal.quickfix.fixes.AddIgnoreTargetMappingAnnotationQuickFix;
30+
import org.mapstruct.eclipse.internal.quickfix.factories.UnmappedTargetPropertyQFFactory;
3131

3232
/**
3333
* Creates possible marker resolutions, i.e. quick fixes for errors that read like being generated by MapStruct
@@ -38,7 +38,7 @@ public class MapStructMarkerResolutionGenerator implements IMarkerResolutionGene
3838

3939
private static final IMarkerResolution[] NO_RESOLUTIONS = new IMarkerResolution[0];
4040

41-
private Collection<? extends MapStructQuickFix> allFixes = allQuickFixes();
41+
private Collection<? extends QuickFixFactory> allFactories = allQuickFixFactories();
4242

4343
@Override
4444
public IMarkerResolution[] getResolutions(IMarker mk) {
@@ -57,19 +57,17 @@ private static boolean isAptCompileProblem(IMarker mk) throws CoreException {
5757
return "org.eclipse.jdt.apt.pluggable.core.compileProblem".equals( mk.getType() );
5858
}
5959

60-
private IMarkerResolution[] findApplicableFixes(IMarker mk) throws CoreException {
60+
private IMarkerResolution[] findApplicableFixes(IMarker marker) throws CoreException {
6161
List<IMarkerResolution> result = new ArrayList<IMarkerResolution>();
62-
for ( MapStructQuickFix fix : allFixes ) {
63-
if ( fix.canFix( mk ) ) {
64-
result.add( fix );
65-
}
62+
for ( QuickFixFactory fix : allFactories ) {
63+
result.addAll( fix.createQuickFix( marker ) );
6664
}
6765
return result.toArray( new IMarkerResolution[result.size()] );
6866
}
6967

70-
private static Collection<? extends MapStructQuickFix> allQuickFixes() {
68+
private static Collection<? extends QuickFixFactory> allQuickFixFactories() {
7169
return Arrays.asList(
72-
new AddIgnoreTargetMappingAnnotationQuickFix()
70+
new UnmappedTargetPropertyQFFactory()
7371
);
7472
}
7573
}

org.mapstruct.eclipse/src/org/mapstruct/eclipse/internal/quickfix/MapStructQuickFix.java

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,6 @@
5252
*/
5353
@SuppressWarnings("restriction")
5454
public abstract class MapStructQuickFix implements IMarkerResolution2 {
55-
private String label;
56-
57-
protected MapStructQuickFix(String label) {
58-
this.label = label;
59-
}
60-
61-
@Override
62-
public String getLabel() {
63-
return label;
64-
}
6555

6656
@Override
6757
public void run(IMarker marker) {
@@ -70,7 +60,7 @@ public void run(IMarker marker) {
7060
IResource resource = marker.getResource();
7161
IJavaElement javaElement = JavaCore.create( resource );
7262

73-
compilationUnit = (ICompilationUnit) javaElement.getAdapter( ICompilationUnit.class );
63+
compilationUnit = javaElement.getAdapter( ICompilationUnit.class );
7464
IEditorInput input = EditorUtility.getEditorInput( compilationUnit );
7565
if ( input != null ) {
7666
CompilationUnit astCompilationUnit = toAST( compilationUnit );
@@ -180,21 +170,6 @@ public Image getImage() {
180170
return JavaPluginImages.get( JavaPluginImages.IMG_CORRECTION_CHANGE );
181171
}
182172

183-
public abstract boolean canFix(IMarker marker);
184-
185-
/**
186-
* @param marker the marker
187-
* @return the message of the marker, or an empty string in case the message cannot be retrieved
188-
*/
189-
protected static String getMessage(IMarker marker) {
190-
try {
191-
return (String) marker.getAttribute( IMarker.MESSAGE );
192-
}
193-
catch ( CoreException e ) {
194-
return "";
195-
}
196-
}
197-
198173
private static CompilationUnit toAST(ICompilationUnit unit) {
199174
CompilationUnit astRoot = SharedASTProvider.getAST( unit, SharedASTProvider.WAIT_YES, null );
200175
if ( astRoot == null ) {
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.eclipse.internal.quickfix;
20+
21+
import java.util.List;
22+
23+
import org.eclipse.core.resources.IMarker;
24+
import org.eclipse.core.runtime.CoreException;
25+
26+
/**
27+
* @author Andreas Gudian
28+
*
29+
*/
30+
public abstract class QuickFixFactory {
31+
/**
32+
* @param marker the problem marker to create a fix for
33+
* @return the quick fixes, or an empty list if the fix is not applicable to the given marker
34+
*/
35+
public abstract List<? extends MapStructQuickFix> createQuickFix(IMarker marker);
36+
37+
/**
38+
* @param marker the marker
39+
* @return the message of the marker, or an empty string in case the message cannot be retrieved
40+
*/
41+
protected static String getMessage(IMarker marker) {
42+
try {
43+
return (String) marker.getAttribute( IMarker.MESSAGE );
44+
}
45+
catch ( CoreException e ) {
46+
return "";
47+
}
48+
}
49+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/**
2+
* Copyright 2012-2015 Gunnar Morling (http://www.gunnarmorling.de/)
3+
* and/or other contributors as indicated by the @authors tag. See the
4+
* copyright.txt file in the distribution for a full listing of all
5+
* contributors.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*/
19+
package org.mapstruct.eclipse.internal.quickfix.factories;
20+
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.List;
24+
import java.util.regex.Matcher;
25+
import java.util.regex.Pattern;
26+
27+
import org.eclipse.core.resources.IMarker;
28+
import org.mapstruct.eclipse.internal.quickfix.MapStructQuickFix;
29+
import org.mapstruct.eclipse.internal.quickfix.QuickFixFactory;
30+
import org.mapstruct.eclipse.internal.quickfix.fixes.AddIgnoreTargetMappingAnnotationQuickFix;
31+
32+
/**
33+
* Quick-Fix factory for warning/error "Unmapped target properties..."
34+
*
35+
* @author Andreas Gudian
36+
*/
37+
public class UnmappedTargetPropertyQFFactory extends QuickFixFactory {
38+
39+
private static final Pattern PATTERN = Pattern.compile( "Unmapped target (property|properties): \"([^\"]+)\"." );
40+
41+
@Override
42+
public List<? extends MapStructQuickFix> createQuickFix(IMarker marker) {
43+
List<String> properties = extractPropertiesFromMessage( getMessage( marker ) );
44+
45+
if ( !properties.isEmpty() ) {
46+
return Arrays.asList( new AddIgnoreTargetMappingAnnotationQuickFix( properties ) );
47+
}
48+
49+
return Collections.emptyList();
50+
}
51+
52+
private static List<String> extractPropertiesFromMessage(String msg) {
53+
Matcher matcher = PATTERN.matcher( msg );
54+
if ( matcher.matches() ) {
55+
String props = matcher.group( 2 );
56+
return Arrays.asList( props.split( ", " ) );
57+
}
58+
59+
return Collections.emptyList();
60+
}
61+
}

org.mapstruct.eclipse/src/org/mapstruct/eclipse/internal/quickfix/fixes/AddIgnoreTargetMappingAnnotationQuickFix.java

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

21-
import java.util.Arrays;
21+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPINGS_FQ_NAME;
22+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPINGS_SIMPLE_NAME;
23+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_FQ_NAME;
24+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_MEMBER_IGNORE;
25+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_MEMBER_TARGET;
26+
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_SIMPLE_NAME;
27+
2228
import java.util.Collection;
2329
import java.util.LinkedList;
24-
import java.util.regex.Matcher;
25-
import java.util.regex.Pattern;
30+
import java.util.List;
2631

2732
import org.eclipse.core.resources.IMarker;
2833
import org.eclipse.core.runtime.CoreException;
@@ -44,41 +49,46 @@
4449
import org.mapstruct.eclipse.internal.quickfix.MapStructQuickFix;
4550
import org.mapstruct.eclipse.internal.quickfix.visitors.FindAnnotationByNameVisitor;
4651

47-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPINGS_FQ_NAME;
48-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPINGS_SIMPLE_NAME;
49-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_FQ_NAME;
50-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_MEMBER_IGNORE;
51-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_MEMBER_TARGET;
52-
import static org.mapstruct.eclipse.internal.MapStructAPIConstants.MAPPING_SIMPLE_NAME;
53-
5452
/**
55-
* Quick fix for error/warning "Unmapped target property" / "Unmapped target properties" that adds
56-
* {@code @Mapping( target = "<property>", ignore = true)} to the method.
53+
* Quick fix that adds {@code @Mapping( target = "<property>", ignore = true)} to the method.
5754
*
5855
* @author Andreas Gudian
5956
*/
6057
public class AddIgnoreTargetMappingAnnotationQuickFix extends MapStructQuickFix {
6158

62-
public AddIgnoreTargetMappingAnnotationQuickFix() {
63-
super( "Ignore unmapped target properties" );
59+
private final List<String> properties;
60+
61+
public AddIgnoreTargetMappingAnnotationQuickFix(List<String> properties) {
62+
this.properties = properties;
6463
}
6564

6665
@Override
67-
public boolean canFix(IMarker marker) {
68-
String message = getMessage( marker );
66+
public String getLabel() {
67+
String suffix = ( properties.size() > 1 ? "ies." : "y " + properties.get( 0 ) );
6968

70-
return message.startsWith( "Unmapped target property:" )
71-
|| message.startsWith( "Unmapped target properties:" );
69+
return "Ignore unmapped target propert" + suffix;
7270
}
7371

7472
@Override
75-
protected ASTRewrite getASTRewrite(CompilationUnit unit, ASTNode nodeWithMarker, IMarker marker) {
76-
Collection<String> properties = extractPropertiesFromMessage( getMessage( marker ) );
73+
public String getDescription() {
74+
String suffix = ( properties.size() > 1 ? "ies." : "y " + properties.get( 0 ) );
75+
76+
StringBuilder sb = new StringBuilder( "<html>Ignore target propert" );
7777

78-
if ( properties == null ) {
79-
return null;
78+
sb.append( suffix );
79+
sb.append( "<br><br>" );
80+
81+
for ( String prop : properties ) {
82+
sb.append( "<b>@Mapping( target = \"" ).append( prop ).append( "\", ignore = true )</b><br>" );
8083
}
8184

85+
sb.append( "</html>" );
86+
87+
return sb.toString();
88+
}
89+
90+
@Override
91+
protected ASTRewrite getASTRewrite(CompilationUnit unit, ASTNode nodeWithMarker, IMarker marker) {
8292
AST ast = unit.getAST();
8393

8494
ASTRewrite rewrite = ASTRewrite.create( ast );
@@ -204,14 +214,4 @@ private void addMappingAnnotations(Collection<String> properties, AST ast, ListR
204214
mappingList.insertFirst( mapping, null );
205215
}
206216
}
207-
208-
private static Collection<String> extractPropertiesFromMessage(String msg) {
209-
Pattern p = Pattern.compile( "Unmapped target (property|properties): \"([^\"]+)\"." );
210-
Matcher matcher = p.matcher( msg );
211-
if ( matcher.matches() ) {
212-
String props = matcher.group( 2 );
213-
return Arrays.asList( props.split( ", " ) );
214-
}
215-
return null;
216-
}
217217
}

0 commit comments

Comments
 (0)