From ac20cbb480158d40a074080dbef499700716a609 Mon Sep 17 00:00:00 2001 From: mihaylov Date: Thu, 6 Jul 2006 10:10:23 +0000 Subject: Added parsing of Java annotations applied to de... Added parsing of Java annotations applied to definitions in classfiles --- .../scala/tools/nsc/ast/TreePrinters.scala | 4 +- .../scala/tools/nsc/backend/jvm/GenJVM.scala | 4 +- .../scala/tools/nsc/symtab/Constants.scala | 4 +- src/compiler/scala/tools/nsc/symtab/Symbols.scala | 2 +- .../nsc/symtab/classfile/ClassfileParser.scala | 53 +++++++++++----------- .../scala/tools/nsc/typechecker/Typers.scala | 2 +- 6 files changed, 36 insertions(+), 33 deletions(-) diff --git a/src/compiler/scala/tools/nsc/ast/TreePrinters.scala b/src/compiler/scala/tools/nsc/ast/TreePrinters.scala index 828d886efc..137de920b9 100644 --- a/src/compiler/scala/tools/nsc/ast/TreePrinters.scala +++ b/src/compiler/scala/tools/nsc/ast/TreePrinters.scala @@ -111,10 +111,10 @@ abstract class TreePrinters { if (!args.isEmpty) str.append(args.mkString("(", ",", ")")) if (!nvPairs.isEmpty) - for (val Pair(Pair(sym, value), index) <- nvPairs.zipWithIndex) { + for (val Pair(Pair(name, value), index) <- nvPairs.zipWithIndex) { if (index > 0) str.append(", ") - str.append(sym.name).append(" = ").append(value) + str.append(name).append(" = ").append(value) } str.toString } diff --git a/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala b/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala index 1995986e9e..ed808580fc 100644 --- a/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala +++ b/src/compiler/scala/tools/nsc/backend/jvm/GenJVM.scala @@ -273,8 +273,8 @@ abstract class GenJVM extends SubComponent { buf.putShort(cpool.addUtf8("value").toShort); emitElement(consts.head); } - for (val Pair(symbol, value) <- nvPairs) { - buf.putShort(cpool.addUtf8(symbol.name.toString()).toShort) + for (val Pair(name, value) <- nvPairs) { + buf.putShort(cpool.addUtf8(name.toString()).toShort) emitElement(value) } } diff --git a/src/compiler/scala/tools/nsc/symtab/Constants.scala b/src/compiler/scala/tools/nsc/symtab/Constants.scala index 7e4be15fc5..38fc822b18 100644 --- a/src/compiler/scala/tools/nsc/symtab/Constants.scala +++ b/src/compiler/scala/tools/nsc/symtab/Constants.scala @@ -194,6 +194,8 @@ trait Constants requires SymbolTable { class ArrayConstant(override val arrayValue: Array[Constant], override val tpe: Type) - extends Constant(arrayValue); + extends Constant(arrayValue) { + override def toString() = arrayValue.mkString("Constant(", "," , ")") + } } diff --git a/src/compiler/scala/tools/nsc/symtab/Symbols.scala b/src/compiler/scala/tools/nsc/symtab/Symbols.scala index 816df859ba..9ab2d8aa39 100644 --- a/src/compiler/scala/tools/nsc/symtab/Symbols.scala +++ b/src/compiler/scala/tools/nsc/symtab/Symbols.scala @@ -20,7 +20,7 @@ trait Symbols requires SymbolTable { var typeSymbolCount = 0 var classSymbolCount = 0 - type AttrInfo = Triple[Type, List[Constant], List[Pair[Symbol,Constant]]] + type AttrInfo = Triple[Type, List[Constant], List[Pair[Name,Constant]]] val emptySymbolArray = new Array[Symbol](0) diff --git a/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala b/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala index 6f95151693..6891b87fe3 100644 --- a/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala +++ b/src/compiler/scala/tools/nsc/symtab/classfile/ClassfileParser.scala @@ -19,12 +19,11 @@ package scala.tools.nsc.symtab.classfile import scala.tools.nsc.util.Position import scala.tools.nsc.io.{AbstractFile, AbstractFileReader} -import scala.collection.mutable.ListBuffer +import scala.collection.mutable.{ListBuffer, ArrayBuffer} import scala.collection.immutable.{Map, ListMap} import java.io.IOException -class AnnotationDefault extends Attribute; abstract class ClassfileParser { def sourcePath : AbstractFile = null @@ -529,55 +528,57 @@ abstract class ClassfileParser { Triple(definitions.AnnotationDefaultAttr.tpe, List(), List()) :: sym.attributes in.skip(attrLen) case nme.RuntimeAnnotationATTR => - //parseAnnotations(attrLen) - in.skip(attrLen) + parseAnnotations(attrLen) + if (settings.debug.value) + global.inform("" + sym + "; attributes = " + sym.attributes) case _ => in.skip(attrLen) } } - def parseTaggedConstant(): Any = { + def parseTaggedConstant(): Constant = { val tag = in.nextByte val index = in.nextChar tag match { - case STRING_TAG => pool.getName(index).toString() - case BOOL_TAG => pool.getConstant(index).intValue != 0 - case BYTE_TAG => pool.getConstant(index).byteValue - case CHAR_TAG => pool.getConstant(index).charValue - case SHORT_TAG => pool.getConstant(index).shortValue - case INT_TAG => pool.getConstant(index).intValue - case LONG_TAG => pool.getConstant(index).longValue - case FLOAT_TAG => pool.getConstant(index).floatValue - case DOUBLE_TAG => pool.getConstant(index).doubleValue - case CLASS_TAG => pool.getType(index).toString() + ".class" + case STRING_TAG => Constant(pool.getName(index).toString()) + case BOOL_TAG => pool.getConstant(index) + case BYTE_TAG => pool.getConstant(index) + case CHAR_TAG => pool.getConstant(index) + case SHORT_TAG => pool.getConstant(index) + case INT_TAG => pool.getConstant(index) + case LONG_TAG => pool.getConstant(index) + case FLOAT_TAG => pool.getConstant(index) + case DOUBLE_TAG => pool.getConstant(index) + case CLASS_TAG => Constant(pool.getType(index)) case ENUM_TAG => - pool.getType(index).toString() + "." + pool.getName(in.nextChar) + val t = pool.getType(index); + val n = pool.getName(in.nextChar); + val s = t.symbol.linkedModule.info.decls.lookup(n) + assert (s != NoSymbol, "while processing " + in.file + ": " + t + "." + n + ": " + t.decls) + Constant(s) case ARRAY_TAG => - val arr = new ListBuffer[Any]() + val arr = new ArrayBuffer[Constant]() for (val i <- 0 until index) { arr += parseTaggedConstant() } - arr.toList.mkString("{", ",", "}") + new ArrayConstant(arr.toArray, + appliedType(definitions.ArrayClass.typeConstructor, List(arr(0).tpe))) } } def parseAnnotations(len: Int): Unit = { - val buf = new StringBuffer() val nAttr = in.nextChar for (val n <- 0 until nAttr) { val attrNameIndex = in.nextChar val attrType = pool.getType(attrNameIndex) - buf.append("@").append(attrType.toString()).append("(") val nargs = in.nextChar + val nvpairs = new ListBuffer[Pair[Name,Constant]] for (val i <- 0 until nargs) { - if (i > 0) buf.append(", ") val name = pool.getName(in.nextChar) - buf.append(name).append(" = ") - val value = parseTaggedConstant() - buf.append(value) + nvpairs += Pair(name, parseTaggedConstant()) } - buf.append(")") + sym.attributes = Triple(attrType, List(), nvpairs.toList) :: sym.attributes } - global.informProgress("parsed attribute " + buf) } + def parseInnerClasses(): unit = { for (val i <- 0 until in.nextChar) { val innerIndex = in.nextChar diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 7d2f0f38e2..1e663af07f 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -1437,7 +1437,7 @@ trait Typers requires Analyzer { null } else { names -= sym - Pair(sym, getConstant(typed(rhs, mode | CONSTmode, + Pair(sym.name, getConstant(typed(rhs, mode | CONSTmode, sym.tpe.resultType))) } } -- cgit v1.2.3