Close

Java Reflection - Method.getTypeParameters() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Method

java.lang.Objectjava.lang.Objectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AccessibleObjectjava.lang.reflect.AnnotatedElementAnnotatedElementjava.lang.reflect.Executablejava.lang.reflect.Executablejava.lang.reflect.MemberMemberjava.lang.reflect.GenericDeclarationGenericDeclarationjava.lang.reflect.Methodjava.lang.reflect.MethodLogicBig

Method:

public TypeVariable<Method>[] getTypeParameters()

Returns an array of TypeVariable objects that represent the type variables declared by this method.


Examples


package com.logicbig.example.method;

import javax.swing.*;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;

public class GetTypeParametersExample {

private static <N extends Number, C extends JComponent> void process(N n, C c) {}

public static void main(String... args) throws NoSuchMethodException {
Method m = GetTypeParametersExample.class.getDeclaredMethod("process", Number.class,
JComponent.class);
TypeVariable<Method>[] typeParameters = m.getTypeParameters();
System.out.println(Arrays.toString(typeParameters));
for (TypeVariable<Method> typeParameter : typeParameters) {
printTypeVariable(typeParameter, 1);
}
}

private static void printTypeVariable(TypeVariable type, int level) {
print(level, "TypeVariable#getName(): " + type.getName());
Type[] bounds = type.getBounds();
print(level, "TypeVariable#getBounds(): " + Arrays.toString(bounds));
for (Type bound : bounds) {
print(level, "TypeVariable#bound: " + bound);
printType(bound, level + 1);
}
}

private static void printType(Type type, int level) {
if (type == null) {
return;
}
if (type instanceof Class) {
print(level, "type is an instance of Class");
print(level, "Class Name: " + ((Class) type).getName());
} else {
throw new RuntimeException("not expected");
}
}

private static void print(int level, String string) {
System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string);
}
}

Output

[N, C]
¦- TypeVariable#getName(): N
¦- TypeVariable#getBounds(): [class java.lang.Number]
¦- TypeVariable#bound: class java.lang.Number
¦- type is an instance of Class
¦- Class Name: java.lang.Number
¦- TypeVariable#getName(): C
¦- TypeVariable#getBounds(): [class javax.swing.JComponent]
¦- TypeVariable#bound: class javax.swing.JComponent
¦- type is an instance of Class
¦- Class Name: javax.swing.JComponent




package com.logicbig.example.method;

import javax.swing.*;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;
import java.util.Objects;

public class GetTypeParametersExample2 {

private static <N extends Number, C extends JComponent> void process() {}

public static void main(String... args) throws NoSuchMethodException {
Method m = GetTypeParametersExample2.class.getDeclaredMethod("process");
TypeVariable<Method>[] typeParameters = m.getTypeParameters();
System.out.println(Arrays.toString(typeParameters));
for (TypeVariable<Method> typeParameter : typeParameters) {
printTypeVariable(typeParameter, 1);
}
}

private static void printType(Type type, int level) {
if (type == null) {
return;
}
print(level, "type: " + Objects.toString(type));
if (type instanceof TypeVariable) {
print(level, "type is an instance of TypeVariable");
printTypeVariable((TypeVariable) type, level + 1);
} else if (type instanceof Class) {
print(level, "type is an instance of Class");
print(level, "Class Name: " + ((Class) type).getName());
} else {
throw new RuntimeException();
}
}

private static void printTypeVariable(TypeVariable type, int level) {
print(level, "TypeVariable#getName(): " + type.getName());
Type[] bounds = type.getBounds();
print(level, "TypeVariable#getBounds(): " + Arrays.toString(bounds));
for (Type bound : bounds) {
print(level, "TypeVariable#bound: " + bound);
printType(bound, level + 1);
}
}

private static void print(int level, String string) {
System.out.printf("%" + (level * 4 - 3) + "s\u00A6- %s%n", "", string);
}
}

Output

[N, C]
¦- TypeVariable#getName(): N
¦- TypeVariable#getBounds(): [class java.lang.Number]
¦- TypeVariable#bound: class java.lang.Number
¦- type: class java.lang.Number
¦- type is an instance of Class
¦- Class Name: java.lang.Number
¦- TypeVariable#getName(): C
¦- TypeVariable#getBounds(): [class javax.swing.JComponent]
¦- TypeVariable#bound: class javax.swing.JComponent
¦- type: class javax.swing.JComponent
¦- type is an instance of Class
¦- Class Name: javax.swing.JComponent




package com.logicbig.example.method;

import javax.swing.*;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
import java.util.Arrays;

public class GetTypeParametersExample3<N extends Number, C extends JComponent> {

//this method does not declare any type variable
private void process(N n, C c) {
}

public static void main(String... args) throws NoSuchMethodException {
Method m = GetTypeParametersExample3.class.getDeclaredMethod("process", Number.class, JComponent.class);
TypeVariable<Method>[] typeParameters = m.getTypeParameters();
System.out.println(Arrays.toString(typeParameters));
}
}

Output

[]




package com.logicbig.example.method;

import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
import java.util.*;

public class GetTypeParametersExample4 {

public static void main(String... args) {
for (Method method : ArrayList.class.getDeclaredMethods()) {
TypeVariable<Method>[] tps = method.getTypeParameters();
if (tps.length > 0) {
System.out.printf("method: %s, typeVariables: %s%n", method, Arrays.toString(tps));
}
}
}
}

Output

method: public java.lang.Object[] java.util.ArrayList.toArray(java.lang.Object[]), typeVariables: [T]
method: static java.lang.Object java.util.ArrayList.elementAt(java.lang.Object[],int), typeVariables: [E]




See Also