Skip to content

Commit

Permalink
feat: add new annotations for further configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
gabizou committed Sep 29, 2024
1 parent 70a9a0a commit 9213b38
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 5 deletions.
5 changes: 4 additions & 1 deletion annotations/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of Event Implementation Generator, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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 {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* This file is part of Event Implementation Generator, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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 {
}
12 changes: 9 additions & 3 deletions src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
47 changes: 47 additions & 0 deletions test-data/src/main/java/test/event/CompositeEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* This file is part of Event Implementation Generator, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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<E extends CompositeEvent<E>> extends Event {

@ToStringExclude
E baseEvent();

List<Event> children();

@Override
default void setCancelled(final boolean cancelled) {
Event.super.setCancelled(cancelled);
for (var child : this.children()) {
child.setCancelled(cancelled);
}
}
}
4 changes: 3 additions & 1 deletion test-data/src/main/java/test/event/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ default String version() {

boolean cancelled();

void setCancelled(final boolean cancelled);
default void setCancelled(final boolean cancelled) {

}

}
37 changes: 37 additions & 0 deletions test-data/src/main/java/test/event/entity/EntityInteractEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of Event Implementation Generator, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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<Post> {

}
}

0 comments on commit 9213b38

Please sign in to comment.