Skip to content

Commit 6854877

Browse files
Merge pull request #6 from ChristopheCVB/core/auto_call_actions
Automatically call action methods
2 parents bd8aab3 + 1d4bb45 commit 6854877

File tree

16 files changed

+481
-129
lines changed

16 files changed

+481
-129
lines changed

Annotations/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
def versionMajor = 4
6-
def versionMinor = 0
6+
def versionMinor = 1
77
def versionPatch = 0
88

99
group 'com.github.ChristopheCVB.TouchPortal.Annotations'

AnnotationsProcessor/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
def versionMajor = 4
6-
def versionMinor = 0
6+
def versionMinor = 1
77
def versionPatch = 0
88

99
group 'com.github.ChristopheCVB.TouchPortal.AnnotationsProcessor'

AnnotationsProcessor/src/main/java/com/github/ChristopheCVB/TouchPortal/AnnotationsProcessor/TouchPortalPluginAnnotationProcessor.java

+31-37
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
125125
* @param roundEnv RoundEnvironment
126126
* @param pluginElement Element
127127
* @return Pair<JsonObject, TypeSpec.Builder> pluginPair
128-
* @throws TPTypeException If a used type is not Supported
128+
* @throws GenericHelper.TPTypeException If a used type is not Supported
129129
*/
130-
private Pair<JsonObject, TypeSpec.Builder> processPlugin(RoundEnvironment roundEnv, Element pluginElement) throws TPTypeException {
130+
private Pair<JsonObject, TypeSpec.Builder> processPlugin(RoundEnvironment roundEnv, Element pluginElement) throws GenericHelper.TPTypeException {
131131
this.messager.printMessage(Diagnostic.Kind.NOTE, "Process Plugin: " + pluginElement.getSimpleName());
132132
Plugin plugin = pluginElement.getAnnotation(Plugin.class);
133133

@@ -164,9 +164,9 @@ private Pair<JsonObject, TypeSpec.Builder> processPlugin(RoundEnvironment roundE
164164
* @param plugin {@link Plugin}
165165
* @param categoryElement Element
166166
* @return Pair<JsonObject, TypeSpec.Builder> categoryPair
167-
* @throws TPTypeException If a used type is not Supported
167+
* @throws GenericHelper.TPTypeException If a used type is not Supported
168168
*/
169-
private Pair<JsonObject, TypeSpec.Builder> processCategory(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement) throws TPTypeException {
169+
private Pair<JsonObject, TypeSpec.Builder> processCategory(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement) throws GenericHelper.TPTypeException {
170170
this.messager.printMessage(Diagnostic.Kind.NOTE, "Process Category: " + categoryElement.getSimpleName());
171171
Category category = categoryElement.getAnnotation(Category.class);
172172

@@ -228,7 +228,7 @@ private Pair<JsonObject, TypeSpec.Builder> processCategory(RoundEnvironment roun
228228
* @param actionElement Element
229229
* @return Pair<JsonObject, TypeSpec.Builder> actionPair
230230
*/
231-
private Pair<JsonObject, TypeSpec.Builder> processAction(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement, Category category, Element actionElement) {
231+
private Pair<JsonObject, TypeSpec.Builder> processAction(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement, Category category, Element actionElement) throws GenericHelper.TPTypeException {
232232
this.messager.printMessage(Diagnostic.Kind.NOTE, "Process Action: " + actionElement.getSimpleName());
233233
Action action = actionElement.getAnnotation(Action.class);
234234

@@ -270,29 +270,31 @@ private Pair<JsonObject, TypeSpec.Builder> processAction(RoundEnvironment roundE
270270
* @param category {@link Category}
271271
* @param stateElement Element
272272
* @return Pair<JsonObject, TypeSpec.Builder> statePair
273-
* @throws TPTypeException If a used type is not Supported
273+
* @throws GenericHelper.TPTypeException If a used type is not Supported
274274
*/
275-
private Pair<JsonObject, TypeSpec.Builder> processState(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement, Category category, Element stateElement) throws TPTypeException {
275+
private Pair<JsonObject, TypeSpec.Builder> processState(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement, Category category, Element stateElement) throws GenericHelper.TPTypeException {
276276
this.messager.printMessage(Diagnostic.Kind.NOTE, "Process State: " + stateElement.getSimpleName());
277277
State state = stateElement.getAnnotation(State.class);
278278

279279
TypeSpec.Builder stateTypeSpecBuilder = this.createStateTypeSpecBuilder(pluginElement, categoryElement, category, stateElement, state);
280280

281+
String className = stateElement.getEnclosingElement().getSimpleName() + "." + stateElement.getSimpleName();
282+
281283
JsonObject jsonState = new JsonObject();
282284
jsonState.addProperty(StateHelper.ID, StateHelper.getStateId(pluginElement, categoryElement, category, stateElement, state));
283-
String tpType = GenericHelper.getTouchPortalType(stateElement);
284-
jsonState.addProperty(StateHelper.TYPE, tpType);
285+
String desiredTPType = GenericHelper.getTouchPortalType(className, stateElement);
286+
jsonState.addProperty(StateHelper.TYPE, desiredTPType);
285287
jsonState.addProperty(StateHelper.DESC, StateHelper.getStateDesc(stateElement, state));
286288
jsonState.addProperty(StateHelper.DEFAULT, state.defaultValue());
287-
if (tpType.equals(StateHelper.TYPE_CHOICE)) {
289+
if (desiredTPType.equals(StateHelper.TYPE_CHOICE)) {
288290
JsonArray stateValueChoices = new JsonArray();
289291
for (String valueChoice : state.valueChoices()) {
290292
stateValueChoices.add(new JsonPrimitive(valueChoice));
291293
}
292294
jsonState.add(StateHelper.VALUE_CHOICES, stateValueChoices);
293295
}
294-
else if (!tpType.equals(StateHelper.TYPE_TEXT)) {
295-
throw new TPTypeException(stateElement, "The type '" + tpType + "' is not supported for states, only '" + StateHelper.TYPE_CHOICE + "' and '" + StateHelper.TYPE_TEXT + "' are.");
296+
else if (!desiredTPType.equals(StateHelper.TYPE_TEXT)) {
297+
throw new GenericHelper.TPTypeException.Builder(className, GenericHelper.TPTypeException.ForAnnotation.STATE, desiredTPType).build();
296298
}
297299

298300
return Pair.create(jsonState, stateTypeSpecBuilder);
@@ -308,23 +310,25 @@ else if (!tpType.equals(StateHelper.TYPE_TEXT)) {
308310
* @param category {@link Category}
309311
* @param eventElement Element
310312
* @return Pair<JsonObject, TypeSpec.Builder> eventPair
311-
* @throws TPTypeException If any used type is not Supported
313+
* @throws GenericHelper.TPTypeException If any used type is not Supported
312314
*/
313-
private Pair<JsonObject, TypeSpec.Builder> processEvent(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement, Category category, Element eventElement) throws TPTypeException {
315+
private Pair<JsonObject, TypeSpec.Builder> processEvent(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement, Category category, Element eventElement) throws GenericHelper.TPTypeException {
314316
this.messager.printMessage(Diagnostic.Kind.NOTE, "Process Event: " + eventElement.getSimpleName());
315317
State state = eventElement.getAnnotation(State.class);
316318
Event event = eventElement.getAnnotation(Event.class);
317319

318320
TypeSpec.Builder eventTypeSpecBuilder = this.createEventTypeSpecBuilder(pluginElement, categoryElement, category, eventElement, event);
319321

322+
String reference = eventElement.getEnclosingElement().getSimpleName() + "." + eventElement.getSimpleName();
323+
320324
JsonObject jsonEvent = new JsonObject();
321325
jsonEvent.addProperty(EventHelper.ID, EventHelper.getEventId(pluginElement, categoryElement, category, eventElement, event));
322326
jsonEvent.addProperty(EventHelper.TYPE, EventHelper.TYPE_COMMUNICATE);
323327
jsonEvent.addProperty(EventHelper.NAME, EventHelper.getEventName(eventElement, event));
324328
jsonEvent.addProperty(EventHelper.FORMAT, event.format());
325-
String tpType = GenericHelper.getTouchPortalType(eventElement);
326-
jsonEvent.addProperty(EventHelper.VALUE_TYPE, tpType);
327-
if (tpType.equals(EventHelper.VALUE_TYPE_CHOICE)) {
329+
String desiredTPType = GenericHelper.getTouchPortalType(reference, eventElement);
330+
jsonEvent.addProperty(EventHelper.VALUE_TYPE, desiredTPType);
331+
if (desiredTPType.equals(EventHelper.VALUE_TYPE_CHOICE)) {
328332
JsonArray stateValueChoices = new JsonArray();
329333
for (String valueChoice : state.valueChoices()) {
330334
stateValueChoices.add(new JsonPrimitive(valueChoice));
@@ -333,7 +337,7 @@ private Pair<JsonObject, TypeSpec.Builder> processEvent(RoundEnvironment roundEn
333337
jsonEvent.addProperty(EventHelper.VALUE_STATE_ID, StateHelper.getStateId(pluginElement, categoryElement, category, eventElement, state));
334338
}
335339
else {
336-
throw new TPTypeException(eventElement, "The type '" + tpType + "' is not supported for events, only '" + EventHelper.VALUE_TYPE_CHOICE + "' is.");
340+
throw new GenericHelper.TPTypeException.Builder(reference, GenericHelper.TPTypeException.ForAnnotation.EVENT, desiredTPType).build();
337341
}
338342

339343
return Pair.create(jsonEvent, eventTypeSpecBuilder);
@@ -353,19 +357,22 @@ private Pair<JsonObject, TypeSpec.Builder> processEvent(RoundEnvironment roundEn
353357
* @param dataElement Element
354358
* @return Pair<JsonObject, TypeSpec.Builder> dataPair
355359
*/
356-
private Pair<JsonObject, TypeSpec.Builder> processActionData(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement, Category category, Element actionElement, Action action, JsonObject jsonAction, Element dataElement) {
360+
private Pair<JsonObject, TypeSpec.Builder> processActionData(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement, Category category, Element actionElement, Action action, JsonObject jsonAction, Element dataElement) throws GenericHelper.TPTypeException {
357361
this.messager.printMessage(Diagnostic.Kind.NOTE, "Process Action Data: " + dataElement.getSimpleName());
358362
Data data = dataElement.getAnnotation(Data.class);
359363

360364
TypeSpec.Builder actionDataTypeSpecBuilder = this.createActionDataTypeSpecBuilder(pluginElement, categoryElement, category, actionElement, action, dataElement, data);
361365

366+
Element method = dataElement.getEnclosingElement();
367+
String className = method.getEnclosingElement().getSimpleName() + "." + method.getSimpleName() + "(" + dataElement.getSimpleName() + ")";
368+
362369
JsonObject jsonData = new JsonObject();
363370
String dataId = DataHelper.getActionDataId(pluginElement, categoryElement, category, actionElement, action, dataElement, data);
364371
jsonData.addProperty(DataHelper.ID, dataId);
365-
String tpType = GenericHelper.getTouchPortalType(dataElement);
366-
jsonData.addProperty(DataHelper.TYPE, tpType);
372+
String desiredTPType = GenericHelper.getTouchPortalType(className, dataElement);
373+
jsonData.addProperty(DataHelper.TYPE, desiredTPType);
367374
jsonData.addProperty(DataHelper.LABEL, DataHelper.getActionDataLabel(dataElement, data));
368-
switch (tpType) {
375+
switch (desiredTPType) {
369376
case GenericHelper.TP_TYPE_NUMBER:
370377
double defaultValue = 0;
371378
try {
@@ -383,14 +390,15 @@ private Pair<JsonObject, TypeSpec.Builder> processActionData(RoundEnvironment ro
383390
jsonData.addProperty(DataHelper.DEFAULT, data.defaultValue());
384391
break;
385392
}
386-
if (tpType.equals(DataHelper.TYPE_CHOICE)) {
393+
if (desiredTPType.equals(DataHelper.TYPE_CHOICE)) {
387394
JsonArray dataValueChoices = new JsonArray();
388395
for (String valueChoice : data.valueChoices()) {
389396
dataValueChoices.add(new JsonPrimitive(valueChoice));
390397
}
391398
jsonData.add(DataHelper.VALUE_CHOICES, dataValueChoices);
392399
}
393400
if (!action.format().isEmpty()) {
401+
// Replace wildcards
394402
String rawFormat = jsonAction.get(ActionHelper.FORMAT).getAsString();
395403
jsonAction.addProperty(ActionHelper.FORMAT, rawFormat.replace("{$" + (data.id().isEmpty() ? dataElement.getSimpleName().toString() : data.id()) + "$}", "{$" + dataId + "$}"));
396404
}
@@ -525,18 +533,4 @@ private TypeSpec.Builder createEventTypeSpecBuilder(Element pluginElement, Eleme
525533
private FieldSpec getStaticFinalStringFieldSpec(String fieldName, String value) {
526534
return FieldSpec.builder(String.class, fieldName.toUpperCase()).addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC).initializer("$S", value).build();
527535
}
528-
529-
/**
530-
* Touch Portal Type Exception
531-
*/
532-
private static class TPTypeException extends Exception {
533-
/**
534-
* Constructor
535-
*
536-
* @param message String
537-
*/
538-
public TPTypeException(Element element, String message) {
539-
super(element.getEnclosingElement().getSimpleName() + "." + element.getSimpleName() + ": " + message);
540-
}
541-
}
542536
}

Helpers/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ plugins {
33
}
44

55
def versionMajor = 4
6-
def versionMinor = 0
6+
def versionMinor = 1
77
def versionPatch = 0
88

99
group 'com.github.ChristopheCVB.TouchPortal.Helpers'

Helpers/src/main/java/com/github/ChristopheCVB/TouchPortal/Helpers/GenericHelper.java

+71-15
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,29 @@ public class GenericHelper {
4242
/**
4343
* Retrieve the internal Touch Portal type according to the Java's element type
4444
*
45-
* @param element Element
45+
* @param reference String
46+
* @param element Element
4647
* @return String tpType
4748
*/
48-
public static String getTouchPortalType(Element element) {
49+
public static String getTouchPortalType(String reference, Element element) throws GenericHelper.TPTypeException {
50+
return GenericHelper.getTouchPortalType(reference, element.asType().toString());
51+
}
52+
53+
/**
54+
* Retrieve the internal Touch Portal type according to the Java's type
55+
*
56+
* @param reference String
57+
* @param rawType String
58+
* @return String tpType
59+
*/
60+
public static String getTouchPortalType(String reference, String rawType) throws GenericHelper.TPTypeException {
4961
String tpType;
50-
String elementType = element.asType().toString();
51-
switch (elementType) {
52-
case "byte":
53-
case "char":
62+
switch (rawType) {
5463
case "short":
5564
case "int":
5665
case "long":
5766
case "float":
5867
case "double":
59-
case "java.lang.Byte":
60-
case "java.lang.Char":
6168
case "java.lang.Short":
6269
case "java.lang.Integer":
6370
case "java.lang.Long":
@@ -71,15 +78,64 @@ public static String getTouchPortalType(Element element) {
7178
tpType = GenericHelper.TP_TYPE_SWITCH;
7279
break;
7380

74-
default:
75-
if (elementType.endsWith("[]")) {
76-
tpType = GenericHelper.TP_TYPE_CHOICE;
77-
}
78-
else {
79-
tpType = GenericHelper.TP_TYPE_TEXT;
80-
}
81+
case "java.lang.String":
82+
tpType = GenericHelper.TP_TYPE_TEXT;
83+
break;
84+
85+
case "java.lang.String[]":
86+
tpType = GenericHelper.TP_TYPE_CHOICE;
8187
break;
88+
89+
default:
90+
throw new TPTypeException.Builder(reference, rawType).build();
8291
}
8392
return tpType;
8493
}
94+
95+
/**
96+
* Touch Portal Type Exception
97+
*/
98+
public static class TPTypeException extends Exception {
99+
100+
/**
101+
* Constructor
102+
*
103+
* @param message String
104+
*/
105+
private TPTypeException(String message) {
106+
super(message);
107+
}
108+
109+
public static class Builder {
110+
private String message;
111+
112+
public Builder(String reference, ForAnnotation forAnnotation, String tpType) {
113+
this.message = reference + ": The type '" + tpType + "' is not supported";
114+
if (forAnnotation != null) {
115+
switch (forAnnotation) {
116+
case STATE:
117+
this.message += " for states, only '" + StateHelper.TYPE_CHOICE + "' and '" + StateHelper.TYPE_TEXT + "' are.";
118+
break;
119+
120+
case EVENT:
121+
this.message += " for events, only '" + EventHelper.VALUE_TYPE_CHOICE + "' is.";
122+
break;
123+
}
124+
}
125+
}
126+
127+
public Builder(String reference, String rawType) {
128+
this(reference, null, rawType);
129+
}
130+
131+
public TPTypeException build() {
132+
return new TPTypeException(this.message);
133+
}
134+
}
135+
136+
public enum ForAnnotation {
137+
STATE,
138+
EVENT
139+
}
140+
}
85141
}

0 commit comments

Comments
 (0)