Fixes, annotation processor works for player cls

This commit is contained in:
Norbi Peti 2017-08-28 17:02:43 +02:00
parent 59d8cd970e
commit 89fb273714
7 changed files with 59 additions and 11 deletions

View file

@ -7,6 +7,7 @@ import java.util.logging.Logger;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Server; import org.bukkit.Server;
import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.PluginManager;
import org.bukkit.scheduler.BukkitScheduler;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock; import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer; import org.mockito.stubbing.Answer;
@ -29,6 +30,8 @@ public class TestPrepare {
return Mockito.mock(PluginManager.class); return Mockito.mock(PluginManager.class);
if (returns(invocation, Collection.class)) if (returns(invocation, Collection.class))
return Collections.EMPTY_LIST; return Collections.EMPTY_LIST;
if (returns(invocation, BukkitScheduler.class))
return Mockito.mock(BukkitScheduler.class);
return null; return null;
} }

View file

@ -99,8 +99,8 @@ public class Channel {
} }
public static class RecipientTestResult { public static class RecipientTestResult {
public String errormessage; public String errormessage = null;
public int score; public int score = -1; // Anything below 0 is "never send"
/** /**
* Creates a result that indicates an <b>error</b> * Creates a result that indicates an <b>error</b>

View file

@ -14,6 +14,7 @@ import javassist.Modifier;
* @author Norbi * @author Norbi
* *
*/ */
@TBMCCommandEnforcer
public abstract class TBMCCommandBase { public abstract class TBMCCommandBase {
public TBMCCommandBase() { public TBMCCommandBase() {

View file

@ -0,0 +1,14 @@
package buttondevteam.lib.chat;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.TYPE)
@Inherited
public @interface TBMCCommandEnforcer {
}

View file

@ -19,6 +19,7 @@ import com.palmergames.bukkit.towny.object.TownyUniverse;
import buttondevteam.lib.TBMCCoreAPI; import buttondevteam.lib.TBMCCoreAPI;
@AbstractUserClass(foldername = "minecraft", prototype = TBMCPlayer.class) @AbstractUserClass(foldername = "minecraft", prototype = TBMCPlayer.class)
@TBMCPlayerEnforcer
public abstract class TBMCPlayerBase extends ChromaGamerBase { public abstract class TBMCPlayerBase extends ChromaGamerBase {
protected UUID uuid; protected UUID uuid;

View file

@ -0,0 +1,11 @@
package buttondevteam.lib.player;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Target;
@Target(ElementType.TYPE)
@Inherited
public @interface TBMCPlayerEnforcer {
}

View file

@ -2,6 +2,8 @@ package buttondevteam.buttonproc;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.function.Function;
import javax.annotation.processing.AbstractProcessor; import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.RoundEnvironment; import javax.annotation.processing.RoundEnvironment;
import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedAnnotationTypes;
@ -9,21 +11,32 @@ import javax.annotation.processing.SupportedSourceVersion;
import javax.lang.model.SourceVersion; import javax.lang.model.SourceVersion;
import javax.lang.model.element.AnnotationMirror; import javax.lang.model.element.AnnotationMirror;
import javax.lang.model.element.Element; import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;
import javax.tools.Diagnostic.Kind;
/** * A simple session bean type annotation processor. The implementation * is based on the standard annotation processing API in Java 6. */
@SupportedSourceVersion(SourceVersion.RELEASE_8)
@SupportedAnnotationTypes("buttondevteam.*") @SupportedAnnotationTypes("buttondevteam.*")
public class ButtonProcessor extends AbstractProcessor { public class ButtonProcessor extends AbstractProcessor {
/** * Check if both @Stateful and @Stateless are present in an * session bean. If so, emits a warning message. */
@Override @Override
public boolean process(Set<? extends TypeElement> typeElements, RoundEnvironment roundEnv) { // TODO: SEparate JAR public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
for (TypeElement te : typeElements) { for (TypeElement te : annotations) {
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(te); Set<? extends Element> classes = roundEnv.getElementsAnnotatedWith(te);
for (Element element : elements) { for (Element targetcl : classes) {
System.out.println("Processing " + element); System.out.println("Processing " + targetcl);
List<? extends AnnotationMirror> annotationMirrors = element.getAnnotationMirrors(); List<? extends AnnotationMirror> annotationMirrors = processingEnv.getElementUtils()
.getAllAnnotationMirrors(targetcl);
System.out.println("Annotations: " + annotationMirrors); System.out.println("Annotations: " + annotationMirrors);
Function<String, Boolean> hasAnnotation = ann -> annotationMirrors.stream()
.anyMatch(am -> am.getAnnotationType().toString().contains(ann));
if (hasAnnotation.apply("ChromaGamerEnforcer") && !hasAnnotation.apply("UserClass")
&& !targetcl.getModifiers().contains(Modifier.ABSTRACT))
processingEnv.getMessager().printMessage(Kind.ERROR,
"No UserClass annotation found for " + targetcl.getSimpleName(), targetcl);
if (hasAnnotation.apply("TBMCPlayerEnforcer") && !hasAnnotation.apply("PlayerClass")
&& !targetcl.getModifiers().contains(Modifier.ABSTRACT))
processingEnv.getMessager().printMessage(Kind.ERROR,
"No PlayerClass annotation found for " + targetcl.getSimpleName(), targetcl);
for (AnnotationMirror annotation : annotationMirrors) { for (AnnotationMirror annotation : annotationMirrors) {
String type = annotation.getAnnotationType().toString(); String type = annotation.getAnnotationType().toString();
System.out.println("Type: " + type); System.out.println("Type: " + type);
@ -32,4 +45,9 @@ public class ButtonProcessor extends AbstractProcessor {
} }
return true; // claim the annotations return true; // claim the annotations
} }
@Override
public SourceVersion getSupportedSourceVersion() {
return SourceVersion.latestSupported();
}
} }