score:27
Receiving a java.lang.Class
or instantiating objects with reflection must be done with a mirror and not with types and symbols, which are compile time information for Scala:
scala> val m = runtimeMirror(getClass.getClassLoader)
m: reflect.runtime.universe.Mirror = JavaMirror with ...
scala> m.runtimeClass(typeOf[X.Y].typeSymbol.asClass)
res25: Class[_] = class X$Y
score:6
Assuming tag
is the type tag of the class you want to get the java class for, you can say:
val mirror = tag.mirror
val clazz = mirror.runtimeClass(tag.tpe.typeSymbol.asClass)
Basically the same as sschaef's answer, except you should use the mirror directly from the tag, instead of using the mirror of the current class' classloader. If the current class and the other class which you have the tag for use diferent classloaders, you would have a classloading error in that case, but no error in the way I explained.
The clazz
variable will hold the java class for whatever you're trying to get.
Source: stackoverflow.com
Related Query
- Any way to obtain a Java class from a Scala (2.10) type tag or symbol?
- How to access a Java static method from Scala given a type alias for that class it resides in
- Initialize scala class with self type dependencies from java
- Accessing Scala Type Class from Java code?
- get java class for type tag of Scala case class
- Obtain Java Class from Scala TypeTag in Macros
- Scala - obtaining a class object from a generic type
- Can I use a scala class which implements a java interface from Java?
- Is there any suitable way to generate a [jpg, png etc.] syntax diagram(and/or AST) directly from Scala Parser Combinators?
- Any way to access the type of a Scala Option declaration at runtime using reflection?
- Get source code of any class from within a Java program
- NoClassDefFoundError using Scala class from Java
- How to refer to protected inner class in Scala when inheriting from Java (with byte code only)
- Using Scala type aliases from Java code
- Is there a way in scala to convert from any Map to java.util.Map?
- Calling Java Generic Typed Method from Scala gives a Type mismatch error : Scala
- Scala trait runtime class from type parameter
- Using Scala class defined in package object from Java
- Scala class inheriting from a Java generic class
- Is there a way to get a JSON-Schema from a Scala Case Class hierarchy?
- Implement Java Interface with Raw type from Scala
- Is there a way to treat scala value as constant from java
- implementing java interface with scala class - type issue
- Scala class cant override compare method from Java Interface which extends java.util.comparator
- Shapeless: is there a way to get Hlist type from a case class or a tuple
- scala case class copy method is unseen from java code
- scala macro Unknown type error with java bean class creation
- Implementing a Java interface featuring recursive type and bounds from Scala
- Is there any way to get Key/Value type from a map in Scala?
- Scala instantiate a Concrete Class From Generic Type
More Query from same tag
- Scala/akka Pattern Matching a Sequence of Futures pipelined to a actor message
- Type inference for generic strategy pattern in Scala
- -Ywarn-unused-import triggering on play routes file
- Comparison in Scala
- How to update a sequence using Scala map method?
- Log4j creating logs in multiple nodes. Want to create a single log in one node
- How to print contents of a BSONDocument
- Usage of _ in scala lambda functions
- DateTime does not equal itself after unserialization
- Race condition appending to mutable Map in future
- Scala Parsing a large XML element
- Deserialize JSON string into List[Map[String, Any]] in Scala
- Spark Scala: Count Consecutive Months
- Extractor composition with dependent sub-patterns
- Why does this Scalaz 7 enumerator leak memory?
- Child actor Termination validation
- How to get the coefficients of the best logistic regression in a spark-ml CrossValidatorModel?
- Custom task in multi-project build in SBT build
- Scala Slick: Issues with groupBy and missing shapes
- What's the difference of the Akka's Actor with Scala's Actor model
- How to find |>>> operator in Scala/Play! API
- How to print all values of tuple in Scala?
- What are the open source scala-based DSL projects out there?
- How to perform arithmetic operations on dataframe in spark using select and map?
- How to return Double with map
- Play Framework 2.2.0 [scala] - WebSocket.async vs WebSocket.using[T]
- how use import a scala project created with sbt in eclipse?
- How to enable tab completion in Scala's MainGenericRunner REPL (interactive interpreter)?
- Play-Framework: Cannot parse parameter companyId as BSONObjectID: wrong ObjectId
- Any way of simplifying this Scala code (pattern matching)?