I can get the full class name of an instance with fullyQualifiedName, but how can I get the fullyQualifiedNames of all it's superclasses? Is it possible?
Use std.traits.BaseClassesTuple.
import std.traits, std.stdio, std.meta;
class A {}
class B: A{}
class C: B{}
void main(){
auto instance = new C;
static foreach(T; AliasSeq!(typeof(instance), BaseClassesTuple!(typeof(instance))))
writeln(fullyQualifiedName!T);
}
Related
I have created my own lint Detector.visitCallExpression(UCallExpression) and I need to find a way to check if a MyClass class parameter passed into a method call is a child of MyParent class?
//Example having this below code somewhere to be Lint scanned.
someObject.method(MyClass.class)
How can I determine MyClass.class inherits from MyParent class?
//Using the IntelliJ InheritanceUtil utility class
//Converts argument of MyClass.class -> psiClass
InheritanceUtil.isInheritor(psiClass, "com.somePackage.MyParent")
The PsiClass I get from the MyClass.class parameter, is resolved to the base java.lang.Class object, so the InheritanceUtil check always return false~
Anyway i found the solution
/**
* Detects if a Class<?> => PsiClass:Class<?> is a subclass of a PsiClass(?)
*
* #param type The PsiType object that is a PsiClass of the class to be checked for subclass
* #param compareToParentCanonicalClass The Class canonical name to be compared as a super/parent class
*
* #return true if the PsiType is a subclass of compareToParentCanonicalClass, false otherwise
*/
open fun isPsiTypeSubClassOfParentClassType(type: PsiType, compareToParentCanonicalClass: String): Boolean {
println("superClass checking:$type")
var psiClss = PsiTypesUtil.getPsiClass(type)
val pct = type as PsiClassType
val psiTypes: List<PsiType> = ArrayList<PsiType>(pct.resolveGenerics().substitutor.substitutionMap.values)
for (i in psiTypes.indices) {
println("canonical:"+ psiTypes[i].canonicalText)
var psiClass = psiClss?.let { JavaPsiFacade.getInstance(it.project).findClass(psiTypes[i].canonicalText, psiClss.resolveScope) }
return InheritanceUtil.isInheritor(psiClass, compareToParentCanonicalClass)
}
return false;
}
preemptive excuse for bad explenation.
But I am attempting to test the functionality of a logic statement within my class:
import ClassIWantToMockk
class MyClass {
fun myMethodToTest(string: String): Boolean = string == ClassIWantToMockk.methodReturningObject.toString()
}
I want to control the returned string from the method in the imported class in my test class. The class contains an override of the toString() method if that is relevant.
I just cant seem to find out how I can control the return without an instance of the class as a parameter...
best regards
I have a class that has a constructor of type Any. I'm passing an instance of a Data Class to that constructor. How can I type check the Any variable to make sure it contains a Data Class?
What I tried so far:
private var myObject : Any
fun dataClassTypeCheck(): Boolean {
if (myObject is KClass<*>) {return true}
return false
}
If you want to know if myObject has a type which is a data class then it's:
myObject::class.isData.
If you want to know if myObject is a KClass object of a data class then it's: myObject.isData
if you have Class<?>:
MyObjectClass::class.java.kotlin.isData
and if you have instance of class:
myObject.javaCalass.kotlin.isData
I just have a (hopefully) simple question. How do I make a variable in one class that can be accessed by another class in Kotlin?
Class A:
var isBlue = 1
Class B:
if isBlue==1 then ...
class A
class A {
var isBlue = 1
}
class B
class B {
var classA = A()
fun demo(){
classA.isBlue//get A member
}
}
hope this helps.
you can either create an instance of the object and access the property like this
ClassA().isBlue
or inherit the class and access the attribute like this.
ClassB:ClassA{ fun someFn(){if (isBlue == 1) do something}}
In java , just declare with " static " , no need to call class name , in Kotlin need to call class name , but null pointer exception , Such headache problem
I'm working on a homework assignment for my object oriented design class, and I'm running into trouble with Scala's companion objects. I've read in a few places that companion objects are supposed to have access to their companion class's private methods, but I can't seem to get it to work. (Just as a note, the meat of the assignment had to do with implementing a binary search tree, so I'm not just asking for answers...)
I have an object that is supposed to create an instance of my private class, BstAtlas (Bst is also defined in the Atlas object, took it out for clarity):
object Atlas {
def focusRoom(newRoom:Room,a:Atlas):Atlas = a.helpFocusRoom(newRoom);
abstract class Atlas {
...
protected def helpFocusRoom(n:Room):Atlas;
...
}
private class BstAtlas(bst:Bst) extends Atlas {
...
protected def helpFocusRoom(newRoom:Room):Atlas = ...
// uses some of bst's methods
...
}
}
But when I go to compile, I get the following error:
Question23.scala:15: error: method
helpFocusRoom cannot be accessed in
Atlas.Atlas
a.helpFocusRoom(newRoom);
The function helpFocusRoom needs to be hidden, but I don't know how to hide it and still have access to it inside of the companion object.
Can anyone tell me what I'm doing wrong here?
The problem is that classes and companion objects can't be nested like that. To define a companion object, you need to define the class outside of the object's body but in the same file.
Companion objects should be next to their real object, not containing it:
object Example {
class C(val i: Int = C.DefaultI) { }
object C { protected val DefaultI = 5 }
}
scala> (new Example.C).i
res0: Int = 5
scala> Example.C.DefaultI
<console>:11: error: value DefaultI cannot be accessed in object Example.C
Example.C.DefaultI
Alternatively, you can alter the scope of the protected keyword to include the enclosing object:
object Example {
def value = (new D).hidden
class D(val i: Int = 5) {
protected[Example] def hidden = i*i
}
}
scala> Example.value
res1: Int = 25
but here you ought not name the outer object the same thing as the inner class or you'll have trouble referring to it from within the class.