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

The following examples show how to use java.lang.reflect.Field#getFloat() . 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 File: Arguments.java    From h2o-2 with Apache License 2.0 6 votes vote down vote up
@Override public String toString() {
  Field[] fields = getFields(this);
  String r="";
  for( Field field : fields ){
    String name = field.getName();
    Class cl = field.getType();
    try{
      if( cl.isPrimitive() ){
        if( cl == Boolean.TYPE ){
          boolean curval = field.getBoolean(this);
          if( curval ) r += " -"+name;
        }
        else if( cl == Integer.TYPE ) r+=" -"+name+"="+field.getInt(this);
        else if( cl == Float.TYPE )  r+=" -"+name+"="+field.getFloat(this);
        else if( cl == Double.TYPE )  r+=" -"+name+"="+field.getDouble(this);
        else if( cl == Long.TYPE )  r+=" -"+name+"="+field.getLong(this);
        else continue;
      } else if( cl == String.class )
        if (field.get(this)!=null) r+=" -"+name+"="+field.get(this);
    } catch( Exception e ) { Log.err("Argument failed with ",e); }
  }
  return r;
}
 
Example 2
Source File: HealthBarRenderPowerPatch.java    From StSLib with MIT License 5 votes vote down vote up
public static void Postfix(AbstractCreature __instance, SpriteBatch sb, float x, float y)
{
    try {
        Field f = AbstractCreature.class.getDeclaredField("targetHealthBarWidth");
        f.setAccessible(true);

        float targetHealthBarWidth = f.getFloat(__instance);
        targetHealthBarWidth += nonPoisonWidthSum;
        nonPoisonWidthSum = 0;
        f.setFloat(__instance, targetHealthBarWidth);
    } catch (IllegalAccessException | NoSuchFieldException e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: GT_TileEntity_DEHP.java    From bartworks with MIT License 5 votes vote down vote up
@Override
@SuppressWarnings("rawtypes")
public void onConfigLoad(GT_Config aConfig) {
    try {
        Class c = TileEntityNuclearReactorElectric.class;
        Field f = c.getDeclaredField("huOutputModifier");
        f.setAccessible(true);
        GT_TileEntity_DEHP.nulearHeatMod = f.getFloat(f);
    } catch (SecurityException | IllegalArgumentException | ExceptionInInitializerError | NullPointerException | IllegalAccessException | NoSuchFieldException e) {
        e.printStackTrace();
    }
    super.onConfigLoad(aConfig);
}
 
Example 4
Source File: PlayerControllerUtils.java    From ForgeWurst with GNU General Public License v3.0 5 votes vote down vote up
public static float getCurBlockDamageMP()
	throws ReflectiveOperationException
{
	Field field = PlayerControllerMP.class.getDeclaredField(
		wurst.isObfuscated() ? "field_78770_f" : "curBlockDamageMP");
	field.setAccessible(true);
	return field.getFloat(mc.playerController);
}
 
Example 5
Source File: DBObject.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
private float getFloatValue(String name)
{
    float result = 0.0f;
    try {
        Field field = this.getClass().getDeclaredField(name);
        result = field.getFloat(this);
    } catch (Exception e) {
        throw new IllegalStateException(e);

    }
    return result;
}
 
Example 6
Source File: LineHeightEditText.java    From LineHeightEditText with Apache License 2.0 5 votes vote down vote up
private void getLineSpacingAddAndLineSpacingMult() {
    try {
        Field mSpacingAddField = TextView.class.getDeclaredField("mSpacingAdd");
        Field mSpacingMultField = TextView.class.getDeclaredField("mSpacingMult");
        mSpacingAddField.setAccessible(true);
        mSpacingMultField.setAccessible(true);
        mSpacingAdd = mSpacingAddField.getFloat(this);
        mSpacingMult = mSpacingMultField.getFloat(this);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: ReflectionUtils.java    From ProtocolSupportPocketStuff with GNU Affero General Public License v3.0 5 votes vote down vote up
public static float getFloat(Field field, Object source) {
	try {
		return field.getFloat(source);
	} catch (Exception e) {
		e.printStackTrace();
	}
	throw new RuntimeException("Error while getting field \"" + field.getName() + "\" from " + source + "!");
}
 
Example 8
Source File: PreferencesDao.java    From photoviewer with Apache License 2.0 5 votes vote down vote up
private void putValueToEditor(SharedPreferences.Editor editor, String preferenceName, T model, Field field) {
    field.setAccessible(true);

    try {
        if (field.getType().equals(boolean.class)) {
            boolean booleanValue = field.getBoolean(model);
            editor.putBoolean(preferenceName, booleanValue);
            return;
        }
        if (field.getType().equals(int.class)) {
            int integerValue = field.getInt(model);
            editor.putInt(preferenceName, integerValue);
            return;
        }
        if (field.getType().equals(float.class)) {
            float floatValue = field.getFloat(model);
            editor.putFloat(preferenceName, floatValue);
            return;
        }
        if (field.getType().equals(long.class)) {
            long longValue = field.getLong(model);
            editor.putLong(preferenceName, longValue);
            return;
        }
        if (field.getType().equals(String.class)) {
            String stringValue = (String) field.get(model);
            editor.putString(preferenceName, stringValue);
            return;
        }

        Gson gson = new Gson();
        editor.putString(preferenceName, gson.toJson(field.get(model)));
    } catch (IllegalAccessException e) {
        Log.wtf(TAG, "Exception during converting of Field's value to Preference", e);
    }
}
 
Example 9
Source File: Utilities.java    From Carbon with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns 0 if there's an error accessing the "strength" field in the minecraft server
 * Blocks class, otherwise, returns the block's given strength.
 * @param b
 * @return
 */
public static float getBlockStrength(net.minecraft.server.v1_7_R4.Block b) {
    try {
        Field field = b.getClass().getField("strength");
        field.setAccessible(true);
        return field.getFloat(b);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
        Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
    }
    return 0;
}
 
Example 10
Source File: Utilities.java    From Carbon with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * Returns 0 if there's an error accessing the "durability" field in the minecraft server
 * Blocks class, otherwise, returns the block's given strength.
 * @param b
 * @return
 */
public static float getBlockDurability(net.minecraft.server.v1_7_R4.Block b) {
    try {
        Field field = b.getClass().getField("durability");
        field.setAccessible(true);
        return field.getFloat(b);
    } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException ex) {
        Logger.getLogger(Utilities.class.getName()).log(Level.SEVERE, null, ex);
    }
    return 0;
}
 
Example 11
Source File: HardwareAddressImpl.java    From c2mon with GNU Lesser General Public License v3.0 4 votes vote down vote up
@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 12
Source File: FieldTest.java    From j2objc with 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;
}
 
Example 13
Source File: FieldByFieldComparison.java    From byte-buddy with Apache License 2.0 4 votes vote down vote up
private static boolean matches(Class<?> type, Object left, Object right, Set<Pair> visited) throws Exception {
    if (!visited.add(new Pair(left, right))) {
        return true;
    } else if (mockingDetails(left).isMock() || mockingDetails(left).isSpy() || mockingDetails(right).isMock() || mockingDetails(right).isSpy()) {
        return left == right;
    }
    while (type.getName().startsWith("net.bytebuddy.")) {
        for (Field field : type.getDeclaredFields()) {
            if (Modifier.isStatic(field.getModifiers()) || field.isSynthetic() && !field.getName().equals("this$0")) {
                continue;
            }
            HashCodeAndEqualsPlugin.ValueHandling valueHandling = field.getAnnotation(HashCodeAndEqualsPlugin.ValueHandling.class);
            if (valueHandling != null && valueHandling.value() == HashCodeAndEqualsPlugin.ValueHandling.Sort.IGNORE) {
                continue;
            }
            field.setAccessible(true);
            if (field.getType() == boolean.class) {
                if (field.getBoolean(left) != field.getBoolean(right)) {
                    return false;
                }
            } else if (field.getType() == byte.class) {
                if (field.getBoolean(left) != field.getBoolean(right)) {
                    return false;
                }
            } else if (field.getType() == short.class) {
                if (field.getShort(left) != field.getShort(right)) {
                    return false;
                }
            } else if (field.getType() == char.class) {
                if (field.getChar(left) != field.getChar(right)) {
                    return false;
                }
            } else if (field.getType() == int.class) {
                if (field.getInt(left) != field.getInt(right)) {
                    return false;
                }
            } else if (field.getType() == long.class) {
                if (field.getLong(left) != field.getLong(right)) {
                    return false;
                }
            } else if (field.getType() == float.class) {
                if (field.getFloat(left) != field.getFloat(right)) {
                    return false;
                }
            } else if (field.getType() == double.class) {
                if (field.getDouble(left) != field.getDouble(right)) {
                    return false;
                }
            } else if (field.getType().isEnum()) {
                if (field.get(left) != field.get(right)) {
                    return false;
                }
            } else {
                Object leftObject = field.get(left), rightObject = field.get(right);
                if (mockingDetails(leftObject).isMock() || mockingDetails(rightObject).isSpy() || mockingDetails(rightObject).isMock() || mockingDetails(rightObject).isSpy()) {
                    if (leftObject != rightObject) {
                        return false;
                    }
                } else if (Iterable.class.isAssignableFrom(field.getType())) {
                    if (rightObject == null) {
                        return false;
                    }
                    Iterator<?> rightIterable = ((Iterable<?>) rightObject).iterator();
                    for (Object instance : (Iterable<?>) leftObject) {
                        if (!rightIterable.hasNext() || !matches(instance.getClass(), instance, rightIterable.next(), visited)) {
                            return false;
                        }
                    }
                } else if (field.getType().getName().startsWith("net.bytebuddy.")) {
                    if (leftObject == null ? rightObject != null : !matches(leftObject.getClass(), leftObject, rightObject, visited)) {
                        return false;
                    }
                } else {
                    if (leftObject == null ? rightObject != null : !leftObject.equals(rightObject)) {
                        return false;
                    }
                }
            }
        }
        type = type.getSuperclass();
    }
    return true;
}