• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 import java.util.HashMap;
3 
4 public class JType {
5 
6     String baseType;
7     boolean isArray;
8     boolean isClass;
9 
10     static HashMap<CType,JType> typeMapping = new HashMap<CType,JType>();
11     static HashMap<CType,JType> arrayTypeMapping = new HashMap<CType,JType>();
12 
13     static {
14     // Primitive types
typeMapping.put(new CType("GLbitfield"), new JType("int"))15     typeMapping.put(new CType("GLbitfield"), new JType("int"));
typeMapping.put(new CType("GLboolean"), new JType("boolean"))16     typeMapping.put(new CType("GLboolean"), new JType("boolean"));
typeMapping.put(new CType("GLclampf"), new JType("float"))17     typeMapping.put(new CType("GLclampf"), new JType("float"));
typeMapping.put(new CType("GLclampx"), new JType("int"))18     typeMapping.put(new CType("GLclampx"), new JType("int"));
typeMapping.put(new CType("GLenum"), new JType("int"))19     typeMapping.put(new CType("GLenum"), new JType("int"));
typeMapping.put(new CType("GLfloat"), new JType("float"))20     typeMapping.put(new CType("GLfloat"), new JType("float"));
typeMapping.put(new CType("GLfixed"), new JType("int"))21     typeMapping.put(new CType("GLfixed"), new JType("int"));
typeMapping.put(new CType("GLint"), new JType("int"))22     typeMapping.put(new CType("GLint"), new JType("int"));
typeMapping.put(new CType("GLintptr"), new JType("int"))23     typeMapping.put(new CType("GLintptr"), new JType("int"));
typeMapping.put(new CType("GLshort"), new JType("short"))24     typeMapping.put(new CType("GLshort"), new JType("short"));
typeMapping.put(new CType("GLsizei"), new JType("int"))25     typeMapping.put(new CType("GLsizei"), new JType("int"));
typeMapping.put(new CType("GLsizeiptr"), new JType("int"))26     typeMapping.put(new CType("GLsizeiptr"), new JType("int"));
typeMapping.put(new CType("GLubyte"), new JType("byte"))27     typeMapping.put(new CType("GLubyte"), new JType("byte"));
typeMapping.put(new CType("GLuint"), new JType("int"))28     typeMapping.put(new CType("GLuint"), new JType("int"));
typeMapping.put(new CType("void"), new JType("void"))29     typeMapping.put(new CType("void"), new JType("void"));
typeMapping.put(new CType("GLubyte", true, true), new JType("String"))30     typeMapping.put(new CType("GLubyte", true, true), new JType("String"));
31 
32     // Untyped pointers map to untyped Buffers
typeMapping.put(new CType("GLvoid", true, true), new JType("java.nio.Buffer", true, false))33     typeMapping.put(new CType("GLvoid", true, true),
34             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("GLvoid", false, true), new JType("java.nio.Buffer", true, false))35     typeMapping.put(new CType("GLvoid", false, true),
36             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("void", false, true), new JType("java.nio.Buffer", true, false))37     typeMapping.put(new CType("void", false, true),
38             new JType("java.nio.Buffer", true, false));
typeMapping.put(new CType("GLeglImageOES", false, false), new JType("java.nio.Buffer", true, false))39     typeMapping.put(new CType("GLeglImageOES", false, false),
40             new JType("java.nio.Buffer", true, false));
41 
42     // Typed pointers map to typed Buffers
typeMapping.put(new CType("GLboolean", false, true), new JType("java.nio.IntBuffer", true, false))43     typeMapping.put(new CType("GLboolean", false, true),
44             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLfixed", false, true), new JType("java.nio.IntBuffer", true, false))45     typeMapping.put(new CType("GLfixed", false, true),
46             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLfixed", true, true), new JType("java.nio.IntBuffer", true, false))47     typeMapping.put(new CType("GLfixed", true, true),
48             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLfloat", false, true), new JType("java.nio.FloatBuffer", true, false))49     typeMapping.put(new CType("GLfloat", false, true),
50             new JType("java.nio.FloatBuffer", true, false));
typeMapping.put(new CType("GLfloat", true, true), new JType("java.nio.FloatBuffer", true, false))51     typeMapping.put(new CType("GLfloat", true, true),
52             new JType("java.nio.FloatBuffer", true, false));
typeMapping.put(new CType("GLint", false, true), new JType("java.nio.IntBuffer", true, false))53     typeMapping.put(new CType("GLint", false, true),
54             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLint", true, true), new JType("java.nio.IntBuffer", true, false))55     typeMapping.put(new CType("GLint", true, true),
56             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLuint", false, true), new JType("java.nio.IntBuffer", true, false))57     typeMapping.put(new CType("GLuint", false, true),
58             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLuint", true, true), new JType("java.nio.IntBuffer", true, false))59     typeMapping.put(new CType("GLuint", true, true),
60             new JType("java.nio.IntBuffer", true, false));
typeMapping.put(new CType("GLshort", true, true), new JType("java.nio.ShortBuffer", true, false))61     typeMapping.put(new CType("GLshort", true, true),
62             new JType("java.nio.ShortBuffer", true, false));
63 
64     // Typed pointers map to arrays + offsets
arrayTypeMapping.put(new CType("GLboolean", false, true), new JType("boolean", false, true))65     arrayTypeMapping.put(new CType("GLboolean", false, true),
66                  new JType("boolean", false, true));
arrayTypeMapping.put(new CType("GLfixed", true, true), new JType("int", false, true))67     arrayTypeMapping.put(new CType("GLfixed", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLfixed", false, true), new JType("int", false, true))68     arrayTypeMapping.put(new CType("GLfixed", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLfloat", false, true), new JType("float", false, true))69     arrayTypeMapping.put(new CType("GLfloat", false, true), new JType("float", false, true));
arrayTypeMapping.put(new CType("GLfloat", true, true), new JType("float", false, true))70     arrayTypeMapping.put(new CType("GLfloat", true, true), new JType("float", false, true));
arrayTypeMapping.put(new CType("GLint", false, true), new JType("int", false, true))71     arrayTypeMapping.put(new CType("GLint", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLint", true, true), new JType("int", false, true))72     arrayTypeMapping.put(new CType("GLint", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLshort", true, true), new JType("short", false, true))73     arrayTypeMapping.put(new CType("GLshort", true, true), new JType("short", false, true));
arrayTypeMapping.put(new CType("GLuint", false, true), new JType("int", false, true))74     arrayTypeMapping.put(new CType("GLuint", false, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLuint", true, true), new JType("int", false, true))75     arrayTypeMapping.put(new CType("GLuint", true, true), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLintptr"), new JType("int", false, true))76     arrayTypeMapping.put(new CType("GLintptr"), new JType("int", false, true));
arrayTypeMapping.put(new CType("GLsizeiptr"), new JType("int", false, true))77     arrayTypeMapping.put(new CType("GLsizeiptr"), new JType("int", false, true));
78     }
79 
JType()80     public JType() {
81     }
82 
JType(String primitiveTypeName)83     public JType(String primitiveTypeName) {
84     this.baseType = primitiveTypeName;
85     this.isClass = false;
86     this.isArray = false;
87     }
88 
JType(String primitiveTypeName, boolean isClass, boolean isArray)89     public JType(String primitiveTypeName, boolean isClass, boolean isArray) {
90     this.baseType = primitiveTypeName;
91     this.isClass = isClass;
92     this.isArray = isArray;
93     }
94 
getBaseType()95     public String getBaseType() {
96     return baseType;
97     }
98 
99     @Override
toString()100     public String toString() {
101     return baseType + (isArray ? "[]" : "");
102     }
103 
isArray()104     public boolean isArray() {
105     return isArray;
106     }
107 
isClass()108     public boolean isClass() {
109     return isClass;
110     }
111 
isPrimitive()112     public boolean isPrimitive() {
113     return !isClass() && !isArray();
114     }
115 
isVoid()116     public boolean isVoid() {
117     return baseType.equals("void");
118     }
119 
isBuffer()120     public boolean isBuffer() {
121     return baseType.indexOf("Buffer") != -1;
122     }
123 
isTypedBuffer()124     public boolean isTypedBuffer() {
125     return !baseType.equals("java.nio.Buffer") &&
126         (baseType.indexOf("Buffer") != -1);
127     }
128 
convert(CType ctype, boolean useArray)129     public static JType convert(CType ctype, boolean useArray) {
130      JType javaType = null;
131      if (useArray) {
132          javaType = arrayTypeMapping.get(ctype);
133      }
134      if (javaType == null) {
135          javaType = typeMapping.get(ctype);
136      }
137      if (javaType == null) {
138          throw new RuntimeException("Unsupported C type: " + ctype);
139      }
140      return javaType;
141     }
142 }
143