16
16
package org .inferred .freebuilder .processor .util .feature ;
17
17
18
18
import com .google .common .base .Optional ;
19
+ import com .google .common .collect .ImmutableList ;
19
20
20
21
import org .inferred .freebuilder .processor .util .Excerpt ;
21
22
import org .inferred .freebuilder .processor .util .QualifiedName ;
22
23
import org .inferred .freebuilder .processor .util .Shading ;
23
24
import org .inferred .freebuilder .processor .util .SourceBuilder ;
24
25
26
+ import java .util .List ;
27
+
25
28
import javax .annotation .processing .ProcessingEnvironment ;
26
29
import javax .lang .model .SourceVersion ;
27
30
import javax .lang .model .util .Elements ;
36
39
*/
37
40
public enum SourceLevel implements Feature <SourceLevel > {
38
41
39
- JAVA_6 ("Java 6" ), JAVA_7 ("Java 7" ), JAVA_8 ("Java 8+" );
42
+ JAVA_6 ("Java 6" , 6 ), JAVA_7 ("Java 7" , 7 ), JAVA_8 ("Java 8+" , 8 );
40
43
41
44
/**
42
45
* Constant to pass to {@link SourceBuilder#feature(FeatureType)} to get the current
@@ -73,20 +76,35 @@ protected SourceLevel forEnvironment(ProcessingEnvironment env, FeatureSet featu
73
76
private static final String ECLIPSE_DISPATCHER =
74
77
Shading .unshadedName ("org.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher" );
75
78
76
- public static Excerpt diamondOperator (final Object type ) {
77
- return new DiamondOperator (type );
79
+ /**
80
+ * An excerpt that uses the diamond operator (<>) whenever available.
81
+ */
82
+ public static Excerpt diamondOperator (Object type ) {
83
+ return new DiamondOperator ("diamondOperator" , type , JAVA_7 );
84
+ }
85
+
86
+ /**
87
+ * An excerpt that uses the diamond operator (<>) whenever nested inference is available
88
+ * (Java 8+).
89
+ */
90
+ public static Excerpt nestedDiamondOperator (Object type ) {
91
+ return new DiamondOperator ("nestedDiamondOperator" , type , JAVA_8 );
78
92
}
79
93
80
94
private static final class DiamondOperator extends Excerpt {
95
+ private final String methodName ;
81
96
private final Object type ;
97
+ private final SourceLevel minimumSourceLevel ;
82
98
83
- private DiamondOperator (Object type ) {
99
+ private DiamondOperator (String methodName , Object type , SourceLevel minimumSourceLevel ) {
100
+ this .methodName = methodName ;
84
101
this .type = type ;
102
+ this .minimumSourceLevel = minimumSourceLevel ;
85
103
}
86
104
87
105
@ Override
88
106
public void addTo (SourceBuilder source ) {
89
- if (source .feature (SOURCE_LEVEL ).compareTo (JAVA_7 ) >= 0 ) {
107
+ if (source .feature (SOURCE_LEVEL ).compareTo (minimumSourceLevel ) >= 0 ) {
90
108
source .add ("<>" );
91
109
} else {
92
110
source .add ("<%s>" , type );
@@ -95,19 +113,23 @@ public void addTo(SourceBuilder source) {
95
113
96
114
@ Override
97
115
public String toString () {
98
- return "diamondOperator (" + type + ")" ;
116
+ return methodName + " (" + type + ")" ;
99
117
}
100
118
101
119
@ Override
102
120
protected void addFields (FieldReceiver fields ) {
121
+ fields .add ("methodName" , methodName );
103
122
fields .add ("type" , type );
123
+ fields .add ("minimumSourceLevel" , minimumSourceLevel );
104
124
}
105
125
}
106
126
107
127
private final String humanReadableFormat ;
128
+ private final int version ;
108
129
109
- SourceLevel (String humanReadableFormat ) {
130
+ SourceLevel (String humanReadableFormat , int version ) {
110
131
this .humanReadableFormat = humanReadableFormat ;
132
+ this .version = version ;
111
133
}
112
134
113
135
public Optional <QualifiedName > safeVarargs () {
@@ -167,6 +189,10 @@ public Optional<QualifiedName> spliterator() {
167
189
}
168
190
}
169
191
192
+ public List <String > javacArguments () {
193
+ return ImmutableList .of ("-source" , Integer .toString (version ));
194
+ }
195
+
170
196
@ Override
171
197
public String toString () {
172
198
return humanReadableFormat ;
0 commit comments