PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/game_platform/lib/lwjgl-2.8.5/java/org/lwjgl/util/generator/NativeTypeTranslator.java

https://bitbucket.org/dagronlund/opengl-game-platform
Java | 256 lines | 168 code | 36 blank | 52 comment | 48 complexity | 6653bd5e5d5d26c40b7172b4f6bdbbbe MD5 | raw file
  1. /*
  2. * Copyright (c) 2002-2008 LWJGL Project
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are
  7. * met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of 'LWJGL' nor the names of
  17. * its contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  22. * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  27. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  28. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  29. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  30. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. package org.lwjgl.util.generator;
  33. /**
  34. *
  35. * A TypeVisitor that translates types (and optional native type
  36. * annotations) to the native type string.
  37. *
  38. * @author elias_naur <elias_naur@users.sourceforge.net>
  39. * @version $Revision: 3783 $
  40. * $Id: NativeTypeTranslator.java 3783 2012-08-07 15:14:03Z spasi $
  41. */
  42. import org.lwjgl.PointerBuffer;
  43. import java.lang.annotation.Annotation;
  44. import java.nio.*;
  45. import java.util.ArrayList;
  46. import java.util.Collection;
  47. import com.sun.mirror.declaration.AnnotationMirror;
  48. import com.sun.mirror.declaration.Declaration;
  49. import com.sun.mirror.type.*;
  50. import com.sun.mirror.util.TypeVisitor;
  51. /**
  52. * $Id: NativeTypeTranslator.java 3783 2012-08-07 15:14:03Z spasi $
  53. * <p/>
  54. * A TypeVisitor that translates (annotated) TypeMirrors to
  55. * native types
  56. *
  57. * @author elias_naur <elias_naur@users.sourceforge.net>
  58. * @version $Revision: 3783 $
  59. */
  60. public class NativeTypeTranslator implements TypeVisitor {
  61. private Collection<Class> native_types;
  62. private boolean is_indirect;
  63. private final Declaration declaration;
  64. private final TypeMap type_map;
  65. public NativeTypeTranslator(TypeMap type_map, Declaration declaration) {
  66. this.declaration = declaration;
  67. this.type_map = type_map;
  68. }
  69. public String getSignature() {
  70. return getSignature(false);
  71. }
  72. public String getSignature(final boolean skipConst) {
  73. StringBuilder signature = new StringBuilder();
  74. if ( !skipConst && declaration.getAnnotation(Const.class) != null )
  75. signature.append("const ");
  76. if ( declaration.getAnnotation(PointerWrapper.class) != null ) {
  77. signature.append(declaration.getAnnotation(PointerWrapper.class).value());
  78. } else if ( declaration.getAnnotation(NativeType.class) != null ) {
  79. signature.append(declaration.getAnnotation(NativeType.class).value());
  80. } else {
  81. // Use the name of the native type annotation as the C type name
  82. signature.append(getAnnotationType().getSimpleName());
  83. }
  84. if ( is_indirect )
  85. signature.append(" *");
  86. return signature.toString();
  87. }
  88. public Class getAnnotationType() {
  89. if ( native_types.size() != 1 )
  90. throw new RuntimeException("Expected only one native type for declaration " + declaration +
  91. ", but got " + native_types.size());
  92. return native_types.iterator().next();
  93. }
  94. public void visitAnnotationType(AnnotationType t) {
  95. throw new RuntimeException(t + " is not allowed");
  96. }
  97. public void visitArrayType(ArrayType t) {
  98. final Class<?> type = Utils.getJavaType(t).getComponentType();
  99. if ( CharSequence.class.isAssignableFrom(type) ) {
  100. is_indirect = true;
  101. native_types = new ArrayList<Class>();
  102. native_types.add(type_map.getStringArrayType());
  103. } else if ( Buffer.class.isAssignableFrom(type) ) {
  104. is_indirect = true;
  105. native_types = new ArrayList<Class>();
  106. native_types.add(type_map.getByteBufferArrayType());
  107. } else if ( org.lwjgl.PointerWrapper.class.isAssignableFrom(type) ) {
  108. is_indirect = false;
  109. } else
  110. throw new RuntimeException(t + " is not allowed");
  111. }
  112. public static PrimitiveType.Kind getPrimitiveKindFromBufferClass(Class c) {
  113. if ( IntBuffer.class.equals(c) )
  114. return PrimitiveType.Kind.INT;
  115. else if ( DoubleBuffer.class.equals(c) )
  116. return PrimitiveType.Kind.DOUBLE;
  117. else if ( ShortBuffer.class.equals(c) )
  118. return PrimitiveType.Kind.SHORT;
  119. else if ( ByteBuffer.class.equals(c) || PointerBuffer.class.equals(c) )
  120. return PrimitiveType.Kind.BYTE;
  121. else if ( FloatBuffer.class.equals(c) )
  122. return PrimitiveType.Kind.FLOAT;
  123. else if ( LongBuffer.class.equals(c) )
  124. return PrimitiveType.Kind.LONG;
  125. else
  126. throw new RuntimeException(c + " is not allowed");
  127. }
  128. @SuppressWarnings("unchecked")
  129. public static Class<? extends Annotation> getClassFromType(DeclaredType t) {
  130. try {
  131. return (Class<? extends Annotation>)Class.forName(t.getDeclaration().getQualifiedName());
  132. } catch (ClassNotFoundException e) {
  133. throw new RuntimeException(e);
  134. }
  135. }
  136. private void getNativeTypeFromAnnotatedPrimitiveType(PrimitiveType.Kind kind) {
  137. native_types = translateAnnotations();
  138. if ( native_types.size() == 0 )
  139. native_types.add(type_map.getNativeTypeFromPrimitiveType(kind));
  140. }
  141. public void visitClassType(ClassType t) {
  142. is_indirect = true;
  143. Class<?> c = getClassFromType(t);
  144. if ( String.class.equals(c) ) {
  145. native_types = new ArrayList<Class>();
  146. native_types.add(type_map.getStringElementType());
  147. } else if ( Buffer.class.equals(c) ) {
  148. native_types = new ArrayList<Class>();
  149. native_types.add(type_map.getVoidType());
  150. } else if ( Buffer.class.isAssignableFrom(c) ) {
  151. PrimitiveType.Kind kind = getPrimitiveKindFromBufferClass(c);
  152. getNativeTypeFromAnnotatedPrimitiveType(kind);
  153. } else if ( PointerBuffer.class.isAssignableFrom(c) ) {
  154. native_types = new ArrayList<Class>();
  155. native_types.add(PointerBuffer.class);
  156. } else if ( org.lwjgl.PointerWrapper.class.isAssignableFrom(c) ) {
  157. native_types = new ArrayList<Class>();
  158. native_types.add(PointerWrapper.class);
  159. is_indirect = false;
  160. } else
  161. throw new RuntimeException(t + " is not allowed");
  162. }
  163. public void visitPrimitiveType(PrimitiveType t) {
  164. getNativeTypeFromAnnotatedPrimitiveType(t.getKind());
  165. }
  166. public void visitDeclaredType(DeclaredType t) {
  167. throw new RuntimeException(t + " is not allowed");
  168. }
  169. public void visitEnumType(EnumType t) {
  170. throw new RuntimeException(t + " is not allowed");
  171. }
  172. public void visitInterfaceType(InterfaceType t) {
  173. // See ARB_debug_label.glObjectPtrLabel
  174. Class<?> c = getClassFromType(t);
  175. if ( org.lwjgl.PointerWrapper.class.isAssignableFrom(c) ) {
  176. native_types = new ArrayList<Class>();
  177. native_types.add(PointerWrapper.class);
  178. is_indirect = false;
  179. } else
  180. throw new RuntimeException(t + " is not allowed");
  181. }
  182. // Check if the annotation is itself annotated with a certain annotation type
  183. public static <T extends Annotation> T getAnnotation(AnnotationMirror annotation, Class<T> type) {
  184. return annotation.getAnnotationType().getDeclaration().getAnnotation(type);
  185. }
  186. private static Class translateAnnotation(AnnotationMirror annotation) {
  187. NativeType native_type = getAnnotation(annotation, NativeType.class);
  188. if ( native_type != null ) {
  189. return getClassFromType(annotation.getAnnotationType());
  190. } else
  191. return null;
  192. }
  193. private Collection<Class> translateAnnotations() {
  194. Collection<Class> result = new ArrayList<Class>();
  195. for ( AnnotationMirror annotation : Utils.getSortedAnnotations(declaration.getAnnotationMirrors()) ) {
  196. Class translated_result = translateAnnotation(annotation);
  197. if ( translated_result != null ) {
  198. result.add(translated_result);
  199. }
  200. }
  201. return result;
  202. }
  203. public void visitReferenceType(ReferenceType t) {
  204. throw new RuntimeException(t + " is not allowed");
  205. }
  206. public void visitTypeMirror(TypeMirror t) {
  207. throw new RuntimeException(t + " is not allowed");
  208. }
  209. public void visitTypeVariable(TypeVariable t) {
  210. throw new RuntimeException(t + " is not allowed");
  211. }
  212. public void visitVoidType(VoidType t) {
  213. native_types = translateAnnotations();
  214. if ( native_types.size() == 0 )
  215. native_types.add(void.class);
  216. }
  217. public void visitWildcardType(WildcardType t) {
  218. throw new RuntimeException(t + " is not allowed");
  219. }
  220. }