Skip to content

Commit

Permalink
Merge pull request #6 from ChristopheCVB/core/auto_call_actions
Browse files Browse the repository at this point in the history
Automatically call action methods
  • Loading branch information
ChristopheCVB authored Jun 18, 2020
2 parents bd8aab3 + 1d4bb45 commit 6854877
Show file tree
Hide file tree
Showing 16 changed files with 481 additions and 129 deletions.
2 changes: 1 addition & 1 deletion Annotations/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

def versionMajor = 4
def versionMinor = 0
def versionMinor = 1
def versionPatch = 0

group 'com.github.ChristopheCVB.TouchPortal.Annotations'
Expand Down
2 changes: 1 addition & 1 deletion AnnotationsProcessor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

def versionMajor = 4
def versionMinor = 0
def versionMinor = 1
def versionPatch = 0

group 'com.github.ChristopheCVB.TouchPortal.AnnotationsProcessor'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
* @param roundEnv RoundEnvironment
* @param pluginElement Element
* @return Pair<JsonObject, TypeSpec.Builder> pluginPair
* @throws TPTypeException If a used type is not Supported
* @throws GenericHelper.TPTypeException If a used type is not Supported
*/
private Pair<JsonObject, TypeSpec.Builder> processPlugin(RoundEnvironment roundEnv, Element pluginElement) throws TPTypeException {
private Pair<JsonObject, TypeSpec.Builder> processPlugin(RoundEnvironment roundEnv, Element pluginElement) throws GenericHelper.TPTypeException {
this.messager.printMessage(Diagnostic.Kind.NOTE, "Process Plugin: " + pluginElement.getSimpleName());
Plugin plugin = pluginElement.getAnnotation(Plugin.class);

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

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

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

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

String className = stateElement.getEnclosingElement().getSimpleName() + "." + stateElement.getSimpleName();

JsonObject jsonState = new JsonObject();
jsonState.addProperty(StateHelper.ID, StateHelper.getStateId(pluginElement, categoryElement, category, stateElement, state));
String tpType = GenericHelper.getTouchPortalType(stateElement);
jsonState.addProperty(StateHelper.TYPE, tpType);
String desiredTPType = GenericHelper.getTouchPortalType(className, stateElement);
jsonState.addProperty(StateHelper.TYPE, desiredTPType);
jsonState.addProperty(StateHelper.DESC, StateHelper.getStateDesc(stateElement, state));
jsonState.addProperty(StateHelper.DEFAULT, state.defaultValue());
if (tpType.equals(StateHelper.TYPE_CHOICE)) {
if (desiredTPType.equals(StateHelper.TYPE_CHOICE)) {
JsonArray stateValueChoices = new JsonArray();
for (String valueChoice : state.valueChoices()) {
stateValueChoices.add(new JsonPrimitive(valueChoice));
}
jsonState.add(StateHelper.VALUE_CHOICES, stateValueChoices);
}
else if (!tpType.equals(StateHelper.TYPE_TEXT)) {
throw new TPTypeException(stateElement, "The type '" + tpType + "' is not supported for states, only '" + StateHelper.TYPE_CHOICE + "' and '" + StateHelper.TYPE_TEXT + "' are.");
else if (!desiredTPType.equals(StateHelper.TYPE_TEXT)) {
throw new GenericHelper.TPTypeException.Builder(className, GenericHelper.TPTypeException.ForAnnotation.STATE, desiredTPType).build();
}

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

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

String reference = eventElement.getEnclosingElement().getSimpleName() + "." + eventElement.getSimpleName();

JsonObject jsonEvent = new JsonObject();
jsonEvent.addProperty(EventHelper.ID, EventHelper.getEventId(pluginElement, categoryElement, category, eventElement, event));
jsonEvent.addProperty(EventHelper.TYPE, EventHelper.TYPE_COMMUNICATE);
jsonEvent.addProperty(EventHelper.NAME, EventHelper.getEventName(eventElement, event));
jsonEvent.addProperty(EventHelper.FORMAT, event.format());
String tpType = GenericHelper.getTouchPortalType(eventElement);
jsonEvent.addProperty(EventHelper.VALUE_TYPE, tpType);
if (tpType.equals(EventHelper.VALUE_TYPE_CHOICE)) {
String desiredTPType = GenericHelper.getTouchPortalType(reference, eventElement);
jsonEvent.addProperty(EventHelper.VALUE_TYPE, desiredTPType);
if (desiredTPType.equals(EventHelper.VALUE_TYPE_CHOICE)) {
JsonArray stateValueChoices = new JsonArray();
for (String valueChoice : state.valueChoices()) {
stateValueChoices.add(new JsonPrimitive(valueChoice));
Expand All @@ -333,7 +337,7 @@ private Pair<JsonObject, TypeSpec.Builder> processEvent(RoundEnvironment roundEn
jsonEvent.addProperty(EventHelper.VALUE_STATE_ID, StateHelper.getStateId(pluginElement, categoryElement, category, eventElement, state));
}
else {
throw new TPTypeException(eventElement, "The type '" + tpType + "' is not supported for events, only '" + EventHelper.VALUE_TYPE_CHOICE + "' is.");
throw new GenericHelper.TPTypeException.Builder(reference, GenericHelper.TPTypeException.ForAnnotation.EVENT, desiredTPType).build();
}

return Pair.create(jsonEvent, eventTypeSpecBuilder);
Expand All @@ -353,19 +357,22 @@ private Pair<JsonObject, TypeSpec.Builder> processEvent(RoundEnvironment roundEn
* @param dataElement Element
* @return Pair<JsonObject, TypeSpec.Builder> dataPair
*/
private Pair<JsonObject, TypeSpec.Builder> processActionData(RoundEnvironment roundEnv, Element pluginElement, Plugin plugin, Element categoryElement, Category category, Element actionElement, Action action, JsonObject jsonAction, Element dataElement) {
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 {
this.messager.printMessage(Diagnostic.Kind.NOTE, "Process Action Data: " + dataElement.getSimpleName());
Data data = dataElement.getAnnotation(Data.class);

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

Element method = dataElement.getEnclosingElement();
String className = method.getEnclosingElement().getSimpleName() + "." + method.getSimpleName() + "(" + dataElement.getSimpleName() + ")";

JsonObject jsonData = new JsonObject();
String dataId = DataHelper.getActionDataId(pluginElement, categoryElement, category, actionElement, action, dataElement, data);
jsonData.addProperty(DataHelper.ID, dataId);
String tpType = GenericHelper.getTouchPortalType(dataElement);
jsonData.addProperty(DataHelper.TYPE, tpType);
String desiredTPType = GenericHelper.getTouchPortalType(className, dataElement);
jsonData.addProperty(DataHelper.TYPE, desiredTPType);
jsonData.addProperty(DataHelper.LABEL, DataHelper.getActionDataLabel(dataElement, data));
switch (tpType) {
switch (desiredTPType) {
case GenericHelper.TP_TYPE_NUMBER:
double defaultValue = 0;
try {
Expand All @@ -383,14 +390,15 @@ private Pair<JsonObject, TypeSpec.Builder> processActionData(RoundEnvironment ro
jsonData.addProperty(DataHelper.DEFAULT, data.defaultValue());
break;
}
if (tpType.equals(DataHelper.TYPE_CHOICE)) {
if (desiredTPType.equals(DataHelper.TYPE_CHOICE)) {
JsonArray dataValueChoices = new JsonArray();
for (String valueChoice : data.valueChoices()) {
dataValueChoices.add(new JsonPrimitive(valueChoice));
}
jsonData.add(DataHelper.VALUE_CHOICES, dataValueChoices);
}
if (!action.format().isEmpty()) {
// Replace wildcards
String rawFormat = jsonAction.get(ActionHelper.FORMAT).getAsString();
jsonAction.addProperty(ActionHelper.FORMAT, rawFormat.replace("{$" + (data.id().isEmpty() ? dataElement.getSimpleName().toString() : data.id()) + "$}", "{$" + dataId + "$}"));
}
Expand Down Expand Up @@ -525,18 +533,4 @@ private TypeSpec.Builder createEventTypeSpecBuilder(Element pluginElement, Eleme
private FieldSpec getStaticFinalStringFieldSpec(String fieldName, String value) {
return FieldSpec.builder(String.class, fieldName.toUpperCase()).addModifiers(Modifier.PUBLIC, Modifier.FINAL, Modifier.STATIC).initializer("$S", value).build();
}

/**
* Touch Portal Type Exception
*/
private static class TPTypeException extends Exception {
/**
* Constructor
*
* @param message String
*/
public TPTypeException(Element element, String message) {
super(element.getEnclosingElement().getSimpleName() + "." + element.getSimpleName() + ": " + message);
}
}
}
2 changes: 1 addition & 1 deletion Helpers/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

def versionMajor = 4
def versionMinor = 0
def versionMinor = 1
def versionPatch = 0

group 'com.github.ChristopheCVB.TouchPortal.Helpers'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,22 +42,29 @@ public class GenericHelper {
/**
* Retrieve the internal Touch Portal type according to the Java's element type
*
* @param element Element
* @param reference String
* @param element Element
* @return String tpType
*/
public static String getTouchPortalType(Element element) {
public static String getTouchPortalType(String reference, Element element) throws GenericHelper.TPTypeException {
return GenericHelper.getTouchPortalType(reference, element.asType().toString());
}

/**
* Retrieve the internal Touch Portal type according to the Java's type
*
* @param reference String
* @param rawType String
* @return String tpType
*/
public static String getTouchPortalType(String reference, String rawType) throws GenericHelper.TPTypeException {
String tpType;
String elementType = element.asType().toString();
switch (elementType) {
case "byte":
case "char":
switch (rawType) {
case "short":
case "int":
case "long":
case "float":
case "double":
case "java.lang.Byte":
case "java.lang.Char":
case "java.lang.Short":
case "java.lang.Integer":
case "java.lang.Long":
Expand All @@ -71,15 +78,64 @@ public static String getTouchPortalType(Element element) {
tpType = GenericHelper.TP_TYPE_SWITCH;
break;

default:
if (elementType.endsWith("[]")) {
tpType = GenericHelper.TP_TYPE_CHOICE;
}
else {
tpType = GenericHelper.TP_TYPE_TEXT;
}
case "java.lang.String":
tpType = GenericHelper.TP_TYPE_TEXT;
break;

case "java.lang.String[]":
tpType = GenericHelper.TP_TYPE_CHOICE;
break;

default:
throw new TPTypeException.Builder(reference, rawType).build();
}
return tpType;
}

/**
* Touch Portal Type Exception
*/
public static class TPTypeException extends Exception {

/**
* Constructor
*
* @param message String
*/
private TPTypeException(String message) {
super(message);
}

public static class Builder {
private String message;

public Builder(String reference, ForAnnotation forAnnotation, String tpType) {
this.message = reference + ": The type '" + tpType + "' is not supported";
if (forAnnotation != null) {
switch (forAnnotation) {
case STATE:
this.message += " for states, only '" + StateHelper.TYPE_CHOICE + "' and '" + StateHelper.TYPE_TEXT + "' are.";
break;

case EVENT:
this.message += " for events, only '" + EventHelper.VALUE_TYPE_CHOICE + "' is.";
break;
}
}
}

public Builder(String reference, String rawType) {
this(reference, null, rawType);
}

public TPTypeException build() {
return new TPTypeException(this.message);
}
}

public enum ForAnnotation {
STATE,
EVENT
}
}
}
Loading

0 comments on commit 6854877

Please sign in to comment.