Created
September 5, 2020 08:57
-
-
Save amaembo/5f4ad6a88352a46f2b9f4b24627d323c to your computer and use it in GitHub Desktop.
Multi-dimensional arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.annotation.*; | |
import java.lang.reflect.*; | |
@Retention(RetentionPolicy.RUNTIME) | |
@Target(ElementType.TYPE_USE) | |
@interface TA { | |
int value(); | |
} | |
public class MyClass { | |
public void test(@TA(0) String @TA(1) [] @TA(2) [] f @TA(3) [] @TA(4) [], | |
@TA(0) String @TA(3) [] @TA(4) [] @TA(1) [] @TA(2) [] g) { } | |
public static void main(String[] args) throws NoSuchMethodException { | |
Method test = MyClass.class.getMethod("test", String[][][][].class, | |
String[][][][].class); | |
System.out.println(readVals(test.getAnnotatedParameterTypes()[0])); | |
System.out.println(readVals(test.getAnnotatedParameterTypes()[1])); | |
} | |
private static String readVals(AnnotatedType type) { | |
String val = "(" + type.getAnnotation(TA.class).value() + ")"; | |
return type instanceof AnnotatedArrayType ? | |
val + readVals(((AnnotatedArrayType) type).getAnnotatedGenericComponentType()) : | |
val; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment