KotlinPoet how to get TypeName of generated class - kotlin

I want to use a class generated with TypeSpec.classBuilder as a property in another class that I am generating. But for this I need to get a TypeName and I cannot find a way to access it. Only from the superclass. Anyone knows a way to do this?

You should be perfectly fine with using ClassName there.
And the easiest way of obtaining the ClassName is to pass the package and the name of the generated type:
ClassName("your.package.here", "NameOfType")
You can see here how I'm specifying a receiver of an extension function ("similar" use-case):

Related

Junit5 set displayName programatically from extension

How can the display name of a test be defined programmatically, via an extension?
From the org.junit.jupiter.api.extension.ExtensionContext I cannot find a way to access the display name. ClassExtensionContext and MethodExtensionContext are package private so I cannot unwrap like:
((ClassExtensionContext) context).getTestDescriptor()
//or
((MethodExtensionContext) context).getTestDescriptor()
even if this was possible the displayName is private with no setter, so the only way was to use reflection which is not a good solution.

How do you use a function in a namespace?

I have the following Namespace with 2 functions in it that I want to use, but I'm not sure how I access them?
I tried importing ("using" for C#) but those 2 functions arent in the namespace.
Do I have to create a class and create those functions myself?
The weird thing is, those 2 functions are found within a different namespace:
Regardless of where it is, how would I turn these into functions I can call from my code?
When I try to access those functions in my code, they don't know up :
I think you're getting a bit confused. It's understandable with namespaces.
Gds.GoogleMap.UltimatePlus.Math.Mathservice is not a namespace name. The namespace name is Gds.GoogleMap.UltimatePlus.Math . MathService is the class name.
If you put the statement
using Gds.GoogleMap.UltimatePlus.Math;
at the top of your file then all you need to do to declare a new object is to say:
MathService myService = new MathService();
assuming it has a default constructor.
Give it a try.

Create a new PSIClass with Generic type parameters

Newbie question on IntelliJ plugin development.
I need to generate a parameterised class (Class with generics) given the name of the class and the name of the type parameter, but I can not find how to?
It seems PSIClass does not support generics.
Example
Given
String className = "MyClass";
String typeName = "T"
I would like to have a PSIClass that represents this:
public class MyClass<T> { ... }
The goal is to dynamically add methods to such class and eventually write the complete class to a file. The class needs to declare the Type Variable because some methods will receive/return T
Thanks!
I have found a solution in the intelliJ developer forums. It doesn't seem to be the neatest one, but it works.
I'd recommend to use
PsiFileFactory.getInstance(...).createFileFromText("ClassName.java",
JavaFileType.INSTANCE, "class ClassName { ...}"), cast the result
to PsiJavaFile and use its getClasses[0] as the result.
Here is the link to the thread:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/115000089970-Create-a-new-PSIClass-with-Generic-type-parameters?page=1#community_comment_115000122164

How to put class dynamically in <>

I know there are various capabilities in Java with reflection.
For example:
Class<?> clazz = Class.forName("java.util.Date");
Object ins = clazz.newInstance();
I wonder if I could pass class dynamicaly in some method declaration in <> tags (or there is other way to do it if it must be fixed). I would like to change that class declaration dynamicaly; because I would like to write generic method for all types of classes.
In there, I have this:
List<Country>
Can I write it something diffrent with reflection? For example can it be somehow be achieved to pass class as parameter (or how else should be this done):
List<ins>
? I would appreciate examples.
This cannot be done because generics are a compile time feature. Once code is compiled, the only place where generics are exists are at method signatures, and they are only used for compiling new code.
When working with reflection, you are basicly working with raw types, and need to code according to that, that means, you can cast the returned result of newInstance() to the list type your need, for example:
List<Country> ins = (List<Country>)clazz.newInstance();
This is a safe operation to do, because you know at that point its empty, and isn't passed to any outside code.
I don't think this is possible. Generics in Java are implemented in a way that prohibits runtime access.
Generics are there so that the compiler can verify correct typing, but are no longer present at runtime (this is called "type erasure"). Reflection deals with the runtime representation of types only. As far as I know the only case where reflection has to deal with generics is to find out "fixed" type parameters of sub-classes, e.g. when you have class Bar<T> and class Foo extends Bar<String>, you can find out that the T of Bar is fixed to String in Foo using reflection. However, this is information found in the class file, too. Except that, reflection can only see or create raw-types.

Can I provide a better name for a pre-defined class

I have a class that has been pre-defined by an XSD but the name is long and confusing. I can't rename the class directly. Is there a way to create a new class with a better name and inherit the poorly named one without having to write a bunch of setters and getters since the XSD could potentially change over time?
I tried just creating an empty class like this:
Public Class TaskDetails : Inherits ExternalResponseTaskDetailTaskDetail
End Class
And that would work fine, except I get a casting error when I try to
Dim td as TaskDetails = ertdtd
This seems like I'm asking a very stupid question, but try as I might, I can't get my Google-Fu working for me on it. The long class name does make sense inside the library I'm working in, but outside the library, it really doesn't. Especially since the library contains an ExternalRequestTaskDetails class and ExternalRequestTaskDetailsTaskDetails is an element contained within it.