diff --git a/annotations/src/main/java/module-info.java b/annotations/src/main/java/module-info.java index 4825a50..d36dc82 100644 --- a/annotations/src/main/java/module-info.java +++ b/annotations/src/main/java/module-info.java @@ -1,4 +1,7 @@ module org.spongepowered.eventimplgen.annotations { exports org.spongepowered.eventgen.annotations; - exports org.spongepowered.eventgen.annotations.internal; + + // This way we can still use the internal package, but not + // to external developers. + exports org.spongepowered.eventgen.annotations.internal to org.spongepowered.eventimplgen; } \ No newline at end of file diff --git a/annotations/src/main/java/org/spongepowered/eventgen/annotations/DefaultImplemented.java b/annotations/src/main/java/org/spongepowered/eventgen/annotations/DefaultImplemented.java index e69de29..138de55 100644 --- a/annotations/src/main/java/org/spongepowered/eventgen/annotations/DefaultImplemented.java +++ b/annotations/src/main/java/org/spongepowered/eventgen/annotations/DefaultImplemented.java @@ -0,0 +1,39 @@ +/* + * This file is part of Event Implementation Generator, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.eventgen.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marks a defaulted method as the implementation of the method. This is useful + * for interfaces that have defaulted a method that is intended to be used + */ +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.CLASS) +public @interface DefaultImplemented { +} diff --git a/annotations/src/main/java/org/spongepowered/eventgen/annotations/ToStringExclude.java b/annotations/src/main/java/org/spongepowered/eventgen/annotations/ToStringExclude.java new file mode 100644 index 0000000..b741932 --- /dev/null +++ b/annotations/src/main/java/org/spongepowered/eventgen/annotations/ToStringExclude.java @@ -0,0 +1,39 @@ +/* + * This file is part of Event Implementation Generator, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.eventgen.annotations; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * Marks the annotated field or method as excluded from being used in generating the {@link Object#toString()} + * method, which can come from using self-referencing fields. + */ +@Retention(RetentionPolicy.CLASS) +@Target({ElementType.FIELD, ElementType.METHOD}) +public @interface ToStringExclude { +} diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 3d262af..2c89f3c 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -1,10 +1,16 @@ +@SuppressWarnings("requires-transitive-automatic") module org.spongepowered.eventimplgen { exports org.spongepowered.eventimplgen.processor; + exports org.spongepowered.eventimplgen.factory; + exports org.spongepowered.eventimplgen.factory.plugin; + exports org.spongepowered.eventimplgen.eventgencore; + exports org.spongepowered.eventimplgen.signature; - requires dagger; - requires javax.inject; + + requires transitive dagger; + requires transitive javax.inject; requires jakarta.inject; - requires io.soabase.java.composer; + requires transitive io.soabase.java.composer; requires transitive java.compiler; requires transitive org.spongepowered.eventimplgen.annotations; diff --git a/src/main/java/org/spongepowered/eventimplgen/factory/ClassContext.java b/src/main/java/org/spongepowered/eventimplgen/factory/ClassContext.java index fc38608..7b83068 100644 --- a/src/main/java/org/spongepowered/eventimplgen/factory/ClassContext.java +++ b/src/main/java/org/spongepowered/eventimplgen/factory/ClassContext.java @@ -175,6 +175,7 @@ void contributeToString( if (useField != null) { overrideToString = useField.overrideToString(); } + final var toStringExclusion = ClassGenerator.getToStringExclude(parentType, property.getName()); final Object value; if (overrideToString) { diff --git a/src/main/java/org/spongepowered/eventimplgen/factory/ClassGenerator.java b/src/main/java/org/spongepowered/eventimplgen/factory/ClassGenerator.java index 447f9ea..3b0b578 100644 --- a/src/main/java/org/spongepowered/eventimplgen/factory/ClassGenerator.java +++ b/src/main/java/org/spongepowered/eventimplgen/factory/ClassGenerator.java @@ -35,6 +35,7 @@ import com.squareup.javapoet.TypeVariableName; import org.jetbrains.annotations.Nullable; import org.spongepowered.eventgen.annotations.PropertySettings; +import org.spongepowered.eventgen.annotations.ToStringExclude; import org.spongepowered.eventgen.annotations.UseField; import org.spongepowered.eventgen.annotations.internal.GeneratedEvent; import org.spongepowered.eventimplgen.eventgencore.Property; @@ -149,6 +150,24 @@ static UseField getUseField(final TypeMirror clazz, final String fieldName) { return null; } + static ToStringExclude getToStringExclude(final TypeMirror clazz, final String methodName) { + if (clazz.getKind() != TypeKind.DECLARED) { + return null; + } + final Element type = ((DeclaredType) clazz).asElement(); + if (type == null) { + return null; + } + + for (final ExecutableElement element : ElementFilter.methodsIn(type.getEnclosedElements())) { + if (element.getSimpleName().contentEquals(methodName)) { + return element.getAnnotation(ToStringExclude.class); + } + } + + return null; + } + public boolean hasDeclaredMethod(final DeclaredType clazz, final String name, final TypeMirror... params) { for (final ExecutableElement method : ElementFilter.methodsIn(this.elements.getAllMembers((TypeElement) clazz.asElement()))) { if (method.getSimpleName().contentEquals(name) && this.parametersEqual(method.getParameters(), params)) { diff --git a/src/main/java/org/spongepowered/eventimplgen/processor/EventImplGenProcessor.java b/src/main/java/org/spongepowered/eventimplgen/processor/EventImplGenProcessor.java index 4d67836..0b1473b 100644 --- a/src/main/java/org/spongepowered/eventimplgen/processor/EventImplGenProcessor.java +++ b/src/main/java/org/spongepowered/eventimplgen/processor/EventImplGenProcessor.java @@ -35,6 +35,7 @@ import javax.annotation.processing.ProcessingEnvironment; import javax.annotation.processing.Processor; import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedOptions; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; @@ -54,8 +55,22 @@ EventGenOptions.EXCLUSIVE_ANNOTATIONS, EventGenOptions.DEBUG }) +@SupportedAnnotationTypes({ + "org.spongepowered.eventgen.annotations.AbsoluteSortPosition", + "org.spongepowered.eventgen.annotations.DefaultImplemented", + "org.spongepowered.eventgen.annotations.GenerateFactoryMethod", + "org.spongepowered.eventgen.annotations.NoFactoryMethod", + "org.spongepowered.eventgen.annotations.PropertySettings", + "org.spongepowered.eventgen.annotations.ToStringExclude", + "org.spongepowered.eventgen.annotations.TransformResult", + "org.spongepowered.eventgen.annotations.TransformWith", + "org.spongepowered.eventgen.annotations.UseField", +}) public class EventImplGenProcessor extends AbstractProcessor { + public EventImplGenProcessor() { + } + private EventGenComponent component; static Element topLevelType(final TypeElement element) { diff --git a/test-data/src/main/java/test/event/CompositeEvent.java b/test-data/src/main/java/test/event/CompositeEvent.java index e69de29..4904118 100644 --- a/test-data/src/main/java/test/event/CompositeEvent.java +++ b/test-data/src/main/java/test/event/CompositeEvent.java @@ -0,0 +1,47 @@ +/* + * This file is part of Event Implementation Generator, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package test.event; + +import org.spongepowered.eventgen.annotations.GenerateFactoryMethod; +import org.spongepowered.eventgen.annotations.ToStringExclude; + +import java.util.List; + +@GenerateFactoryMethod +public interface CompositeEvent> extends Event { + + @ToStringExclude + E baseEvent(); + + List children(); + + @Override + default void setCancelled(final boolean cancelled) { + Event.super.setCancelled(cancelled); + for (var child : this.children()) { + child.setCancelled(cancelled); + } + } +} diff --git a/test-data/src/main/java/test/event/Event.java b/test-data/src/main/java/test/event/Event.java index d6cb2a5..d0782ac 100644 --- a/test-data/src/main/java/test/event/Event.java +++ b/test-data/src/main/java/test/event/Event.java @@ -38,6 +38,8 @@ default String version() { boolean cancelled(); - void setCancelled(final boolean cancelled); + default void setCancelled(final boolean cancelled) { + + } } diff --git a/test-data/src/main/java/test/event/entity/EntityInteractEvent.java b/test-data/src/main/java/test/event/entity/EntityInteractEvent.java index e69de29..e1d05ab 100644 --- a/test-data/src/main/java/test/event/entity/EntityInteractEvent.java +++ b/test-data/src/main/java/test/event/entity/EntityInteractEvent.java @@ -0,0 +1,37 @@ +/* + * This file is part of Event Implementation Generator, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package test.event.entity; + +import org.spongepowered.eventgen.annotations.GenerateFactoryMethod; +import test.event.CompositeEvent; +import test.event.Event; + +public interface EntityInteractEvent extends Event { + + @GenerateFactoryMethod + interface Post extends CompositeEvent { + + } +}