New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Java annotations with enum parameters are reported as errors #2764
Comments
Imported From: https://issues.scala-lang.org/browse/SI-2764?orig=1
|
Alex (alex.savitsky) said: |
@milessabin said: Enum.java public enum Enum {
VALUE;
} Ann.java public @interface Ann {
Enum value();
} Use.scala class Use {
@Ann(Enum.VALUE)
def foo {}
} And in this case an error is reported by command line scalac, src/bug2764/Use.scala:4: error: annotation argument needs to be a constant; found: Enum.VALUE
@Ann(Enum.VALUE)
^
one error found So it looks like the Eclipse vs. command-line build issue might be a red-herring. In both cases scalac is seeing a UniqueConstantType in a context where it's expecting to see a Literal (in Typer.typedAnnotation). |
@lrytz said: The classfile parser does it: sym.setInfo(if ((jflags & JAVA_ACC_ENUM) == 0) info else mkConstantType(Constant(sym))) |
@milessabin said: Anyhow, reassigning to the the compiler. |
Thomas Scheiblauer (tom) said: |
Thomas Scheiblauer (tom) said: |
Thomas Scheiblauer (tom) said: |
Galder (galderz) said: Any idea if this will be fixed for next Scala 2.8 beta? |
@lrytz said: @galderz: Yes, that's right, I'm aware that the issue also applies to java static final constants. The problem is that I really don't know an easy way to fix this. For enumerations, the java source parser would need to add some additional modifier to the trees of the enum constants (fields) to mark them as enum values. That way the type checker could assign a constant type. For static constants, it's even worse: our current java parser does not look at definitions at all, it only parses declarations. So the value of the static final field is just skipped by the parser. It's not possible to know the value of the static final field when type checking an annotation that uses it. |
Thomas Scheiblauer (tom) said: |
@milessabin said: Please try and put together a minimal self-contained non-mavenized Eclipse project which illustrates the problem. If you can verify that you only see the report in the Eclipse Scala editor (ie. a command line build succeeds, and nothing shows in the Eclipse Problems view), then please open a new ticket, assigned to the IDE with the project attached. |
Bram Bouwens (bbouwens) said: |
dubby said: But i cannot create the Scaladoc of my project (using Scala 2.8.0) - getting the known "annotation argument needs to be a constant"-error , because Scaladoc also seems to be "too" strict. Can i tell the Scaladoc to ignore this error, like the compiler does? |
Alan (alanpog) said: |
pdinklag said: I can only second dubby, this is a real problem. It's basically impossible to use custom annotations with parameters, because not even constants can be used (as mentioned already). You have to hardcode literal values at the moment, and that cannot be a solution. |
Martin Krischik (krischik) said: Maven and IntelliJ IDEA work fine while ScalaDoc and NetBeans produce the mentioned error. In Netbeans I just ignore it but for ScalaDoc that is not possible I tried to read the comments above but still fail to see how it can be that one compile technique (maven) produces good output while other don't. |
@lrytz said:
Maven / IDEA work because they happen to compile the Java files containing the enums (or constants) first, and then compile the Scala files against the bytecode. Try doing the same for !ScalaDoc (compile the Java files first and put the output folder in !ScalaDoc's classpath). Concerning the fix, as I mentioned earlier, unfortunately I don't think there will be a solution anytime soon. |
Martin Krischik (krischik) said: And let me check: SET CLASSPATH=%[CLASSPATH];%[Repository]\javax\javaee-api\6.0\javaee-api-6.0.jar Yep it is on the classpath as well. |
@lrytz said:
|
Florian Hars (florian) said: |
heapifyman said (edited on May 23, 2011 11:09:57 AM UTC): Without experimental extensions my test project compiles fine both in Eclipse and on the command line using sbt. |
Jan Berkel (jberkel) said: class MyProject(info: ProjectInfo) extends DefaultProject(info) {
override def compileOrder = CompileOrder.JavaThenScala
} |
Doug Donohoe (donohoe) said: What is odd is that IntelliJ is fine using annotations with enums compiled outside of the project. However, it fails with an Annotation I have defined in the same project that is using that annotation. Based on the comments above, it seems that compile order matters and perhaps this could be an Intellij bug? |
Dmitry Stropaloff (h8) said: |
Andreas Joseph Krogh (andreak) said: object F {
final val a = ""
}
class A {
@MyAnnotation(name = F.a)
var name = "fish"
} So having final val works |
Andreas Joseph Krogh (andreak) said: object F {
final val a: String = ""
}
class A {
@MyAnnotation(name = F.a)
var name = "fish"
} results in: error: annotation argument needs to be a constant; found: F.A
@MyAnnotation(name = F.a) So the type-annotation :String breaks the whole thing, very strange... |
@paulp said:
...as specified in SLS 4.1, "Value Declarations and Definitions". |
Andreas Joseph Krogh (andreak) said: But, to the average developer: val a = "hi" and val a: String = "hi" are the same thing, everything else is unexpected and comes as a surprise. |
Taylor Leese (tleese22) said: |
Tomer Gabel (holograph) said: |
@paulp said: |
Philip Kster (phkoester) said: Now my projects don't compile any longer and I'm stuck. This bug hasn't been resolved in years. Not knowing anything about the internal workings of the Scala compiler, this looks like a comparably easy thing to fix. This is a major problem, not a minor one! I mean, if I can't use enums in annotations, then I cannot use this language. It's that simple. I'll put my Scala efforts on hold until this is fixed. In theory Scala might be great and all, but in practice there are new problems every week. If everything works great in Eclipse, the Maven plugin would not work, or vice versa. I want to code and not spend 90 % of my spare time maintaining my projects. I'm really frustrated. |
@paulp said: |
Philip Kster (phkoester) said (edited on May 11, 2012 4:29:55 PM UTC): Maybe that's a good oppurtunity to visit my family then ... :) |
@paulp said: It's fixed in 8075672308 for trunk. I'll look at backporting it for 2.9.3. |
G. Ralph Kuntz, MD (grkuntzmd) said: I've been trying to use Scala in a JAX-RS project, but some of our custom annotations require Java enum values. It's hard to convince other developers of how great Scala is if I can't use it in one of our major projects :-). |
Alexey Aksenov (ezhik) said (edited on May 11, 2012 6:14:46 PM UTC):
There is no URL because this part of code is not public.
Cache.web as simple as object Cache { King Regards |
@paulp said: |
Philip Kster (phkoester) said: |
@adriaanm said: |
@adriaanm said: |
Enum.java
Ann.java
Use.scala
Compiling them together with scalac gives:
Compiling java first, then scala works.
The same issue arises for static constants in Java source files.
https://lampsvn.epfl.ch/trac/scala/ticket/2764#comment:12
The text was updated successfully, but these errors were encountered: