New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
object.getClass is an error in annotation #2453
Comments
Imported From: https://issues.scala-lang.org/browse/SI-2453?orig=1 |
Paul LaCrosse (pjlacrosse) said: Using the Oracle Berkeley DB for Java, I get: "annotation argument needs to be a constant; found: ONE_TO_ONE{}" when using this annotation:
where "ONE_TO_ONE" was imported as: Interestingly, this problem shows up only in the Eclipse plugin manifesting as the error icon against the line in the editor, but not under the problems views. It also does not generate an error from a command-line scalac compilation. This is using the 2.8 nightly from 20091021. |
Paul LaCrosse (pjlacrosse) said: |
@lrytz said: the question is which object o { def x = 1 } you get
class o { static int x() { return o$$.MODULE$$.x(); } }
class o$$ {
o$$ MODULE$$ = new o$$;
int x() { return 1; }
} I don't know what However, calling scala> o.getClass
res0: java.lang.Class[_] = class o$$ For nested objects, the additional classfile with static forwarders is not generated. Instead, a method is added to the containing class: class A { object p { def x = 1 } } generates
class A {
A$$p$$ p$$module = null;
A$$p$$ p() {
if (p$$module == null) { p$$module = new A$$p$$ };
return A$$p$$;
}
}
class A$$p$$ { int x() { return 1; } } So in this case there doesn't exist any static methods which could be called. Again, scala> (new A).p.getClass
res0: java.lang.Class[_] = class A$$p$$ We cannot allow I could allow |
Francois Armand (fanf) said: Regarding the general problem, without your explanations I would have say "load the class with the static forwarder", but it doesn't seems to be possible :) I don't know what is the best second choice. Or perhaps, there is just not enought use cases to decide (as even mine isn't relevant anymore), and you could close the bug for now, waiting to have better example to know what to do. Sorry for the time spend, and thanks for the explanations. |
Hongxin Liang (honnix) said: As an experiment, I tried to combine scala with powermock, but I met exactly the same problem described by this issue. In order to mock a static class (object in scala), I need to write annotation @PrepareForTest(TheStaticClass.class), but this does not work as described. My question is that whether there is a plan to fix it or this can only be left as one limitation of scala? |
Iskra Vitaly (iskra.vitaly) said:
In some cases users of a object need to pass first class somewhere, in some second. |
Hongxin Liang (honnix) said: |
Francois Armand (fanf) said: |
@jsuereth said: |
@retronym said: |
@retronym said: scala> object O
defined module O
scala> def impl[T: c.WeakTypeTag](c: Context): c.Expr[Class[_]] = {import c.universe._; c.Expr[Class[_]](Literal(Constant(weakTypeOf[T])))}
impl: [T](c: scala.reflect.macros.Context)(implicit evidence$1: c.WeakTypeTag[T])c.Expr[Class[_]]
scala> def moduleClassOf[T <: Singleton]: Class[_] = macro impl[T]
moduleClassOf: [T <: Singleton]=> Class[_]
scala> moduleClassOf[O.type]
res5: Class[O.type] = class O$ |
tenstriker said: |
@soc said: Should we come up with a way to refer to a companion symbol type as a constant (like |
For the record, I run into that issue when using case objects with Jackson annotations. Jason's example didn't work out-of-the-box (it dates from 2013), but needed some small adaptations. Here is what is needed when using more recent version of the macro lib. import scala.reflect.macros.whitebox.Context
import scala.language.experimental.macros
object ObjectClassType {
def impl[T: c.WeakTypeTag](c: Context): c.Expr[Class[T]] = {
import c.universe._
c.Expr[Class[T]](Literal(Constant(weakTypeOf[T])))
}
def classOf[T <: Singleton]: Class[T] = macro impl[T]
}
|
scala lib version: 2.8.0-SNAPSHOT (timestamp: 20091006.003811 buildNumber: 232)
I can't pass the class of an object to a (Java) annotation (for ex, this one: https://svn.apache.org/repos/asf/directory/apacheds/trunk/core-integ/src/main/java/org/apache/directory/server/core/integ/annotations/Factory.java )
The Factory annotation expects a Factory implementation, which is just class with a static method "newInstance", and so is done in Scala with an object.
In Java, I write:
@Factory( MyFactory.class )
In scala,
@Factory( classOf[MyFactory] )
won't work asMyFactory
is an object and@Factory( MyFactory.getClass )
doesn't compile due to the error:"annotation argument needs to be a constant; found: MyFactory.getClass"
I don't see any other way to pass the object's class as the annotation parameter.
The text was updated successfully, but these errors were encountered: