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

Bug 334943

Summary: No default value for annotation binding
Product: [Eclipse Project] JDT Reporter: Sebastian Zarnekow <sebastian.zarnekow>
Component: CoreAssignee: Jay Arthanareeswaran <jarthana>
Status: CLOSED WONTFIX QA Contact:
Severity: normal    
Priority: P3 CC: allprog, amj87.iitr, daniel_megert, digulla, satyam.kandula, srikanth_sankaran
Version: 3.5.2Keywords: helpwanted
Target Milestone: ---   
Hardware: PC   
OS: Mac OS X - Carbon (unsup.)   
Whiteboard: stalebug

Description Sebastian Zarnekow CLA 2011-01-20 15:23:20 EST
All this is with Eclipse 3.5.2 but I could test it with 3.6, too (if that would be helpful).

I'm facing a strange inconsistency when dealing with annotation bindings. In some cases, SourceTypes do not expose annotation defaults.

Please consider the following annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface TestAnnotationWithStringDefault {

    String emptyString() default "";
    String string() default "string";
    
    @TestAnnotationWithStringDefault
    public static class Annotated {}
    
    @TestAnnotationWithStringDefault
    public interface AnnotatedInterface {}
}

and another interface

@TestAnnotationWithStringDefault
public interface AnnotatedInterfaceWithStringDefault {}

Everything is working like a charm when only the class files are present. However, when I try to obtain the ITypeBinding from the source for the inner type TestAnnotationWithStringDefault.Annotated and navigate to the annotations via IBinging#getAnnotations, I'll get an annotation binding with both defaults correctly set. Unfortunately that is not the case when I'm starting with the type AnnotatedInterfaceWithStringDefault. It exposes an annotation binding, too but #getAllMemberValuePairs returns an array with two default value pairs which both point to the value null - which is obviously not correct. Am I doing something wrong?

This the downstripped code that I'm using:

ASTParser parser = ASTParser.newParser(AST.JLS3);
parser.setProject(jdtType.getJavaProject());
// jdtType is AnnotatedInterfaceWithStringDefault
ITypeBinding binding = parser.createBindings(
  new IJavaElement[] {jdtType}, null)[0];
for (IAnnotationBinding annotation : binding.getAnnotations()) {
  for (IMemberValuePairBinding memberValuePair : annotation.getAllMemberValuePairs()) {
    Object value = memberValuePair.getValue();
    // value is null :-(
    ..
  }
}

If somebody is interested: the complete code can be reviewed here:
http://git.eclipse.org/c/tmf/org.eclipse.xtext.git/tree/plugins/org.eclipse.xtext.common.types.ui/src/org/eclipse/xtext/common/types/access/jdt/JdtBasedTypeFactory.java starting with line #77
Comment 1 Satyam Kandula CLA 2011-01-21 04:40:22 EST
Look at http://www.eclipse.org/forums/index.php?t=msg&th=203292&start=0&S=77aca5df744c8d4082b2b67adb3c18e9 for more information.
Comment 2 Jay Arthanareeswaran CLA 2011-01-24 04:55:03 EST
In your code, when you call binding.getAnnotations() on shouldn't you be expecting bindings @Retention and @Target?
Comment 3 Jay Arthanareeswaran CLA 2011-01-24 05:20:04 EST
(In reply to comment #2)
> In your code, when you call binding.getAnnotations() on shouldn't you be
> expecting bindings @Retention and @Target?

Actually, never mind. I missed some part of the code in my test.
Comment 4 Sebastian Zarnekow CLA 2012-02-27 14:11:00 EST
Any progress on this one?
Comment 5 Sebastian Zarnekow CLA 2012-02-27 14:14:52 EST
I get these two stackframes

java.lang.NullPointerException
	at org.eclipse.jdt.core.dom.DefaultValuePairBinding.<init>(DefaultValuePairBinding.java:31)
	at org.eclipse.jdt.core.dom.AnnotationBinding.getAllMemberValuePairs(AnnotationBinding.java:98)
[snip]

when I try to parse the following 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(value={ ElementType.METHOD })
public @interface Inline {
	String value();
	Class<?>[] imported() default {};
	InlineContext when() default InlineContext.EXPRESSION;
}

public enum InlineContext {
	ALWAYS,	EXPRESSION, STATEMENT
}

public class SomeClass {
	@Inline("something")
	public static String someMethod() {}
}
Comment 6 Jay Arthanareeswaran CLA 2012-02-28 01:11:11 EST
(In reply to comment #4)
> Any progress on this one?

I haven't had a chance to look at this yet. I will try and see if I can take in the coming week, though I am not very sure. But you are welcome to post a patch if you can.
Comment 7 Sebastian Zarnekow CLA 2012-06-14 06:11:52 EDT
Any chance to get this into 3.8.x or 3.9?
Comment 8 Srikanth Sankaran CLA 2012-06-15 01:24:23 EDT
(In reply to comment #7)
> Any chance to get this into 3.8.x or 3.9?

Jay, Could we come up with an estimate of the work involved here
and whether there are any workarounds available.

If it is merely a bug fix, we could consider it for 4.3. If it proves
more work than that, we may have to defer it given Java 8 work is the
top priority at the moment and we are very short of hands for that
effort.

Sebastian, are you willing and able to propose a patch ?
Comment 9 Sebastian Zarnekow CLA 2012-06-15 03:11:57 EDT
(In reply to comment #8)
> Sebastian, are you willing and able to propose a patch ?

Yes, I could work on a patch. Given that I'm short of time, too (who's not?), some code pointers would be helpful - just to make sure that I'm not wasting everybodys time by looking at the wrong parts of the code.
Comment 10 Aaron Digulla CLA 2012-06-15 04:10:59 EDT
Yesterday, I ran into this bug, too. A fix in 3.8.1 would be great.
Comment 11 Andras Kovi CLA 2014-02-17 03:09:38 EST
This issue is still there in version 2.5.1

I get various errors:

[INFO] --- xtend-maven-plugin:2.5.1:compile (default) @ variables ---
[ERROR] resource is empty: java:/Objects/com.MySection
[ERROR] 
ERROR: 	MyProcessor.xtend - 
28: The method name is undefined for the type MyProcessor
[ERROR] 
ERROR: 	MyProcessor.xtend - 
28: The method value is undefined for the type MyProcessor
[ERROR] 
ERROR: 	MyProcessor.xtend - 
27: There is no context to infer the closure's argument types from. Consider typing the arguments or put the closures into a typed context.

When I modified the closure in question, I got the infamous:

[ERROR] resolution of uriFragment '|3' failed.
org.eclipse.emf.ecore.resource.impl.ResourceSetImpl$1DiagnosticWrappedException: org.eclipse.xtext.common.types.access.TypeResource$CannotLoadTypeResourceException: java.lang.NullPointerException
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.handleDemandLoadException(ResourceSetImpl.java:319)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:278)
	at org.eclipse.xtext.resource.XtextResourceSet.getResource(XtextResourceSet.java:201)
	at org.eclipse.xtext.common.types.access.impl.ClasspathTypeProvider.findTypeByClass(ClasspathTypeProvider.java:212)
	at org.eclipse.xtext.common.types.access.impl.ClasspathTypeProvider.doFindTypeByName(ClasspathTypeProvider.java:197)
	at org.eclipse.xtext.common.types.access.impl.ClasspathTypeProvider.findTypeByName(ClasspathTypeProvider.java:189)
	at org.eclipse.xtext.common.types.xtext.AbstractTypeScope.getSingleElement(AbstractTypeScope.java:60)
	at org.eclipse.xtext.common.types.xtext.AbstractTypeScope.getSingleElement(AbstractTypeScope.java:54)
	at org.eclipse.xtend.core.scoping.RecordingTypeScope.getSingleElement(RecordingTypeScope.java:41)
	at org.eclipse.xtext.scoping.impl.AbstractScope.getSingleElement(AbstractScope.java:105)
	at org.eclipse.xtext.linking.impl.DefaultLinkingService.getLinkedObjects(DefaultLinkingService.java:120)
	at org.eclipse.xtext.linking.lazy.LazyLinkingResource.getEObject(LazyLinkingResource.java:228)
	at org.eclipse.xtext.xbase.resource.BatchLinkableResource.getEObject(BatchLinkableResource.java:105)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.getEObject(ResourceSetImpl.java:223)
	at org.eclipse.emf.ecore.util.EcoreUtil.resolve(EcoreUtil.java:198)
	at org.eclipse.emf.ecore.util.EcoreUtil.resolve(EcoreUtil.java:258)
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eResolveProxy(BasicEObjectImpl.java:1473)
	at org.eclipse.xtext.xtype.impl.XImportDeclarationImpl.getImportedType(XImportDeclarationImpl.java:188)
	at org.eclipse.xtext.xtype.impl.XImportDeclarationImpl.eGet(XImportDeclarationImpl.java:283)
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1011)
	at org.eclipse.emf.ecore.impl.BasicEObjectImpl.eGet(BasicEObjectImpl.java:1003)
	at org.eclipse.emf.ecore.util.EContentsEList$FeatureIteratorImpl.hasNext(EContentsEList.java:403)
	at org.eclipse.emf.ecore.util.EcoreUtil.resolveCrossReferences(EcoreUtil.java:303)
	at org.eclipse.emf.ecore.util.EcoreUtil.resolveAll(EcoreUtil.java:297)
	at org.eclipse.emf.ecore.util.EcoreUtil.resolveAll(EcoreUtil.java:282)
	at org.eclipse.emf.ecore.util.EcoreUtil.resolveAll(EcoreUtil.java:270)
	at org.eclipse.xtend.core.compiler.batch.XtendBatchCompiler.compile(XtendBatchCompiler.java:392)
	at org.eclipse.xtend.maven.AbstractXtendCompilerMojo.compile(AbstractXtendCompilerMojo.java:109)
	at org.eclipse.xtend.maven.XtendCompile.compileSources(XtendCompile.java:66)
	at org.eclipse.xtend.maven.XtendCompile.internalExecute(XtendCompile.java:59)
	at org.eclipse.xtend.maven.AbstractXtendMojo.execute(AbstractXtendMojo.java:39)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)
	at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)
	at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)
	at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)
	at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)
	at org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)
	at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)
	at org.apache.maven.cli.MavenCli.main(MavenCli.java:141)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)
	at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)
	at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)
	at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)
Caused by: org.eclipse.xtext.common.types.access.TypeResource$CannotLoadTypeResourceException: java.lang.NullPointerException
	at org.eclipse.xtext.common.types.access.TypeResource.doLoad(TypeResource.java:118)
	at org.eclipse.emf.ecore.resource.impl.ResourceImpl.load(ResourceImpl.java:1518)
	at org.eclipse.xtext.common.types.access.TypeResource.load(TypeResource.java:102)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoad(ResourceSetImpl.java:259)
	at org.eclipse.emf.ecore.resource.impl.ResourceSetImpl.demandLoadHelper(ResourceSetImpl.java:274)
	... 50 more
Caused by: java.lang.RuntimeException: java.lang.NullPointerException
	at org.eclipse.xtext.common.types.access.impl.DeclaredTypeFactory.createType(DeclaredTypeFactory.java:94)
	at org.eclipse.xtext.common.types.access.impl.DeclaredTypeFactory.createType(DeclaredTypeFactory.java:1)
	at org.eclipse.xtext.common.types.access.binary.BinaryClassMirror.initialize(BinaryClassMirror.java:41)
	at org.eclipse.xtext.common.types.access.TypeResource.doLoad(TypeResource.java:115)
	... 54 more
Caused by: java.lang.NullPointerException
	at org.eclipse.xtext.common.types.access.binary.asm.JvmAnnotationReferenceBuilder$1.visitEnd(JvmAnnotationReferenceBuilder.java:81)
	at org.objectweb.asm.ClassReader.a(Unknown Source)
	at org.objectweb.asm.ClassReader.a(Unknown Source)
	at org.objectweb.asm.ClassReader.a(Unknown Source)
	at org.objectweb.asm.ClassReader.accept(Unknown Source)
	at org.objectweb.asm.ClassReader.accept(Unknown Source)
	at org.eclipse.xtext.common.types.access.binary.asm.JvmDeclaredTypeBuilder.buildType(JvmDeclaredTypeBuilder.java:71)
	at org.eclipse.xtext.common.types.access.impl.DeclaredTypeFactory.doCreateType(DeclaredTypeFactory.java:112)
	at org.eclipse.xtext.common.types.access.impl.DeclaredTypeFactory.createType(DeclaredTypeFactory.java:92)
	... 57 more

At the same time this happens only in the maven compiler. The eclipse compilation succeeds.
Comment 12 Andras Kovi CLA 2014-02-17 03:46:58 EST
The bug is present in 2.5.2, as well, but not in 2.4.3.
Comment 13 Sebastian Zarnekow CLA 2014-02-17 05:37:11 EST
(In reply to Andras Kovi from comment #12)
> The bug is present in 2.5.2, as well, but not in 2.4.3.

Andras,

your stacktrace does not look like a JDT problem. Could you file a ticket against Xtext and provide detailled steps to reproduce?
Comment 14 Andras Kovi CLA 2014-02-17 06:22:45 EST
Thanks Sebastian. See https://bugs.eclipse.org/bugs/show_bug.cgi?id=428340
Comment 15 Eclipse Genie CLA 2019-11-29 09:39:35 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.

If you have further information on the current state of the bug, please add it. 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.