From 9e8b9ec79d42411ad18604b2f2615419d18a2b5e Mon Sep 17 00:00:00 2001 From: Senmori Date: Sun, 26 Aug 2018 21:49:20 +1000 Subject: Update plugin-annotations to 1.13 * Add more checks to ensure plugin's are using @Plugin correctly. * Add no-args constructor check. * Break up some checks to get better error messages. * Update error messages to be more informative. * Update Bukkit version to 1.13 * Bump version --- .../java/annotation/PluginAnnotationProcessor.java | 38 ++++++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'src/main/java') diff --git a/src/main/java/org/bukkit/plugin/java/annotation/PluginAnnotationProcessor.java b/src/main/java/org/bukkit/plugin/java/annotation/PluginAnnotationProcessor.java index 2f0f8d7..089e709 100644 --- a/src/main/java/org/bukkit/plugin/java/annotation/PluginAnnotationProcessor.java +++ b/src/main/java/org/bukkit/plugin/java/annotation/PluginAnnotationProcessor.java @@ -30,10 +30,12 @@ import javax.annotation.processing.SupportedAnnotationTypes; import javax.annotation.processing.SupportedSourceVersion; import javax.lang.model.SourceVersion; import javax.lang.model.element.Element; +import javax.lang.model.element.ExecutableElement; import javax.lang.model.element.Modifier; import javax.lang.model.element.PackageElement; import javax.lang.model.element.TypeElement; import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.ElementFilter; import javax.tools.Diagnostic; import javax.tools.FileObject; import javax.tools.StandardLocation; @@ -67,7 +69,7 @@ public class PluginAnnotationProcessor extends AbstractProcessor { return false; } - if ( elements.isEmpty() ) { + if ( elements.isEmpty() ) { // don't raise error because we don't know which run this is return false; } if ( hasMainBeenFound ) { @@ -81,19 +83,32 @@ public class PluginAnnotationProcessor extends AbstractProcessor { if ( mainPluginElement instanceof TypeElement ) { mainPluginType = ( TypeElement ) mainPluginElement; } else { - raiseError( "Element annotated with @Plugin is not a type!", mainPluginElement ); + raiseError( "Main plugin class is not a class", mainPluginElement ); return false; } - if ( !( mainPluginType.getEnclosingElement() instanceof PackageElement ) && !mainPluginType.getModifiers().contains( Modifier.STATIC ) ) { - raiseError( "Element annotated with @Plugin is not top-level or static nested!", mainPluginType ); + if ( !( mainPluginType.getEnclosingElement() instanceof PackageElement ) ) { + raiseError( "Main plugin class is not a top-level class", mainPluginType ); + return false; + } + + if ( mainPluginType.getModifiers().contains( Modifier.STATIC ) ) { + raiseError( "Main plugin class cannot be static nested", mainPluginType ); return false; } if ( !processingEnv.getTypeUtils().isSubtype( mainPluginType.asType(), fromClass( JavaPlugin.class ) ) ) { - raiseError( "Class annotated with @Plugin is not an subclass of JavaPlugin!", mainPluginType ); + raiseError( "Main plugin class is not an subclass of JavaPlugin!", mainPluginType ); + } + + if ( mainPluginType.getModifiers().contains( Modifier.ABSTRACT ) ) { + raiseError( "Main plugin class cannot be abstract", mainPluginType ); + return false; } + // check for no-args constructor + checkForNoArgsConstructor( mainPluginType ); + Map yml = Maps.newLinkedHashMap(); // linked so we can maintain the same output into file for sanity // populate mainName @@ -227,11 +242,20 @@ public class PluginAnnotationProcessor extends AbstractProcessor { } catch ( IOException e ) { throw new RuntimeException( e ); } - - processingEnv.getMessager().printMessage( Diagnostic.Kind.WARNING, "NOTE: You are using org.bukkit.plugin.java.annotation, an experimental API!" ); return true; } + private void checkForNoArgsConstructor(TypeElement mainPluginType) { + ExecutableElement construct = null; + for ( ExecutableElement constructor : ElementFilter.constructorsIn( mainPluginType.getEnclosedElements() ) ) { + if ( constructor.getParameters().isEmpty() ) { + return; + } + construct = constructor; + } + raiseError( "Main plugin class must have a no argument constructor.", construct ); + } + private void raiseError(String message) { this.processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, message ); } -- cgit v1.2.3