Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Test whether a method is defined

I use scala reflection to get information on a trait defined in my Model Class. I can easily get the members of this class doing this:

ru.runtimeMirror(myClassLoader).staticClass("model.Model").typeSignature.members

but how to know whether these members are defined or not, ie if they have an implementation or not ?

like image 451
Mathieu Avatar asked May 28 '13 13:05

Mathieu


People also ask

How do you test a method in Java?

Each test method should have an "annotation" @ Test . This tells the JUnit test framework that this is an executable test method. To run the tests, select the project with the right-mouse button and click Test. Let's add functionality for adding and subtracting to the test class.

What is observation test method?

Abstract. Observation method is described as a method to observe and describe the behavior of a subject and it involves the basic technique of simply watching the phenomena until some hunch or insight is gained.

How do you check if a method is in a class python?

The Python's isinstance() function checks whether the object or variable is an instance of the specified class type or data type. For example, isinstance(name, str) checks if name is an instance of a class str .

How do you check whether a method is called in Mockito?

Mockito verify() simple example verify(mockList, times(1)). size(); If we want to make sure a method is called but we don't care about the argument, then we can use ArgumentMatchers with verify method. verify(mockList).


1 Answers

Wow that's an oversight! I've submitted a pull request targetting 2.11.0 (https://github.com/scala/scala/pull/2612) that adds Symbol.isAbstract.

Since this is a new API, due to compatibility restrictions it cannot make it into 2.10.x, so in the meanwhile please use the following workaround:

00:01 ~/Projects/210x (2.10.x)$ scala
Welcome to Scala version 2.10.3-20130527-133534-9b310bc906 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_45).
Type in expressions to have them evaluated.
Type :help for more information.

scala> trait C { def foo: Int; def bar = 2 }
defined trait C

scala> val foo = typeOf[C].declarations.toList.apply(1)
foo: reflect.runtime.universe.Symbol = method foo

scala> val bar = typeOf[C].declarations.toList.apply(2)
bar: reflect.runtime.universe.Symbol = method bar

scala> def isDeferred(sym: Symbol) = sym
         .asInstanceOf[scala.reflect.internal.Symbols#Symbol]
         .hasFlag(scala.reflect.internal.Flags.DEFERRED)
isDeferred: (sym: reflect.runtime.universe.Symbol)Boolean

scala> isDeferred(foo)
res2: Boolean = true

scala> isDeferred(bar)
res3: Boolean = false
like image 198
Eugene Burmako Avatar answered Oct 15 '22 18:10

Eugene Burmako