summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-09-14 21:31:21 +0200
committerEugene Burmako <xeno.by@gmail.com>2012-09-15 02:20:44 +0200
commit39486b03e1e044fcf3256e1d9293dccd0871edf9 (patch)
tree1fd266e78572af232a3763ffe0ab5f2df96c4aa5 /src
parent24580ac84242679619d27f20258078dd012c120a (diff)
downloadscala-39486b03e1e044fcf3256e1d9293dccd0871edf9.tar.gz
scala-39486b03e1e044fcf3256e1d9293dccd0871edf9.tar.bz2
scala-39486b03e1e044fcf3256e1d9293dccd0871edf9.zip
SI-6356 reflection now supports Java annotations
Except for one thingie: java enums are currently not understood by Scala reflection, hence they aren't yet supported in annotations.
Diffstat (limited to 'src')
-rw-r--r--src/partest/scala/tools/partest/nest/RunnerManager.scala1
-rw-r--r--src/reflect/scala/reflect/internal/AnnotationInfos.scala16
-rw-r--r--src/reflect/scala/reflect/runtime/JavaMirrors.scala33
3 files changed, 43 insertions, 7 deletions
diff --git a/src/partest/scala/tools/partest/nest/RunnerManager.scala b/src/partest/scala/tools/partest/nest/RunnerManager.scala
index 376e0e9bdb..4961424e1b 100644
--- a/src/partest/scala/tools/partest/nest/RunnerManager.scala
+++ b/src/partest/scala/tools/partest/nest/RunnerManager.scala
@@ -217,6 +217,7 @@ class RunnerManager(kind: String, val fileManager: FileManager, params: TestRunP
"-Dpartest.output="+outDir.getAbsolutePath,
"-Dpartest.lib="+LATEST_LIB,
"-Dpartest.reflect="+LATEST_REFLECT,
+ "-Dpartest.comp="+LATEST_COMP,
"-Dpartest.cwd="+outDir.getParent,
"-Dpartest.test-path="+testFullPath,
"-Dpartest.testname="+fileBase,
diff --git a/src/reflect/scala/reflect/internal/AnnotationInfos.scala b/src/reflect/scala/reflect/internal/AnnotationInfos.scala
index 8853b872c0..3bd7f4f4fa 100644
--- a/src/reflect/scala/reflect/internal/AnnotationInfos.scala
+++ b/src/reflect/scala/reflect/internal/AnnotationInfos.scala
@@ -65,6 +65,7 @@ trait AnnotationInfos extends api.Annotations { self: SymbolTable =>
*/
abstract class ClassfileAnnotArg extends Product
implicit val JavaArgumentTag = ClassTag[ClassfileAnnotArg](classOf[ClassfileAnnotArg])
+ case object UnmappableAnnotArg extends ClassfileAnnotArg
/** Represents a compile-time Constant (`Boolean`, `Byte`, `Short`,
* `Char`, `Int`, `Long`, `Float`, `Double`, `String`, `java.lang.Class` or
@@ -173,11 +174,14 @@ trait AnnotationInfos extends api.Annotations { self: SymbolTable =>
this
}
- override def toString = (
- atp +
- (if (!args.isEmpty) args.mkString("(", ", ", ")") else "") +
- (if (!assocs.isEmpty) (assocs map { case (x, y) => x+" = "+y } mkString ("(", ", ", ")")) else "")
- )
+ override def toString = completeAnnotationToString(this)
+ }
+
+ private[scala] def completeAnnotationToString(annInfo: AnnotationInfo) = {
+ import annInfo._
+ val s_args = if (!args.isEmpty) args.mkString("(", ", ", ")") else ""
+ val s_assocs = if (!assocs.isEmpty) (assocs map { case (x, y) => x+" = "+y } mkString ("(", ", ", ")")) else ""
+ s"${atp}${s_args}${s_assocs}"
}
/** Symbol annotations parsed in `Namer` (typeCompleter of
@@ -215,7 +219,7 @@ trait AnnotationInfos extends api.Annotations { self: SymbolTable =>
*
* `assocs` stores arguments to classfile annotations as name-value pairs.
*/
- sealed abstract class AnnotationInfo extends AnnotationApi {
+ abstract class AnnotationInfo extends AnnotationApi {
def atp: Type
def args: List[Tree]
def assocs: List[(Name, ClassfileAnnotArg)]
diff --git a/src/reflect/scala/reflect/runtime/JavaMirrors.scala b/src/reflect/scala/reflect/runtime/JavaMirrors.scala
index be2661149a..6977b43d73 100644
--- a/src/reflect/scala/reflect/runtime/JavaMirrors.scala
+++ b/src/reflect/scala/reflect/runtime/JavaMirrors.scala
@@ -9,6 +9,7 @@ import java.lang.reflect.{
Method => jMethod, Constructor => jConstructor, Modifier => jModifier, Field => jField,
Member => jMember, Type => jType, TypeVariable => jTypeVariable, Array => jArray,
GenericDeclaration, GenericArrayType, ParameterizedType, WildcardType, AnnotatedElement }
+import java.lang.annotation.{Annotation => jAnnotation}
import java.io.IOException
import internal.MissingRequirementError
import internal.pickling.ByteCodecs
@@ -573,7 +574,36 @@ trait JavaMirrors extends internal.SymbolTable with api.JavaUniverse { self: Sym
* Note: If `sym` is a method or constructor, its parameter annotations are copied as well.
*/
private def copyAnnotations(sym: Symbol, jann: AnnotatedElement) {
- // to do: implement
+ case class JavaAnnotationProxy(jann: jAnnotation) extends AnnotationInfo {
+ override val atp: Type = classToScala(jann.annotationType).toType
+ override val args: List[Tree] = Nil
+ override def original: Tree = EmptyTree
+ override def setOriginal(t: Tree): this.type = throw new Exception("setOriginal inapplicable for " + this)
+ override def pos: Position = NoPosition
+ override def setPos(pos: Position): this.type = throw new Exception("setPos inapplicable for " + this)
+ override def toString = completeAnnotationToString(this)
+ override def assocs: List[(Name, ClassfileAnnotArg)] =
+ // todo. find out the exact order of assocs as they are written in the class file
+ // currently I'm simply sorting the methods to guarantee stability of the output
+ jann.annotationType.getDeclaredMethods.sortBy(_.getName).toList map (m => {
+ def enumToSymbol(enum: Enum[_]): Symbol =
+ classToScala(enum.getClass).typeSignature.declaration(newTermName(enum.name))
+
+ def toAnnotArg(value: Any, schema: jClass[_]): ClassfileAnnotArg = schema match {
+ case primitive if primitive.isPrimitive => LiteralAnnotArg(Constant(value))
+ case string if string == classOf[String] => LiteralAnnotArg(Constant(value))
+ case clazz if clazz == classOf[jClass[_]] => LiteralAnnotArg(Constant(classToScala(value.asInstanceOf[jClass[_]]).toType))
+ case enum if enum.isEnum => LiteralAnnotArg(Constant(enumToSymbol(value.asInstanceOf[Enum[_]])))
+ case array if array.isArray => ArrayAnnotArg(value.asInstanceOf[Array[_]] map (x => toAnnotArg(x, ScalaRunTime.arrayElementClass(array))))
+ case ann if ann.isAnnotation => NestedAnnotArg(JavaAnnotationProxy(value.asInstanceOf[jAnnotation]))
+ case _ => UnmappableAnnotArg
+ }
+
+ newTermName(m.getName) -> toAnnotArg(m.invoke(jann), m.getReturnType)
+ })
+ }
+
+ sym setAnnotations (jann.getAnnotations map JavaAnnotationProxy).toList
}
/**
@@ -612,6 +642,7 @@ trait JavaMirrors extends internal.SymbolTable with api.JavaUniverse { self: Sym
}
override def complete(sym: Symbol): Unit = {
+ if (jclazz.isEnum) throw new ScalaReflectionException("implementation restriction: Java enums are not supported")
load(sym)
completeRest()
}