Skip to content

Commit

Permalink
Recognize java enums as constants from source.
Browse files Browse the repository at this point in the history
Fixed up one of the mismatches between how java source is modeled
and how java bytecode is modeled.  We should get the rest of them
too.  Closes SI-2764.
  • Loading branch information
paulp committed May 11, 2012
1 parent 2422b06 commit 8075672
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 2 deletions.
1 change: 0 additions & 1 deletion src/compiler/scala/reflect/internal/StdNames.scala
Expand Up @@ -766,7 +766,6 @@ trait StdNames {
object fulltpnme extends TypeNames { object fulltpnme extends TypeNames {
val RuntimeNothing: NameType = "scala.runtime.Nothing$" val RuntimeNothing: NameType = "scala.runtime.Nothing$"
val RuntimeNull: NameType = "scala.runtime.Null$" val RuntimeNull: NameType = "scala.runtime.Null$"
val JavaLangEnum: NameType = "java.lang.Enum"
} }


/** Java binary names, like scala/runtime/Nothing$. /** Java binary names, like scala/runtime/Nothing$.
Expand Down
5 changes: 4 additions & 1 deletion src/compiler/scala/tools/nsc/javac/JavaParsers.scala
Expand Up @@ -872,7 +872,10 @@ trait JavaParsers extends ast.parser.ParsersCommon with JavaScanners {
skipAhead() skipAhead()
accept(RBRACE) accept(RBRACE)
} }
ValDef(Modifiers(Flags.JAVA | Flags.STATIC), name, enumType, blankExpr) // The STABLE flag is to signal to namer that this was read from a
// java enum, and so should be given a Constant type (thereby making
// it usable in annotations.)
ValDef(Modifiers(Flags.STABLE | Flags.JAVA | Flags.STATIC), name, enumType, blankExpr)
} }
} }


Expand Down
8 changes: 8 additions & 0 deletions src/compiler/scala/tools/nsc/typechecker/Namers.scala
Expand Up @@ -567,7 +567,15 @@ trait Namers extends MethodSynthesis {
assignAndEnterFinishedSymbol(tree) assignAndEnterFinishedSymbol(tree)
else else
enterGetterSetter(tree) enterGetterSetter(tree)

// When java enums are read from bytecode, they are known to have
// constant types by the jvm flag and assigned accordingly. When
// they are read from source, the java parser marks them with the
// STABLE flag, and now we receive that signal.
if (tree.symbol hasAllFlags STABLE | JAVA)
tree.symbol setInfo ConstantType(Constant(tree.symbol))
} }

def enterLazyVal(tree: ValDef, lazyAccessor: Symbol): TermSymbol = { def enterLazyVal(tree: ValDef, lazyAccessor: Symbol): TermSymbol = {
// If the owner is not a class, this is a lazy val from a method, // If the owner is not a class, this is a lazy val from a method,
// with no associated field. It has an accessor with $lzy appended to its name and // with no associated field. It has an accessor with $lzy appended to its name and
Expand Down
5 changes: 5 additions & 0 deletions test/files/pos/t2764/Ann.java
@@ -0,0 +1,5 @@
package bippy;

public @interface Ann {
Enum value();
}
5 changes: 5 additions & 0 deletions test/files/pos/t2764/Enum.java
@@ -0,0 +1,5 @@
package bippy;

public enum Enum {
VALUE;
}
6 changes: 6 additions & 0 deletions test/files/pos/t2764/Use.scala
@@ -0,0 +1,6 @@
package bippy

class Use {
@Ann(Enum.VALUE)
def foo {}
}

1 comment on commit 8075672

@lrytz
Copy link
Member

@lrytz lrytz commented on 8075672 May 12, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks paul for fixing this. for the second half of the ticket (using java static constants in annotations) i created a new issue (SI-5791).

Please sign in to comment.