java.lang.reflect.Field.getBoolean()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(120)

本文整理了Java中java.lang.reflect.Field.getBoolean()方法的一些代码示例,展示了Field.getBoolean()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Field.getBoolean()方法的具体详情如下:
包路径:java.lang.reflect.Field
类名称:Field
方法名:getBoolean

Field.getBoolean介绍

[英]Returns the value of the field in the specified object as a boolean. This reproduces the effect of object.fieldName

If this field is static, the object argument is ignored. Otherwise, if the object is null, a NullPointerException is thrown. If the object is not an instance of the declaring class of the method, an IllegalArgumentException is thrown.

If this Field object is enforcing access control (see AccessibleObject) and this field is not accessible from the current context, an IllegalAccessException is thrown.
[中]以布尔值形式返回指定对象中字段的值。这再现了对象的效果。字段名
如果此字段是静态的,则忽略对象参数。否则,如果对象为null,则抛出NullPointerException。如果该对象不是该方法声明类的实例,则会引发IllegalArgumentException。
如果此字段对象正在强制访问控制(请参见AccessibleObject),并且无法从当前上下文访问此字段,则会引发IllegaAccessException。

代码示例

代码示例来源:origin: android-hacker/VirtualXposed

public boolean get(Object object) {
  try {
    return this.field.getBoolean(object);
  } catch (Exception e) {
    return false;
  }
}

代码示例来源:origin: btraceio/btrace

/**
 * Gets the value of a static <code>boolean</code> field.
 *
 * @param field Field object whose value is returned.
 * @return the value of the <code>boolean</code> field
 */
public static boolean getBoolean(Field field) {
  checkStatic(field);
  try {
    return field.getBoolean(null);
  } catch (Exception exp) {
    throw translate(exp);
  }
}

代码示例来源:origin: spring-projects/spring-loaded

public boolean getReflectZ() throws Exception {
  return f_z.getBoolean(t);
}

代码示例来源:origin: btraceio/btrace

/**
 * Gets the value of an instance <code>boolean</code> field.
 *
 * @param field Field object whose value is returned.
 * @param obj the object to extract the <code>boolean</code> value
 * from
 * @return the value of the <code>boolean</code> field
 */
public static boolean getBoolean(Field field, Object obj) {
  try {
    return field.getBoolean(obj);
  } catch (Exception exp) {
    throw translate(exp);
  }
}

代码示例来源:origin: apache/geode

public boolean getBoolean(Object o) throws IllegalArgumentException, IllegalAccessException {
 return this.field.getBoolean(o);
}

代码示例来源:origin: apache/incubator-dubbo

@Override
  void serialize(AbstractHessianOutput out, Object obj, Field field)
      throws IOException {
    boolean value = false;
    try {
      value = field.getBoolean(obj);
    } catch (IllegalAccessException e) {
      log.log(Level.FINE, e.toString(), e);
    }
    out.writeBoolean(value);
  }
}

代码示例来源:origin: spring-projects/spring-loaded

public static boolean callSetAndGetBoolean(Field thiz, Object obj) throws IllegalArgumentException, IllegalAccessException {
  thiz.setBoolean(obj, !thiz.getBoolean(obj));
  return thiz.getBoolean(obj);
}

代码示例来源:origin: spring-projects/spring-loaded

public static boolean callSetBoolean(Field thiz, Object obj) throws IllegalArgumentException, IllegalAccessException {
  thiz.setBoolean(obj, true);
  return thiz.getBoolean(obj);
}

代码示例来源:origin: redisson/redisson

public final boolean getBooleanValue(Object obj) throws IllegalAccessException {
  if (!isAndroid && memOffset >= 0) {
    return FSTUtil.unFlaggedUnsafe.getBoolean(obj, memOffset);
  }
  return field.getBoolean(obj);
}

代码示例来源:origin: apache/drill

private static boolean getStaticBooleanField(Class cls, String name, boolean def) {
 try {
  Field field = cls.getDeclaredField(name);
  field.setAccessible(true);
  return field.getBoolean(null);
 } catch (ReflectiveOperationException e) {
  return def;
 }
}

代码示例来源:origin: RuedigerMoeller/fast-serialization

public final boolean getBooleanValue(Object obj) throws IllegalAccessException {
  if (!isAndroid && memOffset >= 0) {
    return FSTUtil.unFlaggedUnsafe.getBoolean(obj, memOffset);
  }
  return field.getBoolean(obj);
}

代码示例来源:origin: stackoverflow.com

public static Activity getActivity() {
  Class activityThreadClass = Class.forName("android.app.ActivityThread");
  Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
  Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
  activitiesField.setAccessible(true);

  Map<Object, Object> activities = (Map<Object, Object>) activitiesField.get(activityThread);
  if(activities == null)
      return null;

  for (Object activityRecord : activities.values()) {
    Class activityRecordClass = activityRecord.getClass();
    Field pausedField = activityRecordClass.getDeclaredField("paused");
    pausedField.setAccessible(true);
    if (!pausedField.getBoolean(activityRecord)) {
      Field activityField = activityRecordClass.getDeclaredField("activity");
      activityField.setAccessible(true);
      Activity activity = (Activity) activityField.get(activityRecord);
      return activity;
    }
  }
}

代码示例来源:origin: apache/storm

private static boolean getBoolField(HdfsSpout spout, String fieldName) throws NoSuchFieldException, IllegalAccessException {
  Field readerFld = HdfsSpout.class.getDeclaredField(fieldName);
  readerFld.setAccessible(true);
  return readerFld.getBoolean(spout);
}

代码示例来源:origin: rey5137/material

protected void positionSelectorCompat(int position, View sel) {
  final Rect selectorRect = mSelectorRect;
  selectorRect.set(sel.getLeft(), sel.getTop(), sel.getRight(), sel.getBottom());
  // Adjust for selection padding.
  selectorRect.left -= mSelectionLeftPadding;
  selectorRect.top -= mSelectionTopPadding;
  selectorRect.right += mSelectionRightPadding;
  selectorRect.bottom += mSelectionBottomPadding;
  try {
    // AbsListView.mIsChildViewEnabled controls the selector's state so we need to
    // modify it's value
    final boolean isChildViewEnabled = mIsChildViewEnabled.getBoolean(this);
    if (sel.isEnabled() != isChildViewEnabled) {
      mIsChildViewEnabled.set(this, !isChildViewEnabled);
      if (position != INVALID_POSITION) {
        refreshDrawableState();
      }
    }
  } catch (IllegalAccessException e) {
    e.printStackTrace();
  }
}
/**

代码示例来源:origin: h2oai/h2o-2

@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;
 }
}

代码示例来源:origin: apache/drill

private static boolean setBoundsChecking(boolean enabled) throws Exception
{
 Field field = BoundsChecking.class.getDeclaredField("BOUNDS_CHECKING_ENABLED");
 Field modifiersField = Field.class.getDeclaredField("modifiers");
 modifiersField.setAccessible(true);
 modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
 boolean old = field.getBoolean(null);
 field.setAccessible(true);
 field.set(null, enabled);
 return old;
}

代码示例来源:origin: robovm/robovm

/**
 * Returns the value of the field in the specified object as a {@code
 * boolean}. This reproduces the effect of {@code object.fieldName}
 * <p>
 * If this field is static, the object argument is ignored.
 * Otherwise, if the object is {@code null}, a NullPointerException is
 * thrown. If the object is not an instance of the declaring class of the
 * method, an IllegalArgumentException is thrown.
 * <p>
 * If this Field object is enforcing access control (see AccessibleObject)
 * and this field is not accessible from the current context, an
 * IllegalAccessException is thrown.
 *
 * @param object
 *            the object to access
 * @return the field value
 * @throws NullPointerException
 *             if the object is {@code null} and the field is non-static
 * @throws IllegalArgumentException
 *             if the object is not compatible with the declaring class
 * @throws IllegalAccessException
 *             if this field is not accessible
 */
public boolean getBoolean(Object object)
    throws IllegalAccessException, IllegalArgumentException {
  
  checkAccess(object, false);
  checkReceiver(object);
  return getBoolean(object, getType(), Boolean.TYPE);
}

代码示例来源:origin: apache/geode

/**
 * Deal with javax SSL properties
 */
private static void makeNullStaticField(final Class sslClass) {
 Field[] fields = sslClass.getDeclaredFields();
 for (int index = 0; index < fields.length; ++index) {
  Field field = fields[index];
  try {
   if (Modifier.isStatic(field.getModifiers())) {
    field.setAccessible(true);
    if (field.getClass().equals(boolean.class)) {
     field.setBoolean(null, false);
     assertFalse(field.getBoolean(null));
    } else if (sslClass.isInstance(field.get(null))) {
     field.set(null, null);
     assertNull(field.get(null));
    }
   }
  } catch (IllegalAccessException ex) {
   getLogWriter().warning("Exception while clearing static SSL field.", ex);
  } catch (ClassCastException ex) {
   getLogWriter().warning("Exception while clearing static SSL field.", ex);
  }
 }
}

代码示例来源:origin: neo4j/neo4j

@Override
public String toString()
{
  StringBuilder result = new StringBuilder( type.toString() );
  if ( convertToQuery )
  {
    result.append( " (as query)" );
  }
  String sep = ": ";
  for ( Field field : getClass().getDeclaredFields() )
  {
    if ( field.getType() == boolean.class )
    {
      boolean value;
      field.setAccessible( true );
      try
      {
        value = field.getBoolean( this );
      }
      catch ( IllegalAccessException e )
      {
        throw new RuntimeException( e );
      }
      result.append( sep ).append( '.' ).append( field.getName() ).append( "() == " ).append( value );
      sep = ", ";
    }
  }
  return result.toString();
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void test() throws Exception {
 StatefulSerializer<Serializable> s = new CompactJavaSerializer<>(null);
 s.init(new TransientStateRepository());
 ClassLoader loaderW = createClassNameRewritingLoader(C_W.class, B_W.class);
 ByteBuffer b = s.serialize((Serializable) loaderW.loadClass(newClassName(C_W.class)).newInstance());
 pushTccl(createClassNameRewritingLoader(C_R.class, B_R.class, A_R.class));
 try {
  Object out = s.read(b);
  Assert.assertTrue(out.getClass().getField("called").getBoolean(out));
 } finally {
  popTccl();
 }
}

相关文章

微信公众号

最新文章

更多