Skip to content

Commit

Permalink
Start working on event support
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredlll08 committed Jun 16, 2023
1 parent 1aaf8a1 commit 8f0489f
Show file tree
Hide file tree
Showing 18 changed files with 1,093 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.blamejared.crafttweaker.api.event;
import com.blamejared.crafttweaker.api.event.ZenEvent.BusCarrier;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import java.util.ArrayList;
import java.util.List;
import io.toolisticon.aptk.tools.AnnotationUtils;
import io.toolisticon.aptk.tools.TypeMirrorWrapper;
import io.toolisticon.aptk.tools.TypeUtils;


/**
* Wrapper class to read attribute values from Annotation BusCarrier.
*/
public class BusCarrierWrapper {

private final Element annotatedElement;
private final AnnotationMirror annotationMirror;

/**
* Private constructor.
* Used to read annotation from Element.
* @param annotatedElement the annotated Element to annotated with this wrapper annotation
*/
private BusCarrierWrapper (Element annotatedElement) {
this.annotatedElement = annotatedElement;
this.annotationMirror = AnnotationUtils.getAnnotationMirror(annotatedElement, BusCarrier.class);
}

/**
* Private constructor.
* Mainly used for embedded annotations.
* @param element the element related with the passed annotationMirror
* @param annotationMirror the AnnotationMirror to wrap
*/
private BusCarrierWrapper (Element element, AnnotationMirror annotationMirror) {
this.annotatedElement = element;
this.annotationMirror = annotationMirror;
}

/**
* Gets the element on which the wrapped annotation is used.
*/
public Element _annotatedElement() {
return this.annotatedElement;
}

/**
* Gets the wrapped AnnotationMirror.
*/
public AnnotationMirror _annotationMirror() {
return this.annotationMirror;
}



/**
* Checks if passed element is annotated with this wrapper annotation type : BusCarrier
* @param element The element to check for wrapped annotation type
* @return true, if passed element is annotated with BusCarrier annotation, otherwise false
*/
public static boolean isAnnotated(Element element) {
return element != null && element.getAnnotation(BusCarrier.class) != null;
}

/**
* Gets the AnnotationMirror from passed element for this wrappers annotation type and creates a wrapper instance.
* @param element The element to read the annotations from
* @return The wrapped AnnotationMirror if Element is annotated with this wrappers annotation type, otherwise null.
*/
public static BusCarrierWrapper wrap(Element element) {
return isAnnotated(element) ? new BusCarrierWrapper(element) : null;
}

/**
* Wraps an AnnotationMirror.
* Throws an IllegalArgumentException if passed AnnotationMirror type doesn't match the wrapped annotation type.
* @param annotationMirror The element annotated with the annotation to wrap
* @return The wrapper instance
*/
public static BusCarrierWrapper wrap(AnnotationMirror annotationMirror) {
return new BusCarrierWrapper(null, annotationMirror);
}

/**
* Wraps an AnnotationMirror.
* Throws an IllegalArgumentException if passed AnnotationMirror type doesn't match the wrapped annotation type.
* @param element the element bound to the usage of passed AnnotationMirror
* @param annotationMirror The AnnotationMirror to wrap
* @return The wrapper instance
*/
public static BusCarrierWrapper wrap(Element element, AnnotationMirror annotationMirror) {
return new BusCarrierWrapper(element, annotationMirror);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.blamejared.crafttweaker.api.event;
import com.blamejared.crafttweaker.api.event.ZenEvent.Bus;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import java.util.ArrayList;
import java.util.List;
import io.toolisticon.aptk.tools.AnnotationUtils;
import io.toolisticon.aptk.tools.TypeMirrorWrapper;
import io.toolisticon.aptk.tools.TypeUtils;


/**
* Wrapper class to read attribute values from Annotation Bus.
*/
public class BusWrapper {

private final Element annotatedElement;
private final AnnotationMirror annotationMirror;

/**
* Private constructor.
* Used to read annotation from Element.
* @param annotatedElement the annotated Element to annotated with this wrapper annotation
*/
private BusWrapper (Element annotatedElement) {
this.annotatedElement = annotatedElement;
this.annotationMirror = AnnotationUtils.getAnnotationMirror(annotatedElement, Bus.class);
}

/**
* Private constructor.
* Mainly used for embedded annotations.
* @param element the element related with the passed annotationMirror
* @param annotationMirror the AnnotationMirror to wrap
*/
private BusWrapper (Element element, AnnotationMirror annotationMirror) {
this.annotatedElement = element;
this.annotationMirror = annotationMirror;
}

/**
* Gets the element on which the wrapped annotation is used.
*/
public Element _annotatedElement() {
return this.annotatedElement;
}

/**
* Gets the wrapped AnnotationMirror.
*/
public AnnotationMirror _annotationMirror() {
return this.annotationMirror;
}

/**
* Gets the Bus.value from wrapped annotation.
* @return the attribute value as a TypeMirror
*/
public TypeMirror valueAsTypeMirror() {
return (TypeMirror)AnnotationUtils.getAnnotationValueOfAttributeWithDefaults(annotationMirror, "value").getValue();
}

/**
* Gets the Bus.value from wrapped annotation.
* @return the attribute value as a TypeMirror
*/
public TypeMirrorWrapper valueAsTypeMirrorWrapper() {
return TypeMirrorWrapper.wrap((TypeMirror)AnnotationUtils.getAnnotationValueOfAttributeWithDefaults(annotationMirror, "value").getValue());
}

/**
* Gets the Bus.value from wrapped annotation.
* @return the attribute value as a fqn
*/
public String valueAsFqn() {
return TypeUtils.TypeConversion.convertToFqn(valueAsTypeMirror());
}


/**
* Allows to check if attribute was explicitly set or if default value is used.
* @return true, if default value is used, otherwise false
*/
public boolean valueIsDefaultValue(){
return AnnotationUtils.getAnnotationValueOfAttribute(annotationMirror,"value") == null;
}



/**
* Checks if passed element is annotated with this wrapper annotation type : Bus
* @param element The element to check for wrapped annotation type
* @return true, if passed element is annotated with Bus annotation, otherwise false
*/
public static boolean isAnnotated(Element element) {
return element != null && element.getAnnotation(Bus.class) != null;
}

/**
* Gets the AnnotationMirror from passed element for this wrappers annotation type and creates a wrapper instance.
* @param element The element to read the annotations from
* @return The wrapped AnnotationMirror if Element is annotated with this wrappers annotation type, otherwise null.
*/
public static BusWrapper wrap(Element element) {
return isAnnotated(element) ? new BusWrapper(element) : null;
}

/**
* Wraps an AnnotationMirror.
* Throws an IllegalArgumentException if passed AnnotationMirror type doesn't match the wrapped annotation type.
* @param annotationMirror The element annotated with the annotation to wrap
* @return The wrapper instance
*/
public static BusWrapper wrap(AnnotationMirror annotationMirror) {
return new BusWrapper(null, annotationMirror);
}

/**
* Wraps an AnnotationMirror.
* Throws an IllegalArgumentException if passed AnnotationMirror type doesn't match the wrapped annotation type.
* @param element the element bound to the usage of passed AnnotationMirror
* @param annotationMirror The AnnotationMirror to wrap
* @return The wrapper instance
*/
public static BusWrapper wrap(Element element, AnnotationMirror annotationMirror) {
return new BusWrapper(element, annotationMirror);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.blamejared.crafttweaker.api.event;
import com.blamejared.crafttweaker.api.event.ZenEvent;

import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.AnnotationValue;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.PackageElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.ArrayType;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import java.util.ArrayList;
import java.util.List;
import io.toolisticon.aptk.tools.AnnotationUtils;
import io.toolisticon.aptk.tools.TypeMirrorWrapper;
import io.toolisticon.aptk.tools.TypeUtils;


/**
* Wrapper class to read attribute values from Annotation ZenEvent.
*/
public class ZenEventWrapper {

private final Element annotatedElement;
private final AnnotationMirror annotationMirror;

/**
* Private constructor.
* Used to read annotation from Element.
* @param annotatedElement the annotated Element to annotated with this wrapper annotation
*/
private ZenEventWrapper (Element annotatedElement) {
this.annotatedElement = annotatedElement;
this.annotationMirror = AnnotationUtils.getAnnotationMirror(annotatedElement, ZenEvent.class);
}

/**
* Private constructor.
* Mainly used for embedded annotations.
* @param element the element related with the passed annotationMirror
* @param annotationMirror the AnnotationMirror to wrap
*/
private ZenEventWrapper (Element element, AnnotationMirror annotationMirror) {
this.annotatedElement = element;
this.annotationMirror = annotationMirror;
}

/**
* Gets the element on which the wrapped annotation is used.
*/
public Element _annotatedElement() {
return this.annotatedElement;
}

/**
* Gets the wrapped AnnotationMirror.
*/
public AnnotationMirror _annotationMirror() {
return this.annotationMirror;
}



/**
* Checks if passed element is annotated with this wrapper annotation type : ZenEvent
* @param element The element to check for wrapped annotation type
* @return true, if passed element is annotated with ZenEvent annotation, otherwise false
*/
public static boolean isAnnotated(Element element) {
return element != null && element.getAnnotation(ZenEvent.class) != null;
}

/**
* Gets the AnnotationMirror from passed element for this wrappers annotation type and creates a wrapper instance.
* @param element The element to read the annotations from
* @return The wrapped AnnotationMirror if Element is annotated with this wrappers annotation type, otherwise null.
*/
public static ZenEventWrapper wrap(Element element) {
return isAnnotated(element) ? new ZenEventWrapper(element) : null;
}

/**
* Wraps an AnnotationMirror.
* Throws an IllegalArgumentException if passed AnnotationMirror type doesn't match the wrapped annotation type.
* @param annotationMirror The element annotated with the annotation to wrap
* @return The wrapper instance
*/
public static ZenEventWrapper wrap(AnnotationMirror annotationMirror) {
return new ZenEventWrapper(null, annotationMirror);
}

/**
* Wraps an AnnotationMirror.
* Throws an IllegalArgumentException if passed AnnotationMirror type doesn't match the wrapped annotation type.
* @param element the element bound to the usage of passed AnnotationMirror
* @param annotationMirror The AnnotationMirror to wrap
* @return The wrapper instance
*/
public static ZenEventWrapper wrap(Element element, AnnotationMirror annotationMirror) {
return new ZenEventWrapper(element, annotationMirror);
}

}
Loading

0 comments on commit 8f0489f

Please sign in to comment.