java.lang.reflect.Field#getByte ( )源码实例Demo

下面列出了java.lang.reflect.Field#getByte ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: Serial   文件: SerializerDefsTests.java
@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");
            }
        }
    }
}
 
源代码2 项目: openjdk-jdk9   文件: TestInstanceCloneUtils.java
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");
}
 
源代码3 项目: hbase   文件: TestHFileOutputFormat2.java
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;
}
 
源代码4 项目: ghidra   文件: ValueFormats.java
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;
}
 
源代码5 项目: hasor   文件: InstructionInfo.java
@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();
}
 
源代码6 项目: hbase   文件: HFileSystem.java
/**
 * 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;
}
 
源代码7 项目: a   文件: A.java
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";
}
 
源代码8 项目: c2mon   文件: HardwareAddressImpl.java
@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;
}
 
源代码9 项目: j2objc   文件: FieldTest.java
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;
}