Java Code Examples for java.lang.reflect.Field#getByte()

The following examples show how to use java.lang.reflect.Field#getByte() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Serial   File: SerializerDefsTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTypeName() throws IllegalAccessException {
    assertThat(SerializerDefs.getTypeName(SerializerDefs.TYPE_UNKNOWN))
            .contains("unknown");

    for (Field field : SerializerDefs.class.getFields()) {
        if (field.getName().startsWith("TYPE_")) {
            final byte type = field.getByte(null);
            if (type != SerializerDefs.TYPE_UNKNOWN) {
                assertThat(SerializerDefs.getTypeName(type))
                        .as("Type name is unknown: " + field.getName())
                        .doesNotContain("unknown");
            }
        }
    }
}
 
Example 2
int getVal(Field f) {
    try {
        if (f.getType() == int.class) {
            return f.getInt(this);
        } else if (f.getType() == short.class) {
            return (int)f.getShort(this);
        } else if (f.getType() == byte.class) {
            return (int)f.getByte(this);
        } else if (f.getType() == long.class) {
            return (int)f.getLong(this);
        }
    } catch(IllegalAccessException iae) {
        throw new RuntimeException("Setting fields failed");
    }
    throw new RuntimeException("unexpected field type");
}
 
Example 3
Source Project: hbase   File: TestHFileOutputFormat2.java    License: Apache License 2.0 6 votes vote down vote up
private String getStoragePolicyNameForOldHDFSVersion(FileSystem fs, Path path) {
  try {
    if (fs instanceof DistributedFileSystem) {
      DistributedFileSystem dfs = (DistributedFileSystem) fs;
      HdfsFileStatus status = dfs.getClient().getFileInfo(path.toUri().getPath());
      if (null != status) {
        byte storagePolicyId = status.getStoragePolicy();
        Field idUnspecified = BlockStoragePolicySuite.class.getField("ID_UNSPECIFIED");
        if (storagePolicyId != idUnspecified.getByte(BlockStoragePolicySuite.class)) {
          BlockStoragePolicy[] policies = dfs.getStoragePolicies();
          for (BlockStoragePolicy policy : policies) {
            if (policy.getId() == storagePolicyId) {
              return policy.getName();
            }
          }
        }
      }
    }
  } catch (Throwable e) {
    LOG.warn("failed to get block storage policy of [" + path + "]", e);
  }

  return null;
}
 
Example 4
Source Project: ghidra   File: ValueFormats.java    License: Apache License 2.0 5 votes vote down vote up
public final static String toString( byte value ) {
	try {
		Field [ ] fields = ValueFormats.class.getDeclaredFields( );
		for ( Field field : fields ) {
			if ( field.getByte( null ) == value ) {
				return field.getName( );
			}
		}
	}
	catch ( Exception e ) {
		// ignore
	}
	return "Value:" + value;
}
 
Example 5
Source Project: hasor   File: InstructionInfo.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public String toString() {
    StringBuilder codeName = new StringBuilder();
    try {
        //
        Field[] fields = Opcodes.class.getFields();
        for (Field field : fields) {
            byte aByte = field.getByte(null);
            if (aByte == this.instCode) {
                codeName.append(field.getName());
                break;
            }
        }
        //
    } catch (IllegalAccessException e) {
        codeName.append("error : ");
        codeName.append(e.getMessage());
        return codeName.toString();
    }
    //
    int needSpace = 10 - codeName.length();
    if (needSpace > 0) {
        codeName.append(StringUtils.leftPad("", needSpace, ' '));
    }
    for (int i = 0; i < this.instParam.length; i++) {
        if (i > 0) {
            codeName.append(", ");
        }
        codeName.append(this.instParam[i]);
    }
    //
    return codeName.toString().trim();
}
 
Example 6
Source Project: hbase   File: HFileSystem.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Before Hadoop 2.8.0, there's no getStoragePolicy method for FileSystem interface, and we need
 * to keep compatible with it. See HADOOP-12161 for more details.
 * @param path Path to get storage policy against
 * @return the storage policy name
 */
private String getStoragePolicyForOldHDFSVersion(Path path) {
  try {
    if (this.fs instanceof DistributedFileSystem) {
      DistributedFileSystem dfs = (DistributedFileSystem) this.fs;
      HdfsFileStatus status = dfs.getClient().getFileInfo(path.toUri().getPath());
      if (null != status) {
        if (unspecifiedStoragePolicyId < 0) {
          // Get the unspecified id field through reflection to avoid compilation error.
          // In later version BlockStoragePolicySuite#ID_UNSPECIFIED is moved to
          // HdfsConstants#BLOCK_STORAGE_POLICY_ID_UNSPECIFIED
          Field idUnspecified = BlockStoragePolicySuite.class.getField("ID_UNSPECIFIED");
          unspecifiedStoragePolicyId = idUnspecified.getByte(BlockStoragePolicySuite.class);
        }
        byte storagePolicyId = status.getStoragePolicy();
        if (storagePolicyId != unspecifiedStoragePolicyId) {
          BlockStoragePolicy[] policies = dfs.getStoragePolicies();
          for (BlockStoragePolicy policy : policies) {
            if (policy.getId() == storagePolicyId) {
              return policy.getName();
            }
          }
        }
      }
    }
  } catch (Throwable e) {
    LOG.warn("failed to get block storage policy of [" + path + "]", e);
  }

  return null;
}
 
Example 7
Source Project: a   File: A.java    License: Apache License 2.0 5 votes vote down vote up
protected String dataStructureTypeToString(byte dataStructureType)  {
	try{
		for(Field field : CommandTypes.class.getFields()) {
			String name = field.getName();
			byte value = field.getByte(null);
			if( dataStructureType == value ) {
				return name;
			}
		}
	}catch(Exception e){
		return "unknown";
	}
	return "unknown";
}
 
Example 8
@Override
public final int hashCode() {

  int result = 0;

  Field[] fields = this.getClass().getDeclaredFields();

  for (Field field : fields) {
    // compare non-final, non-static and non-transient fields only
    if (!Modifier.isFinal(field.getModifiers()) && !Modifier.isStatic(field.getModifiers())
        && !Modifier.isTransient(field.getModifiers())) {
      try {

        // skip arrays
        if (!field.getType().isArray() && field.get(this) != null) {
          // for string take its length
          if (field.getType().equals(String.class)) {
            result ^= ((String) field.get(this)).length();
          } else if (field.getType().equals(short.class) || field.getType().equals(Short.class)) {
            result ^= field.getShort(this);
          } else if (field.getType().equals(int.class) || field.getType().equals(Integer.class)) {
            result ^= field.getInt(this);
          } else if (field.getType().equals(float.class) || field.getType().equals(Float.class)) {
            result ^= (int) field.getFloat(this);
          } else if (field.getType().equals(double.class) || field.getType().equals(Double.class)) {
            result ^= (int) field.getDouble(this);
          } else if (field.getType().equals(long.class) || field.getType().equals(Long.class)) {
            result ^= (int) field.getLong(this);
          } else if (field.getType().equals(byte.class) || field.getType().equals(Byte.class)) {
            result ^= field.getByte(this);
          } else if (field.getType().equals(boolean.class) || field.getType().equals(Boolean.class)) {
            result ^= field.getBoolean(this) == Boolean.TRUE ? 1 : 0;
          }
        }
      } catch (Exception e) {
        log.error(e.toString());
        throw new RuntimeException("Exception caught while calculating HardwareAddress hashcode.", e);
      }
    }
  }
  return result;
}
 
Example 9
Source Project: j2objc   File: FieldTest.java    License: Apache License 2.0 4 votes vote down vote up
Object getField(char primitiveType, Object o, Field f,
        Class expected) {
    Object res = null;
    try {
        primitiveType = Character.toUpperCase(primitiveType);
        switch (primitiveType) {
        case 'I': // int
            res = new Integer(f.getInt(o));
            break;
        case 'J': // long
            res = new Long(f.getLong(o));
            break;
        case 'Z': // boolean
            res = new Boolean(f.getBoolean(o));
            break;
        case 'S': // short
            res = new Short(f.getShort(o));
            break;
        case 'B': // byte
            res = new Byte(f.getByte(o));
            break;
        case 'C': // char
            res = new Character(f.getChar(o));
            break;
        case 'D': // double
            res = new Double(f.getDouble(o));
            break;
        case 'F': // float
            res = new Float(f.getFloat(o));
            break;
        default:
            res = f.get(o);
        }
        // Since 2011, members are always accessible and throwing is optional
        assertTrue("expected " + expected + " for " + f.getName(),
                expected == null || expected == IllegalAccessException.class);
    } catch (Exception e) {
        if (expected == null) {
            fail("unexpected exception " + e);
        } else {
            assertTrue("expected exception "
                    + expected.getName() + " and got " + e, e
                    .getClass().equals(expected));
        }
    }
    return res;
}