41package com.jogamp.gluegen;
44import java.util.ArrayList;
47import com.jogamp.gluegen.cgram.types.*;
61 VOID, CHAR, SHORT, INT32, INT64, FLOAT, DOUBLE;
74 public static final List<Integer>
pushValueIndex(
final List<PascalStringElem> source, List<Integer> indices) {
75 if(
null == indices ) {
76 indices =
new ArrayList<Integer>(2);
79 indices.add(p.valueIdx);
83 public static final List<Integer>
pushLengthIndex(
final List<PascalStringElem> source) {
84 final List<Integer> lengths =
new ArrayList<Integer>(2);
86 lengths.add(p.lengthIdx);
92 if(
null != pascals ) {
102 if(
null != pascals ) {
118private final Class<?> clazz;
119 private final String clazzName;
120 private final String structName;
121 private final Type elementType;
122 private final C_PTR primitivePointerType;
123 private final boolean opaqued;
127 private static JavaType nioBufferType;
128 private static JavaType nioByteBufferType;
129 private static JavaType nioShortBufferType;
130 private static JavaType nioIntBufferType;
131 private static JavaType nioLongBufferType;
132 private static JavaType nioPointerBufferType;
133 private static JavaType nioFloatBufferType;
134 private static JavaType nioDoubleBufferType;
135 private static JavaType nioByteBufferArrayType;
138 public boolean equals(
final Object arg) {
139 if ((arg ==
null) || (!(arg instanceof
JavaType))) {
144 ( t.clazz == clazz &&
145 ( ( clazzName ==
null ? t.clazzName == null : clazzName.equals(t.clazzName) ) ||
146 ( clazzName !=
null && t.clazzName !=
null && clazzName.equals(t.clazzName) )
148 ( ( structName ==
null ? t.structName == null : structName.equals(t.structName) ) ||
149 ( structName !=
null && t.structName !=
null && structName.equals(t.structName) )
151 ( elementType == t.elementType ||
152 ( elementType !=
null && t.elementType !=
null && elementType.
equals(t.elementType) )
154 primitivePointerType == t.primitivePointerType
161 return clazz.hashCode();
163 if (clazzName !=
null) {
164 return clazzName.hashCode();
166 if (structName !=
null) {
167 return structName.hashCode();
169 if (elementType !=
null) {
172 if (primitivePointerType !=
null) {
173 return primitivePointerType.hashCode();
190 return new JavaType(clazz,
true,
null);
198 return new JavaType(clazz,
false,
null);
259 if (objectType ==
null) {
266 if (nioBufferType ==
null) {
269 return nioBufferType;
273 if (nioByteBufferType ==
null) {
276 return nioByteBufferType;
280 if (nioShortBufferType ==
null) {
283 return nioShortBufferType;
287 if (nioIntBufferType ==
null) {
290 return nioIntBufferType;
294 if (nioLongBufferType ==
null) {
297 return nioLongBufferType;
301 if(nioPointerBufferType ==
null)
302 nioPointerBufferType =
createForClass(com.jogamp.common.nio.PointerBuffer.class);
303 return nioPointerBufferType;
307 if (nioFloatBufferType ==
null) {
310 return nioFloatBufferType;
314 if (nioDoubleBufferType ==
null) {
315 nioDoubleBufferType =
createForClass(java.nio.DoubleBuffer.class);
317 return nioDoubleBufferType;
321 if (nioByteBufferArrayType ==
null) {
322 final ByteBuffer[] tmp =
new ByteBuffer[0];
325 return nioByteBufferArrayType;
345 if (clazz.isArray()) {
346 return arrayName(clazz);
348 return clazz.getName();
350 if( clazzName !=
null ) {
351 return (
null != cfg ? (cfg.
packageForStruct(clazzName) +
".") :
"") + clazzName;
353 if (elementType !=
null) {
356 return (
null != cfg ? (cfg.
packageForStruct(clazzName) +
".") :
"") + structName;
367 return descriptor(clazz);
369 if(
null != clazzName ) {
370 return descriptor((
null != cfg ? (cfg.
packageForStruct(clazzName) +
".") :
"") + clazzName);
372 if(
null != structName ) {
373 return descriptor((
null != cfg ? (cfg.
packageForStruct(structName) +
".") :
"") + structName);
375 if (elementType !=
null) {
376 if(elementType.
getName()==
null) {
377 throw new RuntimeException(
"elementType.name is null: "+
getDebugString());
379 return "[" + descriptor(elementType.
getName());
407 public static StringBuilder
appendDescriptor(
final StringBuilder buf,
final Class<?> c,
final boolean useTrueType) {
408 if (c.isPrimitive()) {
409 if (c == Boolean.TYPE) buf.append(
"Z");
410 else if (c == Byte.TYPE) buf.append(
"B");
411 else if (c == Character.TYPE) buf.append(
"C");
412 else if (c == Short.TYPE) buf.append(
"S");
413 else if (c == Integer.TYPE) buf.append(
"I");
414 else if (c == Long.TYPE) buf.append(
"J");
415 else if (c == Float.TYPE) buf.append(
"F");
416 else if (c == Double.TYPE) buf.append(
"D");
417 else throw new RuntimeException(
"Illegal primitive type \"" + c.getName() +
"\"");
428 final Class<?> componentType = c.getComponentType();
432 (componentType == java.nio.ByteBuffer.class));
435 buf.append(c.getName().replace(
'.',
'/'));
442 }
else if (c == java.lang.String.class) {
444 buf.append(c.getName().replace(
'.',
'/'));
447 buf.append(
"Ljava/lang/Object;");
471 public static StringBuilder
appendJNIDescriptor(
final StringBuilder res,
final Class<?> c,
final boolean useTrueType) {
472 final int start = res.length();
489 return descriptor.replace(
"_",
"_1")
510 replace(descriptor, start,
"_",
"_1");
511 replace(descriptor, start,
"/",
"_");
512 replace(descriptor, start,
";",
"_2");
513 replace(descriptor, start,
"[",
"_3");
516 private static StringBuilder replace(
final StringBuilder buf,
int start,
final String target,
final String replacement) {
517 start = buf.indexOf(target, start);
518 while( 0 <= start ) {
519 buf.replace(start, start + target.length(), replacement);
520 start = buf.indexOf(target, start + replacement.length());
536 return "jobjectArray /* of ByteBuffer */";
539 if ( clazzName !=
null ) {
552 return "j" + clazz.getName();
562 return "jobjectArray /*elements are String*/";
565 final Class<?> elementType = clazz.getComponentType();
568 return "jobjectArray /*elements are " + elementType.
getName() +
"*/";
571 if (elementType.isArray()) {
574 if (elementType.getComponentType().isPrimitive()) {
576 return "jobjectArray /* elements are " + elementType.getComponentType() +
"[]*/";
579 throw new RuntimeException(
"Multi-dimensional arrays of types that are not primitives or Strings are not supported.");
584 throw new RuntimeException(
"Unexpected and unsupported array type: \"" +
this +
"\"");
597 return clazz !=
null && ( java.nio.Buffer.class.isAssignableFrom(clazz) ||
598 com.jogamp.common.nio.NativeBuffer.class.isAssignableFrom(clazz)) ;
602 return (clazz == java.nio.ByteBuffer.class);
606 return (
this == nioByteBufferArrayType);
610 return (
isArray() && (java.nio.Buffer.class.isAssignableFrom(clazz.getComponentType())));
614 return (clazz == java.nio.LongBuffer.class);
618 return (clazz == com.jogamp.common.nio.PointerBuffer.class);
622 return (clazz == java.lang.String.class);
632 return ((clazz !=
null) && clazz.isArray());
636 return (clazz !=
null && clazz.isArray() && clazz.getComponentType() == Float.TYPE);
640 return (clazz !=
null && clazz.isArray() && clazz.getComponentType() == Double.TYPE);
644 return (clazz !=
null && clazz.isArray() && clazz.getComponentType() == Byte.TYPE);
648 return (clazz !=
null && clazz.isArray() && clazz.getComponentType() == Integer.TYPE);
652 return (clazz !=
null && clazz.isArray() && clazz.getComponentType() == Short.TYPE);
656 return (clazz !=
null && clazz.isArray() && clazz.getComponentType() == Long.TYPE);
660 return (clazz !=
null && clazz.isArray() && clazz.getComponentType() == java.lang.String.class);
666 return ((clazz !=
null) && !
isArray() && clazz.isPrimitive() && (clazz != Void.TYPE));
670 return (
isArray() && (clazz.getComponentType().isPrimitive()));
674 return (clazz == Short.TYPE);
678 return (clazz == Float.TYPE);
682 return (clazz == Double.TYPE);
686 return (clazz == Byte.TYPE);
690 return (clazz == Long.TYPE);
694 return (clazz == Integer.TYPE);
698 return (clazz == Void.TYPE);
702 return clazzName !=
null;
706 return structName !=
null && !
isJNIEnv();
710 return elementType !=
null;
715 return primitivePointerType !=
null;
719 return C_PTR.VOID.equals(primitivePointerType);
723 return C_PTR.CHAR.equals(primitivePointerType);
727 return C_PTR.SHORT.equals(primitivePointerType);
731 return C_PTR.INT32.equals(primitivePointerType);
735 return C_PTR.INT64.equals(primitivePointerType);
739 return C_PTR.FLOAT.equals(primitivePointerType);
743 return C_PTR.DOUBLE.equals(primitivePointerType);
747 return "JNIEnv".equals(structName);
764 private void append(
final StringBuilder sb,
final String val,
final boolean prepComma) {
772 sb =
new StringBuilder();
774 boolean prepComma =
false;
776 final String javaTypeName =
getName();
777 if(
null != javaTypeName ) {
778 append(sb, javaTypeName,
false);
780 append(sb,
"ANON",
false);
783 if(
null != clazz ) {
784 append(sb,
"clazz = "+clazz.getName(), prepComma); prepComma=
true;
786 if(
null != clazzName ) {
787 append(sb,
"clazzName = "+clazzName, prepComma); prepComma=
true;
789 if(
null != structName ) {
790 append(sb,
"struct = "+structName, prepComma); prepComma=
true;
792 if(
null != elementType ) {
793 append(sb,
"elementType = "+elementType, prepComma); prepComma=
true;
795 if(
null != primitivePointerType ) {
796 append(sb,
"primitivePointerType = "+primitivePointerType, prepComma); prepComma=
true;
798 append(sb,
"is[", prepComma); prepComma=
false;
801 append(sb,
"opaque", prepComma); prepComma=
true;
804 sb.append(
"pascal ");
807 append(sb,
"string", prepComma); prepComma=
true;
810 append(sb,
"stringArray", prepComma); prepComma=
true;
812 append(sb,
"array", prepComma); prepComma=
true;
815 append(sb,
"compoundArray", prepComma); prepComma=
true;
818 append(sb,
"compound", prepComma); prepComma=
true;
821 append(sb,
"primitive", prepComma); prepComma=
true;
824 append(sb,
"primitiveArray", prepComma); prepComma=
true;
827 append(sb,
"nioBuffer", prepComma); prepComma=
true;
830 append(sb,
"nioBufferArray", prepComma); prepComma=
true;
833 append(sb,
"C-Primitive-Pointer", prepComma); prepComma=
true;
836 append(sb,
"], descriptor '"+
getDescriptor()+
"'",
false); prepComma=
true;
842 final StringBuilder sb =
new StringBuilder();
846 return sb.toString();
854 if(
null == clazz ) {
855 throw new IllegalArgumentException(
"null clazz passed");
857 this.primitivePointerType =
null;
859 this.clazzName =
null;
860 this.structName =
null;
861 this.elementType =
null;
862 this.opaqued = opaqued;
867 private JavaType(
final String clazzName,
final String structName) {
868 if(
null != clazzName &&
null != structName ) {
869 throw new IllegalArgumentException(
"Both clazzName and structName set");
871 if(
null != clazzName ) {
872 this.clazzName = clazzName;
873 this.structName =
null;
874 }
else if(
null != structName ) {
875 this.clazzName =
null;
876 this.structName = structName;
878 throw new IllegalArgumentException(
"Neither clazzName nor structName set");
880 this.primitivePointerType =
null;
882 this.elementType =
null;
883 this.opaqued =
false;
884 this.pascalStrElem =
null;
889 private JavaType(
final C_PTR primitivePointerType) {
890 if(
null == primitivePointerType ) {
891 throw new IllegalArgumentException(
"null primitivePointerType passed");
893 this.primitivePointerType = primitivePointerType;
895 this.clazzName =
null;
896 this.structName =
null;
897 this.elementType =
null;
898 this.opaqued =
false;
899 this.pascalStrElem =
null;
903 private JavaType(
final Type elementType) {
904 if(
null == elementType ) {
905 throw new IllegalArgumentException(
"null elementType passed");
907 this.primitivePointerType =
null;
909 this.clazzName =
null;
910 this.structName =
null;
911 this.elementType = elementType;
912 this.opaqued =
false;
913 this.pascalStrElem =
null;
917 private JavaType(
final C_PTR primitivePointerType,
final Class<?> clazz,
final String clazzName,
final String structName,
final Type elementType,
920 this.primitivePointerType = primitivePointerType;
922 this.clazzName = clazzName;
923 this.structName = structName;
924 this.elementType = elementType;
925 this.opaqued =
false;
930 this(o.primitivePointerType, o.clazz, o.clazzName, o.structName, o.elementType, o.
pascalStrElem);
934 this(o.primitivePointerType, o.clazz, o.clazzName, o.structName, o.elementType,
pascalStrElem);
937 private static String arrayName(Class<?> clazz) {
938 final StringBuilder buf =
new StringBuilder();
940 while (clazz.isArray()) {
942 clazz = clazz.getComponentType();
944 buf.append(clazz.getName());
945 while (--arrayCount >= 0) {
948 return buf.toString();
951 private static String arrayDescriptor(Class<?> clazz) {
952 final StringBuilder buf =
new StringBuilder();
953 while (clazz.isArray()) {
955 clazz = clazz.getComponentType();
957 buf.append(descriptor(clazz));
958 return buf.toString();
961 private static String descriptor(
final Class<?> clazz) {
962 if (clazz.isPrimitive()) {
963 if (clazz == Boolean.TYPE)
return "Z";
964 if (clazz == Byte.TYPE)
return "B";
965 if (clazz == Double.TYPE)
return "D";
966 if (clazz == Float.TYPE)
return "F";
967 if (clazz == Integer.TYPE)
return "I";
968 if (clazz == Long.TYPE)
return "J";
969 if (clazz == Short.TYPE)
return "S";
970 if (clazz == Void.TYPE)
return "V";
971 throw new RuntimeException(
"Unexpected primitive type " + clazz.getName());
973 if (clazz.isArray()) {
974 return arrayDescriptor(clazz);
976 return descriptor(clazz.getName());
979 private static String descriptor(
final String clazzName) {
980 return "L" + clazzName.replace(
'.',
'/') +
";";
Parses and provides access to the contents of .cfg files for the JavaEmitter.
String packageForStruct(final String structName)
Returns the package into which to place the glue code for accessing the specified struct.
Pascal string argument index tuple for length and value.
static final List< Integer > pushValueIndex(final List< PascalStringElem > source, List< Integer > indices)
static PascalStringElem getByLengthIdx(final List< PascalStringElem > pascals, final int lengthIdx)
static final List< Integer > pushLengthIndex(final List< PascalStringElem > source)
static PascalStringElem getByValueIdx(final List< PascalStringElem > pascals, final int valueIdx)
Describes a java-side representation of a type that is used to represent the same data on both the Ja...
static StringBuilder appendDescriptor(final StringBuilder buf, final Class<?> c, final boolean useTrueType)
Appends the descriptor (internal type signature) corresponding to the given Class<?...
boolean isNIOPointerBuffer()
static JavaType forNIOBufferClass()
boolean isCInt32PointerType()
boolean isNIOBufferArray()
static JavaType forNIOByteBufferArrayClass()
boolean isCFloatPointerType()
static StringBuilder appendJNIDescriptor(final StringBuilder res, final Class<?> c, final boolean useTrueType)
Appends the native (JNI) method-name descriptor corresponding to the given Class<?...
boolean equals(final Object arg)
boolean isNIOByteBufferArray()
static JavaType forNIOByteBufferClass()
String getName()
Returns the Java type name corresponding to this type.
static StringBuilder toJNIMethodDescriptor(final StringBuilder descriptor, final int start)
Converts the assumed descriptor (internal type signature) to a native (JNI) method-name descriptor,...
static JavaType createForCStruct(final String name)
Creates a JavaType corresponding to the specified C CompoundType name; for example,...
boolean isNIOLongBuffer()
static JavaType forNIODoubleBufferClass()
static JavaType forNIOShortBufferClass()
boolean isCShortPointerType()
static JavaType createForCShortPointer()
static JavaType createForNamedClass(final String name)
Creates a JavaType corresponding to the given named Java class, not necessarily existing yet.
static JavaType createForCCharPointer()
String getJNIMethodDesciptor()
Returns the native (JNI) method-name descriptor corresponding to this type, i.e.
boolean isCompoundTypeWrapper()
boolean isPascalStrArray()
static JavaType createForCVoidPointer()
static JavaType createForCArray(final Type elementType)
Creates a JavaType corresponding to an array of the given element type.
static JavaType createForJNIEnv()
boolean isPrimitiveArray()
JavaType(final JavaType o)
Copy ctor.
static JavaType forNIOLongBufferClass()
final PascalStringElem pascalStrElem
static JavaType forNIOFloatBufferClass()
static JavaType createForCInt32Pointer()
boolean isCVoidPointerType()
static JavaType createForClass(final Class<?> clazz)
Creates a JavaType corresponding to the given Java type.
JavaType(final JavaType o, final PascalStringElem pascalStrElem)
Copy ctor w/ pascalString variant override.
static JavaType forObjectClass()
static JavaType createForOpaqueClass(final Class<?> clazz)
Creates a JavaType corresponding to the given opaque Java type.
String jniTypeName()
Returns the String corresponding to the JNI type for this type, or NULL if it can't be represented (i...
static JavaType createForCInt64Pointer()
boolean isCCharPointerType()
static JavaType forNIOIntBufferClass()
static JavaType createForStringClass(final Class<?> clazz, final PascalStringElem pascalStrElem)
boolean isCPrimitivePointerType()
boolean isPascalStrElem()
String getDescriptor()
Returns the descriptor (internal type signature) corresponding to this type.
boolean isArrayOfCompoundTypeWrappers()
static String toJNIMethodDescriptor(final String descriptor)
Converts the assumed descriptor (internal type signature) to a native (JNI) method-name descriptor,...
JavaType getElementType()
static JavaType createForCDoublePointer()
boolean isNIOByteBuffer()
static JavaType createForCFloatPointer()
String getDescriptor(final JavaConfiguration cfg)
String getName(final JavaConfiguration cfg)
static JavaType forNIOPointerBufferClass()
boolean isCInt64PointerType()
Class<?> getJavaClass()
Returns the Java Class corresponding to this type.
final StringBuilder getSignature(StringBuilder sb)
boolean isCDoublePointerType()
final int hashCode()
Hashcode for Types.
final String getName()
Returns the name of this type.
final boolean equals(final Object arg)
Equality test for Types inclusive its given name.