Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 434378

Summary: Types.isAssignable()
Product: [Eclipse Project] JDT Reporter: Jonathan Fuerth <jf.eclipse>
Component: APTAssignee: Jay Arthanareeswaran <jarthana>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: jarthana
Version: 3.3   
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X   
Whiteboard: stalebug

Description Jonathan Fuerth CLA 2014-05-07 18:49:55 EDT
org.eclipse.jdt.apt.core version 3.3.500.v20120516-0617

Under JDT APT, the return value of Types.getNoType(TypeKind.VOID) does not work properly with Types.isAssignable(). isAssignable() should say the return type of a void method is assignable to the TypeMirror returned by Types.getNoType(TypeKind.VOID), but it does not. (Javac does)

Here's a recipe for reproducing the issue. Given the following annotation:

------
package ca.fuerth.junk.ap;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * An example annotation that comes with a processor that ensures the target method returns void.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface VoidMethod {

}
------


And the following annotation processor:

------
package ca.fuerth.junk.ap;

import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
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.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeKind;
import javax.lang.model.type.TypeMirror;
import javax.lang.model.util.Types;
import javax.tools.Diagnostic.Kind;

/**
 * An example annotation processor that ensures methods annotated with {@code @VoidMethod} return void.
 */
@SupportedAnnotationTypes("ca.fuerth.junk.ap.VoidMethod")
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class VoidMethodAnnotationProcessor extends AbstractProcessor {

    @Override
    public boolean process( Set<? extends TypeElement> annotations, RoundEnvironment roundEnv ) {
        final Types typeUtils = processingEnv.getTypeUtils();
        for (TypeElement annotation : annotations) {
            for (Element method : roundEnv.getElementsAnnotatedWith( annotation )) {
                TypeMirror voidType = typeUtils.getNoType( TypeKind.VOID );
                if (!typeUtils.isAssignable( ((ExecutableElement) method).getReturnType(), voidType )) {
                    processingEnv.getMessager().printMessage( Kind.ERROR, "This method has to return void", method );
                } else {
                    processingEnv.getMessager().printMessage( Kind.WARNING, "Looks good!", method );
                }
            }
        }
        return true;
    }
}
------


Then the following class (in a separate project in a separate workspace, as required by JDT APT):

------
package ca.fuerth.misc;

import ca.fuerth.junk.ap.VoidMethod;


public class HasVoidMethodAnnotation {

    @VoidMethod
    public void thisIsAVoidMethod() {
        return;
    }
}
------

gets an error marker "This method has to return void" on the annotated method.

Compiling the same file under javac results in the warning message "Looks good!"
Comment 1 Eclipse Genie CLA 2020-03-05 17:35:31 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet. As such, we're closing this bug.

If you have further information on the current state of the bug, please add it and reopen this bug. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.