diff --git a/SwerveRoboticsLibrary/src/main/java/org/swerverobotics/library/internal/Util.java b/SwerveRoboticsLibrary/src/main/java/org/swerverobotics/library/internal/Util.java index 6150bfe0e2a..652e99ba8fc 100644 --- a/SwerveRoboticsLibrary/src/main/java/org/swerverobotics/library/internal/Util.java +++ b/SwerveRoboticsLibrary/src/main/java/org/swerverobotics/library/internal/Util.java @@ -2,14 +2,12 @@ import com.qualcomm.robotcore.hardware.*; import com.qualcomm.robotcore.util.*; -import org.swerverobotics.library.*; + import org.swerverobotics.library.exceptions.*; import java.lang.reflect.*; import java.util.*; import java.util.concurrent.*; -import static junit.framework.Assert.*; - /** * Various internal utilities that assist us. */ @@ -214,23 +212,84 @@ static public List getDeclaredMethodsIncludingSuper(Class clazz) // Private field access - utility //---------------------------------------------------------------------------------------------- - // The Field of the 'fieldDexIndex' field of class Field + // Dalvik VM: the Field of the 'fieldDexIndex' field of class Field static Field fieldDexIndexField = getFieldDexIndexField(); + // ART VM: The Field of the 'artField' field of class Field (Lollipop and greater) + static Field fieldArtField = getFieldArtField(); + // ART VM: The dexIndex field of an ArtField + static Field fieldArtFieldDexIndex = getFieldArtFieldDexIndex(); + static Field getFieldDexIndexField() // Find the Field for the 'fieldDexIndex' field of class Field. + // This works on the Dalvik VM. { - Class fieldClass = Field.class; - Class fieldSuperClass = fieldClass.getSuperclass(); - assertTrue(!BuildConfig.DEBUG || fieldSuperClass.getSuperclass() == Object.class); + Field result = null; + try { + Class fieldClass = Field.class; + Class fieldSuperClass = fieldClass.getSuperclass(); - List superFields = getLocalDeclaredNonStaticFields(fieldSuperClass, false); - List fieldFields = getLocalDeclaredNonStaticFields(fieldClass, false); + List superFields = getLocalDeclaredNonStaticFields(fieldSuperClass, false); + List fieldFields = getLocalDeclaredNonStaticFields(fieldClass, false); - int iFieldTarget = 7; + int iFieldTarget = 7; - Field result = fieldFields.get(iFieldTarget - superFields.size()); - if (!result.isAccessible()) + result = fieldFields.get(iFieldTarget - superFields.size()); + if (!result.isAccessible()) + result.setAccessible(true); + } + catch (Exception ignored) + { + result = null; + } + return result; + } + + static Field getFieldArtField() + // Find the Field for the 'artField' field of class Field + // This works on the ART VM. + { + return getFieldByName(Field.class, "artField"); + } + + static Field getFieldArtFieldDexIndex() + // Field the 'fieldDexIndex' field of the class ArtField + // This works on the ART VM. + { + Field result = null; + try { + Class classArtField = Class.forName("java.lang.reflect.ArtField"); + if (classArtField != null) + return getFieldByName(classArtField, "fieldDexIndex"); + } + catch (Exception e) + { + result = null; + } + return result; + } + + static Field getFieldByName(Class klass, String name) + { + Field result = null; + try { + List fieldFields = getLocalDeclaredNonStaticFields(klass, false); + + for (Field field : fieldFields) + { + if (field.getName().equals(name)) + { + result = field; + break; + } + } + } + catch (Exception e) + { + result = null; + } + + if (result != null && !result.isAccessible()) result.setAccessible(true); return result; @@ -248,13 +307,26 @@ static Field getFieldDexIndexField() static int getSortIndex(Field field) // Returns a sort key that will sort Fields in their declared order { + int result = -1; try { - return fieldDexIndexField.getInt(field); + if (fieldDexIndexField != null) + { + // Dalvik VM + result = fieldDexIndexField.getInt(field); + } + + else if (fieldArtField != null) + { + // ART VM + Object artField = fieldArtField.get(field); + result = fieldArtFieldDexIndex.getInt(artField); + } } catch (IllegalAccessException e) { throw SwerveRuntimeException.wrap(e); } + return result; } //---------------------------------------------------------------------------------------------- @@ -293,7 +365,8 @@ static public List getDeclaredNonStaticFieldsIncludingSuper(Class claz { if (clazz.getSuperclass() == null) { - return getLocalDeclaredNonStaticFields(clazz, sort); + List result = /*getLocalDeclaredNonStaticFields(clazz, sort)*/ /*only want user-visible ones*/ new LinkedList(); + return result; } else {