mirror of
https://hub.spigotmc.org/stash/scm/spigot/plugin-annotations.git
synced 2025-04-13 09:29:56 +00:00
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
This commit is contained in:
parent
39f216740c
commit
9e8b9ec79d
2 changed files with 33 additions and 9 deletions
4
pom.xml
4
pom.xml
|
@ -10,7 +10,7 @@
|
||||||
|
|
||||||
<groupId>org.spigotmc</groupId>
|
<groupId>org.spigotmc</groupId>
|
||||||
<artifactId>plugin-annotations</artifactId>
|
<artifactId>plugin-annotations</artifactId>
|
||||||
<version>1.2.0-SNAPSHOT</version>
|
<version>1.2.1-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
<name>Plugin Annotations</name>
|
<name>Plugin Annotations</name>
|
||||||
|
@ -54,7 +54,7 @@
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.bukkit</groupId>
|
<groupId>org.bukkit</groupId>
|
||||||
<artifactId>bukkit</artifactId>
|
<artifactId>bukkit</artifactId>
|
||||||
<version>1.12.2-R0.1-SNAPSHOT</version>
|
<version>1.13-R0.1-SNAPSHOT</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<!-- we don't currently have tests
|
<!-- we don't currently have tests
|
||||||
|
|
|
@ -30,10 +30,12 @@ import javax.annotation.processing.SupportedAnnotationTypes;
|
||||||
import javax.annotation.processing.SupportedSourceVersion;
|
import javax.annotation.processing.SupportedSourceVersion;
|
||||||
import javax.lang.model.SourceVersion;
|
import javax.lang.model.SourceVersion;
|
||||||
import javax.lang.model.element.Element;
|
import javax.lang.model.element.Element;
|
||||||
|
import javax.lang.model.element.ExecutableElement;
|
||||||
import javax.lang.model.element.Modifier;
|
import javax.lang.model.element.Modifier;
|
||||||
import javax.lang.model.element.PackageElement;
|
import javax.lang.model.element.PackageElement;
|
||||||
import javax.lang.model.element.TypeElement;
|
import javax.lang.model.element.TypeElement;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
|
import javax.lang.model.util.ElementFilter;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
import javax.tools.FileObject;
|
import javax.tools.FileObject;
|
||||||
import javax.tools.StandardLocation;
|
import javax.tools.StandardLocation;
|
||||||
|
@ -67,7 +69,7 @@ public class PluginAnnotationProcessor extends AbstractProcessor {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( elements.isEmpty() ) {
|
if ( elements.isEmpty() ) { // don't raise error because we don't know which run this is
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if ( hasMainBeenFound ) {
|
if ( hasMainBeenFound ) {
|
||||||
|
@ -81,19 +83,32 @@ public class PluginAnnotationProcessor extends AbstractProcessor {
|
||||||
if ( mainPluginElement instanceof TypeElement ) {
|
if ( mainPluginElement instanceof TypeElement ) {
|
||||||
mainPluginType = ( TypeElement ) mainPluginElement;
|
mainPluginType = ( TypeElement ) mainPluginElement;
|
||||||
} else {
|
} else {
|
||||||
raiseError( "Element annotated with @Plugin is not a type!", mainPluginElement );
|
raiseError( "Main plugin class is not a class", mainPluginElement );
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !( mainPluginType.getEnclosingElement() instanceof PackageElement ) && !mainPluginType.getModifiers().contains( Modifier.STATIC ) ) {
|
if ( !( mainPluginType.getEnclosingElement() instanceof PackageElement ) ) {
|
||||||
raiseError( "Element annotated with @Plugin is not top-level or static nested!", mainPluginType );
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( !processingEnv.getTypeUtils().isSubtype( mainPluginType.asType(), fromClass( JavaPlugin.class ) ) ) {
|
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<String, Object> yml = Maps.newLinkedHashMap(); // linked so we can maintain the same output into file for sanity
|
Map<String, Object> yml = Maps.newLinkedHashMap(); // linked so we can maintain the same output into file for sanity
|
||||||
|
|
||||||
// populate mainName
|
// populate mainName
|
||||||
|
@ -227,11 +242,20 @@ public class PluginAnnotationProcessor extends AbstractProcessor {
|
||||||
} catch ( IOException e ) {
|
} catch ( IOException e ) {
|
||||||
throw new RuntimeException( 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;
|
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) {
|
private void raiseError(String message) {
|
||||||
this.processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, message );
|
this.processingEnv.getMessager().printMessage( Diagnostic.Kind.ERROR, message );
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue