From 6d7a4da067f3bf3175a3ddc72f8f3d205faeffae Mon Sep 17 00:00:00 2001 From: sjaakd Date: Tue, 23 Sep 2014 23:21:36 +0200 Subject: [PATCH] #298 fix for not found annotation nullpointer --- .../org/mapstruct/ap/MappingProcessor.java | 2 +- .../AnnotationNotFoundTest.java | 55 +++++++++++++++++++ .../annotationnotfound/ErroneousMapper.java | 32 +++++++++++ .../NotFoundAnnotation.java | 33 +++++++++++ .../erroneous/annotationnotfound/Source.java | 39 +++++++++++++ .../erroneous/annotationnotfound/Target.java | 39 +++++++++++++ 6 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/AnnotationNotFoundTest.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/ErroneousMapper.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/NotFoundAnnotation.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/Source.java create mode 100644 processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/Target.java diff --git a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java index dba726ef1..3b8ece218 100644 --- a/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java +++ b/processor/src/main/java/org/mapstruct/ap/MappingProcessor.java @@ -137,7 +137,7 @@ public class MappingProcessor extends AbstractProcessor { // on some JDKs, RoundEnvironment.getElementsAnnotatedWith( ... ) returns types with // annotations unknown to the compiler, even though they are not declared Mappers - if ( MapperPrism.getInstanceOn( mapperTypeElement ) == null ) { + if ( mapperTypeElement == null || MapperPrism.getInstanceOn( mapperTypeElement ) == null ) { continue; } diff --git a/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/AnnotationNotFoundTest.java b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/AnnotationNotFoundTest.java new file mode 100644 index 000000000..d766ce103 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/AnnotationNotFoundTest.java @@ -0,0 +1,55 @@ +/** + * Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.erroneous.annotationnotfound; + +import javax.tools.Diagnostic.Kind; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mapstruct.ap.testutil.IssueKey; +import org.mapstruct.ap.testutil.WithClasses; +import org.mapstruct.ap.testutil.compilation.annotation.CompilationResult; +import org.mapstruct.ap.testutil.compilation.annotation.Diagnostic; +import org.mapstruct.ap.testutil.compilation.annotation.ExpectedCompilationOutcome; +import org.mapstruct.ap.testutil.runner.AnnotationProcessorTestRunner; + +/** + * Test for (custom / external) annotation that is not on class path + * + * @author Sjaak Derksen + */ +@WithClasses( { Source.class, Target.class, ErroneousMapper.class } ) +@RunWith( AnnotationProcessorTestRunner.class ) +public class AnnotationNotFoundTest { + + @Test + @IssueKey( "298" ) + @ExpectedCompilationOutcome( + value = CompilationResult.FAILED, + diagnostics = { + @Diagnostic( type = ErroneousMapper.class, + kind = Kind.ERROR, + line = 30, + messageRegExp = "cannot find symbol\n" + + " symbol: class NotFoundAnnotation\n" + + " location: interface org.mapstruct.ap.test.erroneous.annotationnotfound.ErroneousMapper" ) + } + ) + public void shouldFailToGenerateMappings() { + } +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/ErroneousMapper.java b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/ErroneousMapper.java new file mode 100644 index 000000000..88017a372 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/ErroneousMapper.java @@ -0,0 +1,32 @@ +/** + * Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.erroneous.annotationnotfound; + +import org.mapstruct.Mapper; + +/** + * + * @author Sjaak Derksen + */ +@Mapper +public interface ErroneousMapper { + + @NotFoundAnnotation + Target map( Source source ); +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/NotFoundAnnotation.java b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/NotFoundAnnotation.java new file mode 100644 index 000000000..6273ec67e --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/NotFoundAnnotation.java @@ -0,0 +1,33 @@ +/** + * Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.erroneous.annotationnotfound; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * + * @author Sjaak Derksen + */ +@Retention(RetentionPolicy.CLASS) +@java.lang.annotation.Target(ElementType.METHOD) +public @interface NotFoundAnnotation { + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/Source.java b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/Source.java new file mode 100644 index 000000000..dbe616870 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/Source.java @@ -0,0 +1,39 @@ +/** + * Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.erroneous.annotationnotfound; + +/** + * + * @author Sjaak Derksen + */ +public class Source { + + private Integer foo; + + public Integer getFoo() { + return foo; + } + + public void setFoo( Integer foo ) { + this.foo = foo; + } + + + +} diff --git a/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/Target.java b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/Target.java new file mode 100644 index 000000000..0ad65d696 --- /dev/null +++ b/processor/src/test/java/org/mapstruct/ap/test/erroneous/annotationnotfound/Target.java @@ -0,0 +1,39 @@ +/** + * Copyright 2012-2014 Gunnar Morling (http://www.gunnarmorling.de/) + * and/or other contributors as indicated by the @authors tag. See the + * copyright.txt file in the distribution for a full listing of all + * contributors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.mapstruct.ap.test.erroneous.annotationnotfound; + +/** + * + * @author Sjaak Derksen + */ +public class Target { + + private String foo; + + public String getFoo() { + return foo; + } + + public void setFoo( String foo ) { + this.foo = foo; + } + + + +}