From 80f6342a7f709a2d6fe0cf3f1274abc00b566ec7 Mon Sep 17 00:00:00 2001 From: Jay Arthanareeswaran Date: Mon, 27 Jul 2015 17:38:40 +0530 Subject: Bug 340635 - Types#erasure() doesn't return the raw type. Change-Id: Ie460e993af920229a8ef5a338f93b79bfda6dcc3 --- .../lib/apttestprocessors.jar | Bin 193253 -> 198078 bytes .../AnnotationProcessorTests/Bug340635Proc.java | 89 +++++++++++++++++++++ .../bug340635/annotation/GetType.java | 11 +++ .../bug340635/classes/MyImpl.java | 12 +++ .../bug340635/classes/MyInterface.java | 6 ++ .../apt/tests/AnnotationProcessorTests.java | 49 ++++++++++++ 6 files changed, 167 insertions(+) create mode 100644 org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/AnnotationProcessorTests/Bug340635Proc.java create mode 100644 org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/annotation/GetType.java create mode 100644 org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/classes/MyImpl.java create mode 100644 org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/classes/MyInterface.java (limited to 'org.eclipse.jdt.compiler.apt.tests') diff --git a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors.jar b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors.jar index 52cebf9c9e..d60c3cec07 100644 Binary files a/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors.jar and b/org.eclipse.jdt.compiler.apt.tests/lib/apttestprocessors.jar differ diff --git a/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/AnnotationProcessorTests/Bug340635Proc.java b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/AnnotationProcessorTests/Bug340635Proc.java new file mode 100644 index 0000000000..ca60bf979f --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/processors/org/eclipse/jdt/compiler/apt/tests/processors/AnnotationProcessorTests/Bug340635Proc.java @@ -0,0 +1,89 @@ +/******************************************************************************* + * Copyright (c) 2015 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Kevin Pollet - SERLI - (kevin.pollet@serli.com) - initial API and implementation + *******************************************************************************/ +package org.eclipse.jdt.compiler.apt.tests.processors.AnnotationProcessorTests; + +import java.util.Set; + +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Messager; +import javax.annotation.processing.RoundEnvironment; +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.TypeElement; +import javax.lang.model.type.DeclaredType; +import javax.lang.model.type.TypeMirror; +import javax.lang.model.util.SimpleTypeVisitor6; +import javax.lang.model.util.Types; +import javax.tools.Diagnostic.Kind; + +@SupportedSourceVersion(SourceVersion.RELEASE_6) +@SupportedAnnotationTypes("annotation.GetType") +public class Bug340635Proc extends AbstractProcessor { + + private static final boolean ALLOW_OTHER_PROCESSORS_TO_PROCESS = false; + + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + final Types types = processingEnv.getTypeUtils(); + final Messager messager = processingEnv.getMessager(); + + for (TypeElement annotation : annotations) { + final Set elements = roundEnv.getElementsAnnotatedWith(annotation); + + // process the annotations + for (Element element : elements) { + // search and display informations about the first generic type + // found in the + // class or interface hierarchy. + if (element.getKind().isInterface() || element.getKind().isClass()) { + DeclaredType genericType = element.asType().accept(new GenericTypeVisitor(types), null); + DeclaredType erasedType = (DeclaredType) types.erasure(genericType); + + StringBuffer message = new StringBuffer(); + message.append("Erased type: " + erasedType); + message.append(" - type arguments: "); + for (TypeMirror typeArgument : erasedType.getTypeArguments()) { + message.append(typeArgument + ","); + } + messager.printMessage(Kind.WARNING, message.toString(), element); + } + } + + } + + return ALLOW_OTHER_PROCESSORS_TO_PROCESS; + } + + private class GenericTypeVisitor extends SimpleTypeVisitor6 { + private final Types types; + + public GenericTypeVisitor(Types types) { + this.types = types; + } + + @Override + public DeclaredType visitDeclared(DeclaredType t, Void p) { + if (t.getTypeArguments().size() > 0) { + return t; + } + for (TypeMirror superType : types.directSupertypes(t)) { + DeclaredType tmp = superType.accept(this, p); + if (tmp != null) { + return tmp; + } + } + return null; + } + } + +} diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/annotation/GetType.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/annotation/GetType.java new file mode 100644 index 0000000000..3cbc06a9da --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/annotation/GetType.java @@ -0,0 +1,11 @@ +package annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface GetType { +} diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/classes/MyImpl.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/classes/MyImpl.java new file mode 100644 index 0000000000..e109efea96 --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/classes/MyImpl.java @@ -0,0 +1,12 @@ +package classes; + +import classes.MyInterface; +import annotation.GetType; + +@GetType +public class MyImpl implements MyInterface { + @Override + public Class foo(String param) { + return String.class; + } +} diff --git a/org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/classes/MyInterface.java b/org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/classes/MyInterface.java new file mode 100644 index 0000000000..12d82c0dc1 --- /dev/null +++ b/org.eclipse.jdt.compiler.apt.tests/resources/targets/AnnotationProcessorTests/bug340635/classes/MyInterface.java @@ -0,0 +1,6 @@ +package classes; + +public interface MyInterface { + + Class foo(S param); +} diff --git a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/AnnotationProcessorTests.java b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/AnnotationProcessorTests.java index 74170ce3a0..f34011e88e 100644 --- a/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/AnnotationProcessorTests.java +++ b/org.eclipse.jdt.compiler.apt.tests/src/org/eclipse/jdt/compiler/apt/tests/AnnotationProcessorTests.java @@ -16,12 +16,45 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Locale; +import javax.tools.Diagnostic; +import javax.tools.DiagnosticListener; import javax.tools.JavaCompiler; +import javax.tools.JavaFileObject; import junit.framework.TestCase; public class AnnotationProcessorTests extends TestCase { + + public final class DiagnosticReport implements DiagnosticListener { + public int count; + public StringBuffer buffer; + private List> warnings = new ArrayList<>(); + DiagnosticReport() { + this.count = 0; + this.buffer = new StringBuffer(); + } + public void report(Diagnostic diagnostic) { + if (diagnostic.getKind() == Diagnostic.Kind.WARNING) { + warnings.add(diagnostic); + count++; + buffer.append(diagnostic.getMessage(Locale.getDefault())); + buffer.append("\n"); + } + } + public Diagnostic getErrorAt(int index) { + return warnings.get(index); + } + public String toString() { + return this.buffer.toString(); + } + public void clear() { + this.count = 0; + this.buffer = new StringBuffer(); + } + } + @Override protected void setUp() throws Exception { super.setUp(); @@ -83,4 +116,20 @@ public class AnnotationProcessorTests extends TestCase { BatchTestUtils.compileTreeWithErrors(compiler, options, targetFolder, null, true); assertNull(System.getProperty(PROC)); } + public void testBug340635() throws IOException { + JavaCompiler compiler = BatchTestUtils.getEclipseCompiler(); + File targetFolder = TestUtils.concatPath(BatchTestUtils.getSrcFolderName(), "targets", "AnnotationProcessorTests", "bug340635"); + BatchTestUtils.copyResources("targets/AnnotationProcessorTests/bug340635", targetFolder); + List options = new ArrayList(); + final String PROC = "org.eclipse.jdt.compiler.apt.tests.processors.AnnotationProcessorTests.Bug340635Proc"; + options.add("-processorpath"); + options.add(" "); + options.add("-processor"); + options.add(PROC); + DiagnosticReport diagnosticListener = new DiagnosticReport(); + BatchTestUtils.compileTreeWithErrors(compiler, options, targetFolder, diagnosticListener, true); + assertNull(System.getProperty(PROC)); + assertEquals("incorrect number of messages", 1, diagnosticListener.count); + assertEquals("Erased type: classes.MyInterface - type arguments: \n", diagnosticListener.buffer.toString()); + } } -- cgit v1.2.3