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

The following examples show how to use java.lang.reflect.Field#getDouble() . 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: h2o-2   File: Arguments.java    License: 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
void serialize(AbstractHessianOutput out, Object obj, Field field)
  throws IOException
{
  double value = 0;

  try {
    value = field.getDouble(obj);
  } catch (IllegalAccessException e) {
    log.log(Level.FINE, e.toString(), e);
  }

  out.writeDouble(value);
}
 
Example 3
Source Project: joyrpc   File: JavaSerializer.java    License: Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field) throws IOException {
    double value = 0;

    try {
        value = field.getDouble(obj);
    } catch (IllegalAccessException e) {
        log.log(Level.FINE, e.toString(), e);
    }

    out.writeDouble(value);
}
 
Example 4
Source Project: sofa-hessian   File: JavaSerializer.java    License: Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field)
    throws IOException
{
    double value = 0;

    try {
        value = field.getDouble(obj);
    } catch (IllegalAccessException e) {
        log.log(Level.FINE, e.toString(), e);
    }

    out.writeDouble(value);
}
 
Example 5
@Override
void serialize(AbstractHessianOutput out, Object obj, Field field)
        throws IOException {
    double value = 0;

    try {
        value = field.getDouble(obj);
    } catch (IllegalAccessException e) {
        log.log(Level.FINE, e.toString(), e);
    }

    out.writeDouble(value);
}
 
Example 6
Source Project: dubbox   File: JavaSerializer.java    License: Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field)
     throws IOException
   {
     double value = 0;

     try {
value = field.getDouble(obj);
     } catch (IllegalAccessException e) {
log.log(Level.FINE, e.toString(), e);
     }

     out.writeDouble(value);
   }
 
Example 7
Source Project: dubbox-hystrix   File: JavaSerializer.java    License: Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field)
     throws IOException
   {
     double value = 0;

     try {
value = field.getDouble(obj);
     } catch (IllegalAccessException e) {
log.log(Level.FINE, e.toString(), e);
     }

     out.writeDouble(value);
   }
 
Example 8
public static double getDouble(Field field, Object source) {
	try {
		return field.getDouble(source);
	} catch (Exception e) {
		e.printStackTrace();
	}
	throw new RuntimeException("Error while getting field \"" + field.getName() + "\" from " + source + "!");
}
 
Example 9
Source Project: dubbo3   File: JavaSerializer.java    License: Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field)
        throws IOException {
    double value = 0;

    try {
        value = field.getDouble(obj);
    } catch (IllegalAccessException e) {
        log.log(Level.FINE, e.toString(), e);
    }

    out.writeDouble(value);
}
 
Example 10
Source Project: dubbox   File: JavaSerializer.java    License: Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field)
     throws IOException
   {
     double value = 0;

     try {
value = field.getDouble(obj);
     } catch (IllegalAccessException e) {
log.log(Level.FINE, e.toString(), e);
     }

     out.writeDouble(value);
   }
 
Example 11
/**
 * Output a field containing a primitive.
 * @param obj object containing the field.
 * @param sb where the output is to be placed.
 * @param field to be output.
 * @param fieldClass class of the field.
 */
private static void primitiveToString(final Object obj, final StringBuilder sb, final Field field, final Class<?> fieldClass) {
  try {
    if (fieldClass == Double.TYPE || fieldClass == Float.TYPE) {

      final double dbl = field.getDouble(obj);
      sb.append(Utils.realFormat(dbl, 4));

    } else if (fieldClass == Long.TYPE || fieldClass == Integer.TYPE || fieldClass == Short.TYPE || fieldClass == Byte.TYPE) {

      final long fInt = field.getLong(obj);
      sb.append(fInt);

    } else if (fieldClass == Boolean.TYPE) {

      final boolean bool = field.getBoolean(obj);
      sb.append(bool);

    } else if (fieldClass == Character.TYPE) {

      final char ch = field.getChar(obj);
      sb.append("'").append(ch).append("'");

    }
  } catch (final IllegalArgumentException | IllegalAccessException e) {
    // keep going
  }
}
 
Example 12
Source Project: dubbox   File: JavaSerializer.java    License: Apache License 2.0 5 votes vote down vote up
void serialize(AbstractHessianOutput out, Object obj, Field field)
     throws IOException
   {
     double value = 0;

     try {
value = field.getDouble(obj);
     } catch (IllegalAccessException e) {
log.log(Level.FINE, e.toString(), e);
     }

     out.writeDouble(value);
   }
 
Example 13
@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 14
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;
}
 
Example 15
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;
}