How to convert regular class to inner class in model transformation Java? - enterprise-architect

I am trying to make a transformation of classes in which all are normal classes, I would like that when making the transformation, some of these classes were inner class of another one.
For example, the Element class becomes an innerclass of CriticalActivity when the transformation is performed.

Related

Scala newInstance() an inner class nested in a trait

I'm writing a script to automatically configure sharding for some specific MongoDB collections when the app is being deployed on a fresh cluster. The application is using the Lift framework and basically every sharded collection is mapped to a MongoRecord class extending a particular "ShardedCollection" trait. I need to call a particular method on those classes in order to get their collection name.
So the first step is to find in the code those specifics classes and for that I use ClassUtil . Then I need a way to instantiate them and for that I thought that java reflection should be able to do it. It's working but only if those classes do not belong to an outer class.
The configuration in this specific edge case is like:
class X {
class Y extends ShardedCollection {
}
}
So after reading some documentation I found that I had to call YConstructor.newInstance(XObject), newInstance taking as a first argument an XObject (as an instance of X) When Y is an inner class of X. My strategy is to recursively instantiate the enclosing classes until I'm getting the one that has the ShardedCollection trait.
The problem arise when X is no more a class but a trait, and then there is no constructor that I can use for it, but I still need to feed an XObject to newInstance .. Tricky :(
To be very concise from the java doc
If the constructor's declaring class is an inner class in a non-static context, the first argument to the constructor needs to be the enclosing instance
What do I do when the enclosing "thing" is a trait ? (assuming that I can't modify anything in the code base)

Scala: pattern matching on a type with a given "path"?

Lets say I have:
object myObject{
class myClass1(){
class myClass11(){
class myClass111
}
class myClass12(){
class myClass121
}
}
}
class myClass{
class myClass2(){
object myObj21
object myObj22
}
}
I want to pattern match on the types and objects introduced above and get instances of:
myClass1 only (nested type inside an object)
myClass11 and myClass12 (doubly nested types inside aObject/aClass)
myClass2 only (nested type inside a class)
myObj21 , myObj22 only. (doubly nested types inside aClass/aClass)
myClass111, myClass121. ( nested types in different branches but the same nest level and same "root")
ie. different variations of object/class and same level types or same sublevel types.
Can I do this with a type "regex" equivalent instead of spelling the type paths explicitly?
No, there is no nice way to do this. Pattern matching on nested types at all is iffy, let alone complex matches about them.
You might be able to write some custom unapply to do this kind of stuff using reflection, or perhaps even a macro. But thinking about that rabbit hole makes me wonder why this is necessary in the first place. Maybe you could add some traits that represent what actual behavior you care about, and pattern match on those?

How to correctly implement a custom number-like class in Scala?

I am currently trying to implement my own UnsignedInt. I would like to implement this correctly so that it fits into the whole Scala type & class system. However, I am really confused by all the classes that fit into Number.
With which class(es) do I need to work: Numeric, Integral or ScalaNumber? Or something completely different? What classes and/or traits should my own class implement?
The short answer is: don't implement your own, use the Spire one :) Otherwise, you should implement Integral (which includes Numeric). Note that your type shouldn't extend it; you need implicit values in the companion object, i.e.
class UnsignedInt { ... }
object UnsignedInt {
implicit val UIntIntegral: Integral[UnsignedInt] = ...
}
You should also consider making your class a value class.

Scala: Type resolution of an inner case class in extending classes

I have something similar to a tree like structure.
I have a trait T. And two classes (A and B) that extends T.
A is like the root node that creates new instances from B while B itself can create new instances from B.
In trait T I define a case class C that I use to save some specific values as well as a List[C] (lets call it listC to keep things simple) that contains all instances of C.
Also every B knows its parent. What I want to do is listC = parent.listC. However this does not work as listC expects the typ List[C] but gets List[parent.C].
How can I solve this issue?
Additionally here are some solutions that I tried but didn't work / are not possible:
I can not define the case class outside of the trait as its values have types that only get defined within the trait.
An easy solution would be to drop the class and use a Tuple instead. However I would like to stick with the class to enhance readability. It's just nice to call x.varName as x._4
If you want that the type C in different subclasses of T to be the same type you cannot define the type as a path dependent type in T.
Tuple2[A.D, A.E] will not be the same as Tuple2[B.D, B.E], this is the exact same problem.
But if a tuple would work, then you should be able to just define the C case class in the companion object of T for example just as well as using a tuple.
One way to do this could be to put bounds on what the nested types can be and use a common supertype in your C/Tuples

scala - find objects derived from class

Is there a way in scala to get list of all objects(by object I mean scalas object) that derive from specified abstract class?
something like that:
abstract class A
object B extends A //in different file
object C extends A //in different file
def findObjectsDerivingFromA(): Seq[A] //which will give result: Seq(B, C)
I know from here : Can I get a compile-time list of all of the case objects which derive from a sealed parent in Scala?
that it's possible with sealed base trait/class but in my case objects B and C will be pretty complex so I need to have them in different files.
edit:
I've changed method name because previous one was misleading.