简体   繁体   中英

How do I include the name of type parameter T in the summary of a generic method?

I want to do something like:

public class myClass<T> {
    /**
    returns a {class name of T}
    */
    public T getT() {
        return new T();
    }
}

I would like to replace "{Class Name of T}" with whatever T's classname is when the description shows up in vscode or another IDE like this: 在此处输入图像描述

Java type erasure is going to require you pass a Class<T> somewhere in order to get a new T instance. Java class names start with a capital letter. As for your Javadoc, you could do something like @returns a new T instance . For example,

public class MyClass<T> {
    private Class<T> cls;
    public MyClass(Class<T> cls) {
        this.cls = cls;
    }
    
    /**
     * @returns a new T instance.
     * @throws IllegalAccessException 
     * @throws InstantiationException 
     * @throws SecurityException 
     * @throws NoSuchMethodException 
     * @throws InvocationTargetException 
     * @throws IllegalArgumentException 
    */
    public T getT() throws InstantiationException, 
            IllegalAccessException, IllegalArgumentException, 
            InvocationTargetException, NoSuchMethodException, 
            SecurityException {
        return cls.getConstructor().newInstance();
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM