From 0179ad6058f54fb3052b6d612814e840eaf4d534 Mon Sep 17 00:00:00 2001 From: "@fbiville" Date: Thu, 16 Mar 2017 20:41:55 +0100 Subject: [PATCH] =?UTF-8?q?Merge=20@=E2=80=8BPerformWrites=20check=20into?= =?UTF-8?q?=20procedure=20processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../procedure/PerformsWriteProcessor.java | 89 ------------------- .../visitors/StoredProcedureVisitor.java | 10 ++- .../procedure/PerformsWriteProcessorTest.java | 52 ----------- .../procedure/ProcedureProcessorTest.java | 13 +++ 4 files changed, 22 insertions(+), 142 deletions(-) delete mode 100644 community/procedure-compiler/processor/src/main/java/org/neo4j/tooling/procedure/PerformsWriteProcessor.java delete mode 100644 community/procedure-compiler/processor/src/test/java/org/neo4j/tooling/procedure/PerformsWriteProcessorTest.java diff --git a/community/procedure-compiler/processor/src/main/java/org/neo4j/tooling/procedure/PerformsWriteProcessor.java b/community/procedure-compiler/processor/src/main/java/org/neo4j/tooling/procedure/PerformsWriteProcessor.java deleted file mode 100644 index c1b7a1cf1f27c..0000000000000 --- a/community/procedure-compiler/processor/src/main/java/org/neo4j/tooling/procedure/PerformsWriteProcessor.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright (c) 2002-2017 "Neo Technology," - * Network Engine for Objects in Lund AB [http://neotechnology.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.tooling.procedure; - -import com.google.auto.service.AutoService; -import org.neo4j.tooling.procedure.messages.CompilationMessage; -import org.neo4j.tooling.procedure.messages.MessagePrinter; -import org.neo4j.tooling.procedure.visitors.PerformsWriteMethodVisitor; - -import java.lang.annotation.Annotation; -import java.util.HashSet; -import java.util.Set; -import java.util.stream.Stream; -import javax.annotation.processing.AbstractProcessor; -import javax.annotation.processing.ProcessingEnvironment; -import javax.annotation.processing.Processor; -import javax.annotation.processing.RoundEnvironment; -import javax.lang.model.SourceVersion; -import javax.lang.model.element.Element; -import javax.lang.model.element.ElementVisitor; -import javax.lang.model.element.TypeElement; - -import org.neo4j.procedure.PerformsWrites; - -@AutoService( Processor.class ) -public class PerformsWriteProcessor extends AbstractProcessor -{ - private static final Class performWritesType = PerformsWrites.class; - private MessagePrinter messagePrinter; - private ElementVisitor,Void> visitor; - - @Override - public Set getSupportedAnnotationTypes() - { - Set types = new HashSet<>(); - types.add( performWritesType.getName() ); - return types; - } - - @Override - public SourceVersion getSupportedSourceVersion() - { - return SourceVersion.RELEASE_8; - } - - @Override - public synchronized void init( ProcessingEnvironment processingEnv ) - { - super.init( processingEnv ); - messagePrinter = new MessagePrinter( processingEnv.getMessager() ); - visitor = new PerformsWriteMethodVisitor(); - } - - @Override - public boolean process( Set annotations, RoundEnvironment roundEnv ) - { - processPerformsWriteElements( roundEnv ); - return false; - } - - private void processPerformsWriteElements( RoundEnvironment roundEnv ) - { - roundEnv.getElementsAnnotatedWith( performWritesType ).stream().flatMap( this::validate ) - .forEachOrdered( messagePrinter::print ); - - } - - private Stream validate( Element element ) - { - return visitor.visit( element ); - } -} diff --git a/community/procedure-compiler/processor/src/main/java/org/neo4j/tooling/procedure/visitors/StoredProcedureVisitor.java b/community/procedure-compiler/processor/src/main/java/org/neo4j/tooling/procedure/visitors/StoredProcedureVisitor.java index 14b6a94745092..176bef483ac94 100644 --- a/community/procedure-compiler/processor/src/main/java/org/neo4j/tooling/procedure/visitors/StoredProcedureVisitor.java +++ b/community/procedure-compiler/processor/src/main/java/org/neo4j/tooling/procedure/visitors/StoredProcedureVisitor.java @@ -46,6 +46,7 @@ public class StoredProcedureVisitor extends SimpleElementVisitor8,Void> classVisitor; private final TypeVisitor,Void> recordVisitor; private final ElementVisitor,Void> parameterVisitor; + private final ElementVisitor,Void> performsWriteVisitor; public StoredProcedureVisitor( Types typeUtils, Elements elementUtils, boolean ignoresWarnings ) { @@ -56,6 +57,7 @@ public StoredProcedureVisitor( Types typeUtils, Elements elementUtils, boolean i this.classVisitor = new StoredProcedureClassVisitor( typeUtils, elementUtils, ignoresWarnings ); this.recordVisitor = new RecordTypeVisitor( typeUtils, typeMirrors ); this.parameterVisitor = new ParameterVisitor( new ParameterTypeVisitor( typeUtils, typeMirrors ) ); + this.performsWriteVisitor = new PerformsWriteMethodVisitor(); } /** @@ -66,7 +68,8 @@ public Stream visitExecutable( ExecutableElement executableE { return Stream.of( classVisitor.visit( executableElement.getEnclosingElement() ), validateParameters( executableElement.getParameters(), ignored ), - validateReturnType( executableElement ) ).flatMap( Function.identity() ); + validateReturnType( executableElement ), validatePerformsWriteUsage( executableElement ) ) + .flatMap( Function.identity() ); } private Stream validateParameters( List parameters, Void ignored ) @@ -97,6 +100,11 @@ private Stream validateReturnType( ExecutableElement method return recordVisitor.visit( returnType ); } + private Stream validatePerformsWriteUsage( ExecutableElement executableElement ) + { + return performsWriteVisitor.visit( executableElement ); + } + private AnnotationMirror annotationMirror( List mirrors ) { AnnotationTypeVisitor nameVisitor = new AnnotationTypeVisitor( Name.class ); diff --git a/community/procedure-compiler/processor/src/test/java/org/neo4j/tooling/procedure/PerformsWriteProcessorTest.java b/community/procedure-compiler/processor/src/test/java/org/neo4j/tooling/procedure/PerformsWriteProcessorTest.java deleted file mode 100644 index bf8f72307615d..0000000000000 --- a/community/procedure-compiler/processor/src/test/java/org/neo4j/tooling/procedure/PerformsWriteProcessorTest.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright (c) 2002-2017 "Neo Technology," - * Network Engine for Objects in Lund AB [http://neotechnology.com] - * - * This file is part of Neo4j. - * - * Neo4j is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -package org.neo4j.tooling.procedure; - -import com.google.testing.compile.CompilationRule; -import org.neo4j.tooling.procedure.testutils.JavaFileObjectUtils; -import org.junit.Rule; -import org.junit.Test; - -import javax.annotation.processing.Processor; -import javax.tools.JavaFileObject; - -import static com.google.common.truth.Truth.assert_; -import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; - -public class PerformsWriteProcessorTest -{ - @Rule - public CompilationRule compilation = new CompilationRule(); - - private Processor processor = new PerformsWriteProcessor(); - - @Test - public void fails_with_conflicting_mode() - { - JavaFileObject procedure = JavaFileObjectUtils.INSTANCE.procedureSource( - "invalid/conflicting_mode/ConflictingMode.java" ); - - assert_().about( javaSource() ).that( procedure ).processedWith( processor ).failsToCompile() - .withErrorCount( 1 ) - .withErrorContaining( "@PerformsWrites usage error: cannot use mode other than Mode.DEFAULT" ) - .in( procedure ).onLine( 30 ); - - } -} diff --git a/community/procedure-compiler/processor/src/test/java/org/neo4j/tooling/procedure/ProcedureProcessorTest.java b/community/procedure-compiler/processor/src/test/java/org/neo4j/tooling/procedure/ProcedureProcessorTest.java index 4a208071e1a3a..c3a5d5e5ec0a8 100644 --- a/community/procedure-compiler/processor/src/test/java/org/neo4j/tooling/procedure/ProcedureProcessorTest.java +++ b/community/procedure-compiler/processor/src/test/java/org/neo4j/tooling/procedure/ProcedureProcessorTest.java @@ -256,4 +256,17 @@ public void fails_if_context_injected_fields_have_unsupported_types() "BadContextUnsupportedTypeError#foo, expected one of: , , , , " ) .in( sproc ).onLine( 28 ); } + + @Test + public void fails_with_conflicting_mode() + { + JavaFileObject procedure = JavaFileObjectUtils.INSTANCE.procedureSource( + "invalid/conflicting_mode/ConflictingMode.java" ); + + assert_().about( javaSource() ).that( procedure ).processedWith( processor ).failsToCompile() + .withErrorCount( 1 ) + .withErrorContaining( "@PerformsWrites usage error: cannot use mode other than Mode.DEFAULT" ) + .in( procedure ).onLine( 30 ); + + } }