2

I would like to obtain int[].class from Matlab. Unfortunately, Matlab does not allow this syntax. Simultaneously, I allows to call any Java functions or access static members as is.

For example, I can't call

int.class

but can

java.lang.Integer.TYPE

Is it possible to find int[].class somewhere in JDK API in the same way?

Dims
  • 47,675
  • 117
  • 331
  • 600

2 Answers2

3

So I tried this in a jshell:

int[].class.getName()

And that yielded:

[I

And tried to reverse it:

Class.forName("[I")

And that seemed to parse it: class [I

So you could try Class.forName("[I"). And that seems to work just fine:

Class.forName("[I").isArray() // outputs true
ernest_k
  • 44,416
  • 5
  • 53
  • 99
1

You can use the Apache Commons package in order to achieve your goal. Signally, ClassUtils.getClass is what you are looking for:

>> org.apache.commons.lang.ClassUtils.getClass('int[]')

 ans = 
       class [I

For the sake of analyzing things deeply:

>> ans.get()

              Annotation: 0
             Annotations: [0×1 java.lang.annotation.Annotation[]]
          AnonymousClass: 0
                   Array: 1
           CanonicalName: 'int[]'
                   Class: [1×1 java.lang.Class]
             ClassLoader: []
                 Classes: [0×1 java.lang.Class[]]
           ComponentType: [1×1 java.lang.Class]
            Constructors: [0×1 java.lang.reflect.Constructor[]]
     DeclaredAnnotations: [0×1 java.lang.annotation.Annotation[]]
         DeclaredClasses: [0×1 java.lang.Class[]]
    DeclaredConstructors: [0×1 java.lang.reflect.Constructor[]]
          DeclaredFields: [0×1 java.lang.reflect.Field[]]
         DeclaredMethods: [0×1 java.lang.reflect.Method[]]
          DeclaringClass: []
          EnclosingClass: []
    EnclosingConstructor: []
         EnclosingMethod: []
                    Enum: 0
           EnumConstants: []
                  Fields: [0×1 java.lang.reflect.Field[]]
       GenericInterfaces: [2×1 java.lang.Class[]]
       GenericSuperclass: [1×1 java.lang.Class]
               Interface: 0
              Interfaces: [2×1 java.lang.Class[]]
              LocalClass: 0
             MemberClass: 0
                 Methods: [9×1 java.lang.reflect.Method[]]
               Modifiers: 1041
                    Name: '[I'
                 Package: []
               Primitive: 0
        ProtectionDomain: [1×1 java.security.ProtectionDomain]
                 Signers: []
              SimpleName: 'int[]'
              Superclass: [1×1 java.lang.Class]
               Synthetic: 0
          TypeParameters: [0×1 java.lang.reflect.TypeVariable[]]
Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98