commit
8b11b67f82
|
@ -1,164 +1,93 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-runtime</artifactId>
|
||||
<version>4.5-SNAPSHOT</version>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-runtime</artifactId>
|
||||
<version>4.5-SNAPSHOT</version>
|
||||
|
||||
<name>ANTLR 4 Runtime</name>
|
||||
<description>The ANTLR 4 Runtime</description>
|
||||
<name>ANTLR 4 Runtime</name>
|
||||
<description>The ANTLR 4 Runtime</description>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Terence Parr</name>
|
||||
<url>http://parrt.cs.usfca.edu</url>
|
||||
<roles>
|
||||
<role>Project lead</role>
|
||||
</roles>
|
||||
</developer>
|
||||
<!--
|
||||
IMPORTANT NOTE
|
||||
Building ANTLR is now performed via the bild.py script.
|
||||
However, we still need a pom to: - manage dependencies during development
|
||||
- publish metadata to maven central The above are the only reason for keeping
|
||||
this file. No support will be provided.
|
||||
-->
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Terence Parr</name>
|
||||
<url>http://parrt.cs.usfca.edu</url>
|
||||
<roles>
|
||||
<role>Project lead</role>
|
||||
</roles>
|
||||
</developer>
|
||||
|
||||
<developer>
|
||||
<name>Sam Harwell</name>
|
||||
<url>http://tunnelvisionlabs.com</url>
|
||||
<roles>
|
||||
<role>Developer - Coauthor of tool, C# target</role>
|
||||
</roles>
|
||||
</developer>
|
||||
<developer>
|
||||
<name>Sam Harwell</name>
|
||||
<url>http://tunnelvisionlabs.com</url>
|
||||
<roles>
|
||||
<role>Developer - Coauthor of tool, C# target</role>
|
||||
</roles>
|
||||
</developer>
|
||||
|
||||
<developer>
|
||||
<name>Eric Vergnaud</name>
|
||||
<roles>
|
||||
<role>Developer - JavaScript, C#, Python 2, Python 3</role>
|
||||
</roles>
|
||||
</developer>
|
||||
<developer>
|
||||
<name>Eric Vergnaud</name>
|
||||
<roles>
|
||||
<role>Developer - JavaScript, C#, Python 2, Python 3</role>
|
||||
</roles>
|
||||
</developer>
|
||||
|
||||
<developer>
|
||||
<name>Jim Idle</name>
|
||||
<email>jimi@idle.ws</email>
|
||||
<url>http://www.linkedin.com/in/jimidle</url>
|
||||
<roles>
|
||||
<role>Developer - Maven Plugin</role>
|
||||
</roles>
|
||||
</developer>
|
||||
</developers>
|
||||
<developer>
|
||||
<name>Jim Idle</name>
|
||||
<email>jimi@idle.ws</email>
|
||||
<url>http://www.linkedin.com/in/jimidle</url>
|
||||
<roles>
|
||||
<role>Developer - Maven Plugin</role>
|
||||
</roles>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<mailingLists>
|
||||
<mailingList>
|
||||
<name>antlr-discussion</name>
|
||||
<archive>https://groups.google.com/forum/?fromgroups#!forum/antlr-discussion</archive>
|
||||
</mailingList>
|
||||
</mailingLists>
|
||||
<mailingLists>
|
||||
<mailingList>
|
||||
<name>antlr-discussion</name>
|
||||
<archive>https://groups.google.com/forum/?fromgroups#!forum/antlr-discussion</archive>
|
||||
</mailingList>
|
||||
</mailingLists>
|
||||
|
||||
<issueManagement>
|
||||
<system>GitHub Issues</system>
|
||||
<url>https://github.com/antlr/antlr4/issues</url>
|
||||
</issueManagement>
|
||||
<issueManagement>
|
||||
<system>GitHub Issues</system>
|
||||
<url>https://github.com/antlr/antlr4/issues</url>
|
||||
</issueManagement>
|
||||
|
||||
<scm>
|
||||
<url>https://github.com/antlr/antlr4/tree/master</url>
|
||||
<connection>scm:git:git://github.com/antlr/antlr4.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:antlr/antlr4.git</developerConnection>
|
||||
<tag>HEAD</tag>
|
||||
</scm>
|
||||
<scm>
|
||||
<url>https://github.com/antlr/antlr4/tree/master</url>
|
||||
<connection>scm:git:git://github.com/antlr/antlr4.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:antlr/antlr4.git</developerConnection>
|
||||
<tag>HEAD</tag>
|
||||
</scm>
|
||||
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
<id>ossrh</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</snapshotRepository>
|
||||
<repository>
|
||||
<id>ossrh</id>
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
<id>ossrh</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
|
||||
</snapshotRepository>
|
||||
<repository>
|
||||
<id>ossrh</id>
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<properties>
|
||||
<!-- Assumes dot is in the system path, or specified for the build. -->
|
||||
<dot.path>dot</dot.path>
|
||||
</properties>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.abego.treelayout</groupId>
|
||||
<artifactId>org.abego.treelayout.core</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>sonatype-oss-release</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>us.bryon</groupId>
|
||||
<artifactId>graphviz-maven-plugin</artifactId>
|
||||
<version>1.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>dot</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<dot>${dot.path}</dot>
|
||||
<destdir>${project.build.directory}/apidocs</destdir>
|
||||
<output>svg</output>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</dependencies>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<configuration>
|
||||
<includeDependencySources>true</includeDependencySources>
|
||||
<dependencySourceIncludes>
|
||||
<dependencySourceInclude>org.antlr:*</dependencySourceInclude>
|
||||
</dependencySourceIncludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<inherited>false</inherited>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<minimizeJar>false</minimizeJar>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||
<createSourcesJar>true</createSourcesJar>
|
||||
<shadedClassifierName>complete</shadedClassifierName>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<build>
|
||||
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<resources />
|
||||
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-maven-plugin</artifactId>
|
||||
<version>4.5-SNAPSHOT</version>
|
||||
<configuration>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>antlr4</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2012 Terence Parr
|
||||
* Copyright (c) 2012 Sam Harwell
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.misc;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* This annotation marks a field, parameter, local variable, or method (return
|
||||
* value) as never being {@code null}. The specific semantics implied by this
|
||||
* annotation depend on the kind of element the annotation is applied to.
|
||||
*
|
||||
* <ul>
|
||||
* <li><strong>Field or Local Variable:</strong> Code reading the field or local
|
||||
* variable may assume that the value is never {@code null}. Code writing to the
|
||||
* field or local variable should ensure that a {@code null} reference is never
|
||||
* written.</li>
|
||||
* <li><strong>Parameter:</strong> Code calling the method should never pass
|
||||
* {@code null} for this parameter. The implementation may assume that the value
|
||||
* is never {@code null}, and the behavior of the method if the parameter is
|
||||
* {@code null} is undefined. Overriding methods may optionally use the
|
||||
* {@link Nullable} annotation instead of this annotation for the parameter,
|
||||
* indicating that the overriding method provides additional code to handle a
|
||||
* {@code null} reference passed for the parameter.</li>
|
||||
* <li><strong>Method (Return Value):</strong> Code calling the method may
|
||||
* assume that the result of the method is never {@code null}. The
|
||||
* implementation of the method should ensure that a {@code null} reference is
|
||||
* never returned.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>
|
||||
* The {@link NullUsageProcessor} annotation processor validates certain usage
|
||||
* scenarios for this annotation, with compile-time errors or warnings reported
|
||||
* for misuse. For detailed information about the supported analysis, see the
|
||||
* documentation for {@link NullUsageProcessor}.</p>
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.CLASS)
|
||||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
|
||||
public @interface NotNull {
|
||||
}
|
|
@ -0,0 +1,332 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2013 Terence Parr
|
||||
* Copyright (c) 2013 Sam Harwell
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.misc;
|
||||
|
||||
import javax.annotation.processing.AbstractProcessor;
|
||||
import javax.annotation.processing.RoundEnvironment;
|
||||
import javax.annotation.processing.SupportedAnnotationTypes;
|
||||
import javax.lang.model.SourceVersion;
|
||||
import javax.lang.model.element.AnnotationMirror;
|
||||
import javax.lang.model.element.Element;
|
||||
import javax.lang.model.element.ElementKind;
|
||||
import javax.lang.model.element.ExecutableElement;
|
||||
import javax.lang.model.element.TypeElement;
|
||||
import javax.lang.model.element.VariableElement;
|
||||
import javax.lang.model.type.NoType;
|
||||
import javax.lang.model.type.PrimitiveType;
|
||||
import javax.lang.model.type.TypeKind;
|
||||
import javax.lang.model.type.TypeMirror;
|
||||
import javax.tools.Diagnostic;
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Deque;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A compile-time validator for correct usage of the {@link NotNull} and
|
||||
* {@link Nullable} annotations.
|
||||
*
|
||||
* <p>The validation process checks the following items.</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li><strong>Error</strong>: an element is annotated with both {@link NotNull} and {@link Nullable}.</li>
|
||||
* <li><strong>Error</strong>: an method which returns {@code void} is annotated with {@link NotNull} or {@link Nullable}.</li>
|
||||
* <li><strong>Error</strong>: an element with a primitive type is annotated with {@link Nullable}.</li>
|
||||
* <li><strong>Error</strong>: a parameter is annotated with {@link NotNull}, but the method overrides or implements a method where the parameter is annotated {@link Nullable}.</li>
|
||||
* <li><strong>Error</strong>: a method is annotated with {@link Nullable}, but the method overrides or implements a method that is annotated with {@link NotNull}.</li>
|
||||
* <li><strong>Warning</strong>: an element with a primitive type is annotated with {@link NotNull}.</li>
|
||||
* <li><strong>Warning</strong>: a parameter is annotated with {@link NotNull}, but the method overrides or implements a method where the parameter is not annotated.</li>
|
||||
* <li><strong>Warning</strong>: a method is annotated with {@link Nullable}, but the method overrides or implements a method that is not annotated.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>In the future, the validation process may be updated to check the following additional items.</p>
|
||||
*
|
||||
* <ul>
|
||||
* <li><strong>Warning</strong>: a parameter is not annotated, but the method overrides or implements a method where the parameter is annotated with {@link NotNull} or {@link Nullable}.</li>
|
||||
* <li><strong>Warning</strong>: a method is not annotated, but the method overrides or implements a method that is annotated with with {@link NotNull} or {@link Nullable}.</li>
|
||||
* </ul>
|
||||
*
|
||||
* @author Sam Harwell
|
||||
*/
|
||||
@SupportedAnnotationTypes({NullUsageProcessor.NotNullClassName, NullUsageProcessor.NullableClassName})
|
||||
public class NullUsageProcessor extends AbstractProcessor {
|
||||
public static final String NotNullClassName = "org.antlr.v4.runtime.misc.NotNull";
|
||||
public static final String NullableClassName = "org.antlr.v4.runtime.misc.Nullable";
|
||||
|
||||
private TypeElement notNullType;
|
||||
private TypeElement nullableType;
|
||||
|
||||
public NullUsageProcessor() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public SourceVersion getSupportedSourceVersion() {
|
||||
SourceVersion latestSupported = SourceVersion.latestSupported();
|
||||
|
||||
if (latestSupported.ordinal() <= 6) {
|
||||
return SourceVersion.RELEASE_6;
|
||||
}
|
||||
else if (latestSupported.ordinal() <= 8) {
|
||||
return latestSupported;
|
||||
}
|
||||
else {
|
||||
// this annotation processor is tested through Java 8
|
||||
return SourceVersion.values()[8];
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||
if (!checkClassNameConstants()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
notNullType = processingEnv.getElementUtils().getTypeElement(NotNullClassName);
|
||||
nullableType = processingEnv.getElementUtils().getTypeElement(NullableClassName);
|
||||
Set<? extends Element> notNullElements = roundEnv.getElementsAnnotatedWith(notNullType);
|
||||
Set<? extends Element> nullableElements = roundEnv.getElementsAnnotatedWith(nullableType);
|
||||
|
||||
Set<Element> intersection = new HashSet<Element>(notNullElements);
|
||||
intersection.retainAll(nullableElements);
|
||||
for (Element element : intersection) {
|
||||
String error = String.format("%s cannot be annotated with both %s and %s", element.getKind().toString().replace('_', ' ').toLowerCase(), notNullType.getSimpleName(), nullableType.getSimpleName());
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error, element);
|
||||
}
|
||||
|
||||
checkVoidMethodAnnotations(notNullElements, notNullType);
|
||||
checkVoidMethodAnnotations(nullableElements, nullableType);
|
||||
|
||||
checkPrimitiveTypeAnnotations(nullableElements, Diagnostic.Kind.ERROR, nullableType);
|
||||
checkPrimitiveTypeAnnotations(notNullElements, Diagnostic.Kind.WARNING, notNullType);
|
||||
|
||||
// method name -> method -> annotated elements of method
|
||||
Map<String, Map<ExecutableElement, List<Element>>> namedMethodMap =
|
||||
new HashMap<String, Map<ExecutableElement, List<Element>>>();
|
||||
addElementsToNamedMethodMap(notNullElements, namedMethodMap);
|
||||
addElementsToNamedMethodMap(nullableElements, namedMethodMap);
|
||||
|
||||
for (Map.Entry<String, Map<ExecutableElement, List<Element>>> entry : namedMethodMap.entrySet()) {
|
||||
for (Map.Entry<ExecutableElement, List<Element>> subentry : entry.getValue().entrySet()) {
|
||||
checkOverriddenMethods(subentry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkClassNameConstants() {
|
||||
boolean success = checkClassNameConstant(NotNullClassName, NotNull.class);
|
||||
success &= checkClassNameConstant(NullableClassName, Nullable.class);
|
||||
return success;
|
||||
}
|
||||
|
||||
private boolean checkClassNameConstant(String className, Class<?> clazz) {
|
||||
if (className == null) {
|
||||
throw new NullPointerException("className");
|
||||
}
|
||||
|
||||
if (clazz == null) {
|
||||
throw new NullPointerException("clazz");
|
||||
}
|
||||
|
||||
if (!className.equals(clazz.getCanonicalName())) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, String.format("Unable to process null usage annotations due to class name mismatch: %s != %s", className, clazz.getCanonicalName()));
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void checkVoidMethodAnnotations(Set<? extends Element> elements, TypeElement annotationType) {
|
||||
for (Element element : elements) {
|
||||
if (element.getKind() != ElementKind.METHOD) {
|
||||
continue;
|
||||
}
|
||||
|
||||
ExecutableElement executableElement = (ExecutableElement)element;
|
||||
TypeMirror returnType = executableElement.getReturnType();
|
||||
if (returnType instanceof NoType && returnType.getKind() == TypeKind.VOID) {
|
||||
String error = String.format("void method cannot be annotated with %s", annotationType.getSimpleName());
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error, element, getAnnotationMirror(element, annotationType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void checkPrimitiveTypeAnnotations(Set<? extends Element> elements, Diagnostic.Kind kind, TypeElement annotationType) {
|
||||
for (Element element : elements) {
|
||||
TypeMirror typeToCheck;
|
||||
switch (element.getKind()) {
|
||||
case FIELD:
|
||||
case PARAMETER:
|
||||
case LOCAL_VARIABLE:
|
||||
// checking variable type
|
||||
VariableElement variableElement = (VariableElement)element;
|
||||
typeToCheck = variableElement.asType();
|
||||
break;
|
||||
|
||||
case METHOD:
|
||||
// checking return type
|
||||
ExecutableElement executableElement = (ExecutableElement)element;
|
||||
typeToCheck = executableElement.getReturnType();
|
||||
break;
|
||||
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
|
||||
if (typeToCheck instanceof PrimitiveType && typeToCheck.getKind().isPrimitive()) {
|
||||
String error = String.format("%s with a primitive type %s be annotated with %s", element.getKind().toString().replace('_', ' ').toLowerCase(), kind == Diagnostic.Kind.ERROR ? "cannot" : "should not", annotationType.getSimpleName());
|
||||
processingEnv.getMessager().printMessage(kind, error, element, getAnnotationMirror(element, annotationType));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void addElementsToNamedMethodMap(Set<? extends Element> elements, Map<String, Map<ExecutableElement, List<Element>>> namedMethodMap) {
|
||||
for (Element element : elements) {
|
||||
ExecutableElement method;
|
||||
switch (element.getKind()) {
|
||||
case PARAMETER:
|
||||
method = (ExecutableElement)element.getEnclosingElement();
|
||||
assert method.getKind() == ElementKind.METHOD;
|
||||
break;
|
||||
|
||||
case METHOD:
|
||||
method = (ExecutableElement)element;
|
||||
break;
|
||||
|
||||
default:
|
||||
continue;
|
||||
}
|
||||
|
||||
Map<ExecutableElement, List<Element>> annotatedMethodWithName =
|
||||
namedMethodMap.get(method.getSimpleName().toString());
|
||||
if (annotatedMethodWithName == null) {
|
||||
annotatedMethodWithName = new HashMap<ExecutableElement, List<Element>>();
|
||||
namedMethodMap.put(method.getSimpleName().toString(), annotatedMethodWithName);
|
||||
}
|
||||
|
||||
List<Element> annotatedElementsOfMethod = annotatedMethodWithName.get(method);
|
||||
if (annotatedElementsOfMethod == null) {
|
||||
annotatedElementsOfMethod = new ArrayList<Element>();
|
||||
annotatedMethodWithName.put(method, annotatedElementsOfMethod);
|
||||
}
|
||||
|
||||
annotatedElementsOfMethod.add(element);
|
||||
}
|
||||
}
|
||||
|
||||
private void checkOverriddenMethods(ExecutableElement method) {
|
||||
TypeElement declaringType = (TypeElement)method.getEnclosingElement();
|
||||
Set<Element> errorElements = new HashSet<Element>();
|
||||
Set<Element> warnedElements = new HashSet<Element>();
|
||||
typeLoop:
|
||||
for (TypeMirror supertypeMirror : getAllSupertypes(processingEnv.getTypeUtils().getDeclaredType(declaringType))) {
|
||||
for (Element element : ((TypeElement)processingEnv.getTypeUtils().asElement(supertypeMirror)).getEnclosedElements()) {
|
||||
if (element instanceof ExecutableElement) {
|
||||
if (processingEnv.getElementUtils().overrides(method, (ExecutableElement)element, declaringType)) {
|
||||
checkOverriddenMethod(method, (ExecutableElement)element, errorElements, warnedElements);
|
||||
continue typeLoop;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<? extends TypeMirror> getAllSupertypes(TypeMirror type) {
|
||||
Set<TypeMirror> supertypes = new HashSet<TypeMirror>();
|
||||
Deque<TypeMirror> worklist = new ArrayDeque<TypeMirror>();
|
||||
worklist.add(type);
|
||||
while (!worklist.isEmpty()) {
|
||||
List<? extends TypeMirror> next = processingEnv.getTypeUtils().directSupertypes(worklist.poll());
|
||||
if (supertypes.addAll(next)) {
|
||||
worklist.addAll(next);
|
||||
}
|
||||
}
|
||||
|
||||
return new ArrayList<TypeMirror>(supertypes);
|
||||
}
|
||||
|
||||
private void checkOverriddenMethod(ExecutableElement overrider, ExecutableElement overridden, Set<Element> errorElements, Set<Element> warnedElements) {
|
||||
// check method annotation
|
||||
if (isNullable(overrider) && isNotNull(overridden) && errorElements.add(overrider)) {
|
||||
String error = String.format("method annotated with %s cannot override or implement a method annotated with %s", nullableType.getSimpleName(), notNullType.getSimpleName());
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error, overrider, getNullableAnnotationMirror(overrider));
|
||||
}
|
||||
else if (isNullable(overrider) && !(isNullable(overridden) || isNotNull(overridden)) && !errorElements.contains(overrider) && warnedElements.add(overrider)) {
|
||||
String error = String.format("method annotated with %s overrides a method that is not annotated", nullableType.getSimpleName());
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, error, overrider, getNullableAnnotationMirror(overrider));
|
||||
}
|
||||
|
||||
List<? extends VariableElement> overriderParameters = overrider.getParameters();
|
||||
List<? extends VariableElement> overriddenParameters = overridden.getParameters();
|
||||
for (int i = 0; i < overriderParameters.size(); i++) {
|
||||
if (isNotNull(overriderParameters.get(i)) && isNullable(overriddenParameters.get(i)) && errorElements.add(overriderParameters.get(i))) {
|
||||
String error = String.format("parameter %s annotated with %s cannot override or implement a parameter annotated with %s", overriderParameters.get(i).getSimpleName(), notNullType.getSimpleName(), nullableType.getSimpleName());
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, error, overriderParameters.get(i), getNotNullAnnotationMirror(overriderParameters.get(i)));
|
||||
}
|
||||
else if (isNotNull(overriderParameters.get(i)) && !(isNullable(overriddenParameters.get(i)) || isNotNull(overriddenParameters.get(i))) && !errorElements.contains(overriderParameters.get(i)) && warnedElements.add(overriderParameters.get(i))) {
|
||||
String error = String.format("parameter %s annotated with %s overrides a parameter that is not annotated", overriderParameters.get(i).getSimpleName(), notNullType.getSimpleName());
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.WARNING, error, overriderParameters.get(i), getNotNullAnnotationMirror(overriderParameters.get(i)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isNotNull(Element element) {
|
||||
return getNotNullAnnotationMirror(element) != null;
|
||||
}
|
||||
|
||||
private boolean isNullable(Element element) {
|
||||
return getNullableAnnotationMirror(element) != null;
|
||||
}
|
||||
|
||||
private AnnotationMirror getNotNullAnnotationMirror(Element element) {
|
||||
return getAnnotationMirror(element, notNullType);
|
||||
}
|
||||
|
||||
private AnnotationMirror getNullableAnnotationMirror(Element element) {
|
||||
return getAnnotationMirror(element, nullableType);
|
||||
}
|
||||
|
||||
private AnnotationMirror getAnnotationMirror(Element element, TypeElement annotationType) {
|
||||
for (AnnotationMirror annotationMirror : element.getAnnotationMirrors()) {
|
||||
if (annotationMirror.getAnnotationType().asElement() == annotationType) {
|
||||
return annotationMirror;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* [The "BSD license"]
|
||||
* Copyright (c) 2012 Terence Parr
|
||||
* Copyright (c) 2012 Sam Harwell
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. The name of the author may not be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||||
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
package org.antlr.v4.runtime.misc;
|
||||
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* This annotation marks a field, parameter, local variable, or method (return
|
||||
* value) as potentially having the value {@code null}. The specific semantics
|
||||
* implied by this annotation depend on the kind of element the annotation is
|
||||
* applied to.
|
||||
*
|
||||
* <ul>
|
||||
* <li><strong>Field or Local Variable:</strong> Code reading the field or local
|
||||
* variable may not assume that the value is never {@code null}.</li>
|
||||
* <li><strong>Parameter:</strong> Code calling the method might pass
|
||||
* {@code null} for this parameter. The documentation for the method should
|
||||
* describe the behavior of the method in the event this parameter is
|
||||
* {@code null}.
|
||||
* </li>
|
||||
* <li><strong>Method (Return Value):</strong> Code calling the method may not
|
||||
* assume that the result of the method is never {@code null}. The documentation
|
||||
* for the method should describe the meaning of a {@code null} reference being
|
||||
* returned. Overriding methods may optionally use the {@link NotNull}
|
||||
* annotation instead of this annotation for the method, indicating that the
|
||||
* overriding method (and any method which overrides it) will never return a
|
||||
* {@code null} reference.</li>
|
||||
* </ul>
|
||||
*
|
||||
* <p>
|
||||
* The {@link NullUsageProcessor} annotation processor validates certain usage
|
||||
* scenarios for this annotation, with compile-time errors or warnings reported
|
||||
* for misuse. For detailed information about the supported analysis, see the
|
||||
* documentation for {@link NullUsageProcessor}.</p>
|
||||
*/
|
||||
@Documented
|
||||
@Retention(RetentionPolicy.CLASS)
|
||||
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
|
||||
public @interface Nullable {
|
||||
}
|
129
tool/pom.xml
129
tool/pom.xml
|
@ -1,11 +1,21 @@
|
|||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4</artifactId>
|
||||
<version>4.5-SNAPSHOT</version>
|
||||
|
||||
<name>ANTLR 4 Tool</name>
|
||||
<description>The ANTLR 4 grammar compiler.</description>
|
||||
|
||||
<!--
|
||||
IMPORTANT NOTE
|
||||
Building ANTLR is now performed via the bild.py script.
|
||||
However, we still need a pom to: - manage dependencies during development
|
||||
- publish metadata to maven central The above are the only reason for keeping
|
||||
this file. No support will be provided.
|
||||
-->
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Terence Parr</name>
|
||||
|
@ -94,7 +104,6 @@
|
|||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-runtime</artifactId>
|
||||
<version>4.5-SNAPSHOT</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.antlr</groupId>
|
||||
|
@ -110,123 +119,5 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>sonatype-oss-release</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.sonatype.plugins</groupId>
|
||||
<artifactId>nexus-staging-maven-plugin</artifactId>
|
||||
<version>1.6.3</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<serverId>ossrh</serverId>
|
||||
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
|
||||
<autoReleaseAfterClose>true</autoReleaseAfterClose>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<configuration>
|
||||
<includeDependencySources>true</includeDependencySources>
|
||||
<dependencySourceIncludes>
|
||||
<dependencySourceInclude>org.antlr:antlr4-*</dependencySourceInclude>
|
||||
</dependencySourceIncludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>2.2</version>
|
||||
<inherited>false</inherited>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>shade</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<minimizeJar>true</minimizeJar>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<shadedArtifactAttached>true</shadedArtifactAttached>
|
||||
<createSourcesJar>true</createSourcesJar>
|
||||
<shadedClassifierName>complete</shadedClassifierName>
|
||||
<filters>
|
||||
<filter>
|
||||
<artifact>org.antlr:antlr4-runtime</artifact>
|
||||
<includes>
|
||||
<include>org/antlr/v4/runtime/**</include>
|
||||
</includes>
|
||||
</filter>
|
||||
<filter>
|
||||
<artifact>org.antlr:antlr4-annotations</artifact>
|
||||
<includes>
|
||||
<include>org/antlr/v4/runtime/**</include>
|
||||
</includes>
|
||||
</filter>
|
||||
</filters>
|
||||
<transformers>
|
||||
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
|
||||
<mainClass>org.antlr.v4.Tool</mainClass>
|
||||
</transformer>
|
||||
</transformers>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<build>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<testSourceDirectory>test</testSourceDirectory>
|
||||
<testResources>
|
||||
<testResource>
|
||||
<directory>test</directory>
|
||||
</testResource>
|
||||
</testResources>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr3-maven-plugin</artifactId>
|
||||
<version>3.5.2</version>
|
||||
<configuration>
|
||||
<sourceDirectory>src</sourceDirectory>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>antlr</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>test-jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
|
|
Loading…
Reference in New Issue