This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Annotations on fields

No replies
pkolaczk
Joined: 2010-01-14,
User offline. Last seen 2 years 38 weeks ago.

Hi,

I'm writing a circuit simulation program in Scala and I got stuck...

I have a Java annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.FIELD} )
public @interface ModelEquation {
}

And then I'm trying to use it on the val or def.

case class Resistor(id: Id, resistance: Expression[DoubleWithError])
extends ReflectiveModel {

val t1 = Terminal(id.subId("1"))
val t2 = Terminal(id.subId("2"))

@ModelEquation
val eq1 = t1.current * resistance + t1.voltage - t2.voltage

@ModelEquation
val eq2 = t1.current + t2.current
}

Now I want to read at runtime which equations are given by this model:

val methods =
this.getClass.getMethods.filter(_.isAnnotationPresent(classOf[ModelEquation]))

I get an empty list. When I change those fields from vals to defs, it
works, giving the annotated methods. What happens to the annotation on a
val field? Where can I find it at runtime? I want to make my
@ModelEquation annotation work for both defs and vals.

Regards,
Piotr Kołaczkowski

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland