Skip to content

Commit 59f1ee5

Browse files
committed
Query methods in api.Symbols for Java flags
Adds query methods to the public reflection API for querying the JAVA_ENUM and JAVA_ANNOTATION flags. Didn't include JAVA_DEFAULTMETHOD because it does not correspond to a real java classfile flag (just a non-abstract method in an interface), and we want to clean the usage of this flag before adding it to a public API. The flags themselfs are not added to the reflection API. A comment in api/FlagSets.scala says: Q: I have a pretty flag. Can I put it here? A: Only if there's a tree that cannot be built without it. If you want to put a flag here so that it can be tested against, introduce an `isXXX` method in one of the api.Symbols classes instead.
1 parent 76c133d commit 59f1ee5

File tree

4 files changed

+23
-0
lines changed

4 files changed

+23
-0
lines changed

src/reflect/scala/reflect/api/FlagSets.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ trait FlagSets { self: Universe =>
173173
* - the enum's class
174174
* - enum constants
175175
**/
176+
@deprecated("Use `isJavaEnum` on the corresponding symbol instead.", since = "2.11.8")
176177
val ENUM: FlagSet
177178

178179
/** Flag indicating that tree represents a parameter of the primary constructor of some class

src/reflect/scala/reflect/api/Symbols.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,18 @@ trait Symbols { self: Universe =>
504504
*/
505505
def isImplicit: Boolean
506506

507+
/** Does this symbol represent a java enum class or a java enum value?
508+
*
509+
* @group Tests
510+
*/
511+
def isJavaEnum: Boolean
512+
513+
/** Does this symbol represent a java annotation interface?
514+
*
515+
* @group Tests
516+
*/
517+
def isJavaAnnotation: Boolean
518+
507519
/******************* helpers *******************/
508520

509521
/** Provides an alternate if symbol is a NoSymbol.

src/reflect/scala/reflect/internal/Symbols.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
102102
def isPrivateThis = (this hasFlag PRIVATE) && (this hasFlag LOCAL)
103103
def isProtectedThis = (this hasFlag PROTECTED) && (this hasFlag LOCAL)
104104

105+
def isJavaEnum: Boolean = hasJavaEnumFlag
106+
def isJavaAnnotation: Boolean = hasJavaAnnotationFlag
107+
105108
def newNestedSymbol(name: Name, pos: Position, newFlags: Long, isClass: Boolean): Symbol = name match {
106109
case n: TermName => newTermSymbol(n, pos, newFlags)
107110
case n: TypeName => if (isClass) newClassSymbol(n, pos, newFlags) else newNonClassSymbol(n, pos, newFlags)

test/junit/scala/tools/nsc/symtab/FlagsTest.scala

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,11 @@ class FlagsTest {
8686

8787
assertEquals(withFlagMask(AllFlags)(sym.setFlag(lateFlags).flags), lateFlags | lateable)
8888
}
89+
90+
@Test
91+
def javaClassMirrorAnnotationFlag(): Unit = {
92+
import scala.reflect.runtime.universe._
93+
val dep = typeOf[java.lang.Deprecated].typeSymbol
94+
assertTrue(dep.isJavaAnnotation && dep.isJava)
95+
}
8996
}

0 commit comments

Comments
 (0)