score:5
if you are modifying the behaviour of a class for which you don't have the source code, then you could try looking into aspect-oriented programming.
aspects allow you to 'wrap' a method call so that you can log information, or modify the input parameters or return values, or even just replace the method call altogether.
if all you want to do is log information, then this would be a good way to go. however, aspects can lead to code which is very hard to understand and follow if you use them everywhere, so be sure that it fits your use case.
you will need to define a pointcut for the method you are interested in.
the scala-ide uses aspects to weave code into the jdt, it uses aspectj, so it does work with scala.
score:0
i guess you could do it using byte code manipulation. powermock allows to mock constructors, so i'd guess the same technique could be used to replace the byte code of base with something that does whatever you need to get done.
this approach of course will:
- be extremely confusing for anybody being not aware of it.
- fail if you don't have the classloaders under your control.
- might break legal constraints
- is just generally a bad idea for production code.
score:1
you mean like this?
class notsubclass {
val base = new base
def somemethod = base somemethod
}
score:2
if the method belongs to an interface you can reimplement that and wrap base. otherwise you are out of luck.
Source: stackoverflow.com
Related Query
- In Scala, other than inheritance is there a technique to know when a base class method has been called?
- Is there a way to know through inheritance (or other way) when a class defines the .map function in Scala?
- Why does Scala complain about illegal inheritance when there are raw types in the class hierarchy?
- Is there a way for a Scala base class to get informed when all the derived classes have been constructed? (guess not)
- Is there a way to get a warning when a Scala Value Class needs to become instantiated?
- Are there any Scala template engines other than scalate?
- java.lang.RuntimeException: There is no started application error, when testing a class from scala worksheet
- Is there a way to create an instance of a generic type in Scala other than reflection
- Calling methods from each other when using Abstact Class and Factory in Scala (Companion Object)
- Reassignment to val error when var member of base class in scala
- Scala mock : call Instance of spy mock when other class Instance is initiated inside the object
- Is there a way to export csv or other files in spark 3.0.1 using scala with name different than part*?
- Isn't there any other way to convert bson objects to case class and the vice-versa rather than the implicit conversion in scala?
- How to update a mongo record using Rogue with MongoCaseClassField when case class contains a scala Enumeration
- Scala case class inheritance
- Scala pattern matching on sequences other than Lists
- Does Scala scale better than other JVM languages?
- Scala - No TypeTag Available Exception when using case class to try to get TypeTag?
- Scala programmer - "should there be one obvious way to do it" or "more than one way to do it"?
- Is there a way to "enrich" a Scala class without wrapping the code into another object?
- Is there an efficiency penalty when using Scala inner functions within non-tail recursive functions?
- scala - parse json of more than 22 elements into case class
- java.lang.NoClassDefFoundError: Could not initialize class when launching spark job via spark-submit in scala code
- How to refer to protected inner class in Scala when inheriting from Java (with byte code only)
- Derived Scala case class with same member variables as base
- What are standard Scala monads other than Option?
- Idiomatic Scala way to deal with base vs derived class field names?
- Scala inheritance default parameter in parent class
- Replacing case class inheritance with extractors preserving exhaustiveness checks in Scala
- Is there any Scala built-in class for capturing an external process's output?
More Query from same tag
- dotty seq mapping to union
- Migrate a Traversable that uses a visitor to an Iterable in Scala 2.13
- Trying to keep types unwrapped when using refined
- data lost when converting DStream to Dataframe
- Multiple combinations of value in the list based on the target and count
- Scala Generic Array and Ordered?
- Is there a play2 framework plugin to build OAuth REST API?
- Why is the main function not running in the REPL?
- How to properly join two DataFrames for my case?
- How to get notified when unfiltered Netty server actually gets shutdown?
- How to print array values in Scala? I am getting different values
- Is there any way to use immutable collections here + make the code look better?
- Scala: package object v.s. singleton object within a package
- How can I use scala generators to generate date with range?
- How to conditionally load specific libraries depending on OS?
- Spark Dataset unique id performance - row_number vs monotonically_increasing_id
- How to import and use kafka connect datagen in spark application
- What very large functional language projects are freely available?
- converting from Java script Json to Scala Json without eleminating field name for update operation
- How to mock MVar2 make method
- Implicit not resolved on high order function
- Play slick application - connection leak
- add prefix to spark rdd elements
- Missing parameter type for expanded function in Scala ArrayBuffer
- Type-field example in "Programming in Scala" doesn't work when statically typed using superclass
- sbt file doesn't recognize the spark input
- How to add new field to struct column?
- mocking methods which use ClassTag in scala using scalamock
- Decoding a Seq of objects using Circe
- subtracting a DateTime from a DateTime in scala