Skip to content

Commit 26fe065

Browse files
committed
cleanup
1 parent 16de82d commit 26fe065

File tree

8 files changed

+169
-178
lines changed

8 files changed

+169
-178
lines changed

core/src/main/java/net/jbock/compiler/Constants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
final class Constants {
1111

12+
static final String JAVA_LANG_STRING = "java.lang.String";
13+
1214
static final ClassName STRING = ClassName.get(String.class);
1315

1416
static final ParameterizedTypeName OPTIONAL_STRING =

core/src/main/java/net/jbock/compiler/Context.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,4 @@ private static ClassName peer(ClassName type, String suffix) {
6868
String name = String.join("_", type.simpleNames()) + suffix;
6969
return type.topLevelClassName().peerClass(name);
7070
}
71-
72-
public TypeName returnType() {
73-
return TypeName.get(sourceType.asType());
74-
}
7571
}

core/src/main/java/net/jbock/compiler/Option.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import net.jbock.com.squareup.javapoet.TypeSpec;
3030

3131
/**
32-
* Defines the *_Parser.Option inner class.
32+
* Defines the *_Parser.Option enum.
3333
*
3434
* @see Parser
3535
*/

core/src/main/java/net/jbock/compiler/Param.java

Lines changed: 62 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package net.jbock.compiler;
22

3-
import static net.jbock.compiler.Util.AS_TYPE_ELEMENT;
3+
import static net.jbock.compiler.Constants.JAVA_LANG_STRING;
4+
import static net.jbock.compiler.Util.asDeclared;
5+
import static net.jbock.compiler.Util.asType;
46
import static net.jbock.compiler.Util.equalsType;
57

68
import java.util.Objects;
@@ -45,14 +47,14 @@ private Param(
4547
this.optionType = getOptionType(sourceMethod);
4648
}
4749

48-
private static Type getOptionType(ExecutableElement executableElement) {
49-
if (executableElement.getAnnotation(OtherTokens.class) != null) {
50+
private static Type getOptionType(ExecutableElement sourceMethod) {
51+
if (sourceMethod.getAnnotation(OtherTokens.class) != null) {
5052
return Type.OTHER_TOKENS;
5153
}
52-
if (executableElement.getAnnotation(EverythingAfter.class) != null) {
54+
if (sourceMethod.getAnnotation(EverythingAfter.class) != null) {
5355
return Type.EVERYTHING_AFTER;
5456
}
55-
TypeMirror type = executableElement.getReturnType();
57+
TypeMirror type = sourceMethod.getReturnType();
5658
if (type.getKind() == TypeKind.BOOLEAN) {
5759
return Type.FLAG;
5860
}
@@ -66,55 +68,64 @@ private static Type getOptionType(ExecutableElement executableElement) {
6668
return Type.REQUIRED;
6769
}
6870
String message = "Only String, Optional<String>, List<String> and boolean allowed, " +
69-
String.format("but %s() returns %s", executableElement.getSimpleName(), type);
70-
throw new ValidationException(message, executableElement);
71+
String.format("but %s() returns %s", sourceMethod.getSimpleName(), type);
72+
throw new ValidationException(sourceMethod, message);
7173
}
7274

73-
static Param create(ExecutableElement parameter) {
74-
CreateHelper createHelper = new CreateHelper(parameter);
75-
if (parameter.getAnnotation(OtherTokens.class) != null) {
75+
static Param create(ExecutableElement sourceMethod) {
76+
CreateHelper createHelper = new CreateHelper(sourceMethod);
77+
OtherTokens otherTokens = sourceMethod.getAnnotation(OtherTokens.class);
78+
EverythingAfter everythingAfter = sourceMethod.getAnnotation(EverythingAfter.class);
79+
if (otherTokens != null && everythingAfter != null) {
80+
throw new ValidationException(sourceMethod,
81+
"OtherTokens and EverythingAfter cannot be combined");
82+
}
83+
84+
if (otherTokens != null) {
7685
return createHelper.createOtherTokens();
7786
}
78-
if (parameter.getAnnotation(EverythingAfter.class) != null) {
87+
if (everythingAfter != null) {
7988
return createHelper.createEverythingAfter();
8089
}
81-
String longName = longName(parameter);
82-
String shortName = shortName(parameter);
90+
String longName = longName(sourceMethod);
91+
String shortName = shortName(sourceMethod);
8392
if (shortName == null && longName == null) {
84-
throw new ValidationException("Neither long nor short name defined", parameter);
93+
throw new ValidationException(sourceMethod,
94+
"Neither long nor short name defined");
8595
}
8696
createHelper.checkName(shortName);
8797
createHelper.checkName(longName);
8898
return new Param(
8999
shortName,
90100
longName,
91101
null,
92-
parameter);
102+
sourceMethod);
93103
}
94104

95-
private static String shortName(ExecutableElement parameter) {
96-
ShortName shortName = parameter.getAnnotation(ShortName.class);
105+
private static String shortName(ExecutableElement sourceMethod) {
106+
ShortName shortName = sourceMethod.getAnnotation(ShortName.class);
97107
return shortName != null ? Character.toString(shortName.value()) : null;
98108
}
99109

100-
private static String longName(ExecutableElement parameter) {
101-
LongName longName = parameter.getAnnotation(LongName.class);
102-
if (parameter.getAnnotation(SuppressLongName.class) != null) {
110+
private static String longName(ExecutableElement sourceMethod) {
111+
LongName longName = sourceMethod.getAnnotation(LongName.class);
112+
if (sourceMethod.getAnnotation(SuppressLongName.class) != null) {
103113
if (longName != null) {
104-
throw new ValidationException("LongName and SuppressLongName cannot be combined",
105-
parameter);
114+
throw new ValidationException(sourceMethod,
115+
"LongName and SuppressLongName cannot be combined");
106116
}
107117
return null;
108118
}
109119
if (longName == null) {
110-
return parameter.getSimpleName().toString();
120+
return sourceMethod.getSimpleName().toString();
111121
}
112122
return longName.value();
113123
}
114124

115-
private static void checkList(ExecutableElement variableElement) {
116-
if (!isListOfString(variableElement.getReturnType())) {
117-
throw new ValidationException("Must be a List<String>", variableElement);
125+
private static void checkList(ExecutableElement sourceMethod) {
126+
if (!isListOfString(sourceMethod.getReturnType())) {
127+
throw new ValidationException(sourceMethod,
128+
"Must be a List<String>");
118129
}
119130
}
120131

@@ -127,32 +138,34 @@ private static boolean isOptionalString(TypeMirror type) {
127138
}
128139

129140
private static boolean isXOfString(
130-
TypeMirror type, String x) {
131-
DeclaredType declared = type.accept(Util.AS_DECLARED, null);
141+
TypeMirror type,
142+
String x) {
143+
DeclaredType declared = asDeclared(type);
132144
if (declared == null) {
133145
return false;
134146
}
135147
if (declared.getTypeArguments().size() != 1) {
136148
return false;
137149
}
138-
TypeElement element = declared.asElement().accept(AS_TYPE_ELEMENT, null);
150+
TypeElement typeElement = asType(declared.asElement());
139151
return x.equals(
140-
element.getQualifiedName().toString()) &&
152+
typeElement.getQualifiedName().toString()) &&
141153
equalsType(declared.getTypeArguments().get(0),
142-
"java.lang.String");
154+
JAVA_LANG_STRING);
143155
}
144156

145157
private static boolean isString(
146158
TypeMirror type) {
147-
DeclaredType declared = type.accept(Util.AS_DECLARED, null);
159+
DeclaredType declared = asDeclared(type);
148160
if (declared == null) {
149161
return false;
150162
}
151163
if (!declared.getTypeArguments().isEmpty()) {
152164
return false;
153165
}
154-
TypeElement element = declared.asElement().accept(AS_TYPE_ELEMENT, null);
155-
return "java.lang.String".equals(element.getQualifiedName().toString());
166+
TypeElement element = asType(declared.asElement());
167+
return JAVA_LANG_STRING.equals(
168+
element.getQualifiedName().toString());
156169
}
157170

158171
private static final class CreateHelper {
@@ -167,71 +180,46 @@ private void checkName(String name) {
167180
return;
168181
}
169182
basicCheckName(name);
170-
if (name.startsWith("-")) {
171-
throw new ValidationException("The name may not start with '-'", sourceMethod);
183+
if (name.indexOf(0) == '-') {
184+
throw new ValidationException(sourceMethod,
185+
"The name may not start with '-'");
172186
}
173187
if (name.indexOf('=') >= 0) {
174-
throw new ValidationException("The name may not contain '='", sourceMethod);
188+
throw new ValidationException(sourceMethod,
189+
"The name may not contain '='");
175190
}
176191
}
177192

178193
private void basicCheckName(String name) {
179194
if (name == null) {
180-
throw new ValidationException("The name may not be null", sourceMethod);
195+
throw new ValidationException(sourceMethod,
196+
"The name may not be null");
181197
}
182198
if (name.isEmpty()) {
183-
throw new ValidationException("The name may not be empty", sourceMethod);
199+
throw new ValidationException(sourceMethod,
200+
"The name may not be empty");
184201
}
185202
if (WHITE_SPACE.matcher(name).matches()) {
186-
throw new ValidationException("The name may not contain whitespace characters", sourceMethod);
203+
throw new ValidationException(sourceMethod,
204+
"The name may not contain whitespace characters");
187205
}
188206
}
189207

190208
private Param createEverythingAfter() {
191209
checkList(sourceMethod);
192-
if (sourceMethod.getAnnotation(OtherTokens.class) != null) {
193-
throw new ValidationException(
194-
"EverythingAfter and OtherTokens cannot be combined", sourceMethod);
195-
}
196-
if (sourceMethod.getAnnotation(SuppressLongName.class) != null) {
197-
throw new ValidationException("EverythingAfter and SuppressLongName cannot be combined",
198-
sourceMethod);
199-
}
200-
if (sourceMethod.getAnnotation(LongName.class) != null) {
201-
throw new ValidationException(
202-
"EverythingAfter and LongName cannot be combined", sourceMethod);
203-
}
204-
if (sourceMethod.getAnnotation(ShortName.class) != null) {
205-
throw new ValidationException(
206-
"EverythingAfter and ShortName cannot be combined", sourceMethod);
207-
}
208210
String stopword = sourceMethod.getAnnotation(EverythingAfter.class).value();
209211
basicCheckName(stopword);
210-
return new Param(null,
212+
return new Param(
213+
null,
211214
null,
212215
stopword,
213216
sourceMethod);
214217
}
215218

216219
private Param createOtherTokens() {
217220
checkList(sourceMethod);
218-
if (sourceMethod.getAnnotation(EverythingAfter.class) != null) {
219-
throw new ValidationException(
220-
"OtherTokens and EverythingAfter cannot be combined", sourceMethod);
221-
}
222-
if (sourceMethod.getAnnotation(SuppressLongName.class) != null) {
223-
throw new ValidationException("EverythingAfter and SuppressLongName cannot be combined",
224-
sourceMethod);
225-
}
226-
if (sourceMethod.getAnnotation(LongName.class) != null) {
227-
throw new ValidationException(
228-
"OtherTokens and LongName cannot be combined", sourceMethod);
229-
}
230-
if (sourceMethod.getAnnotation(ShortName.class) != null) {
231-
throw new ValidationException(
232-
"OtherTokens and ShortName cannot be combined", sourceMethod);
233-
}
234-
return new Param(null,
221+
return new Param(
222+
null,
235223
null,
236224
null,
237225
sourceMethod);

core/src/main/java/net/jbock/compiler/Parser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ private MethodSpec parseMethod() {
138138
.addParameter(args)
139139
.addCode(builder.build())
140140
.addException(IllegalArgumentException.class)
141-
.returns(context.returnType())
141+
.returns(TypeName.get(context.sourceType.asType()))
142142
.addModifiers(PUBLIC, STATIC)
143143
.build();
144144
}

0 commit comments

Comments
 (0)