java.lang.NoSuchFieldException.<init>()方法的使用及代码示例

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

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

NoSuchFieldException.<init>介绍

[英]Constructs a new NoSuchFieldException that includes the current stack trace.
[中]构造包含当前堆栈跟踪的新NoSuchFieldException。

代码示例

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

/** @param name the name of the field
 * @return the public field of this type or one of its super interfaces with the given name. See
 *         {@link Class#getField(String)}.
 * @throws NoSuchFieldException */
public Field getField (String name) throws NoSuchFieldException {
  for (Field f : getFields()) {
    if (f.name.equals(name)) return f;
  }
  throw new NoSuchFieldException();
}

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

/** @param name the name of the field
 * @return the declared field of this type. See {@link Class#getDeclaredField(String)}.
 * @throws NoSuchFieldException */
public Field getDeclaredField (String name) throws NoSuchFieldException {
  for (Field f : getDeclaredFields()) {
    if (f.name.equals(name)) return f;
  }
  throw new NoSuchFieldException();
}

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

/** @param name the name of the field
 * @return the public field of this type or one of its super interfaces with the given name. See
 *         {@link Class#getField(String)}.
 * @throws NoSuchFieldException */
public Field getField (String name) throws NoSuchFieldException {
  for (Field f : getFields()) {
    if (f.name.equals(name)) return f;
  }
  throw new NoSuchFieldException();
}

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

/** @param name the name of the field
 * @return the declared field of this type. See {@link Class#getDeclaredField(String)}.
 * @throws NoSuchFieldException */
public Field getDeclaredField (String name) throws NoSuchFieldException {
  for (Field f : getDeclaredFields()) {
    if (f.name.equals(name)) return f;
  }
  throw new NoSuchFieldException();
}

代码示例来源:origin: spullara/mustache.java

protected void checkField(Field member) throws NoSuchFieldException {
 if ((member.getModifiers() & Modifier.PRIVATE) == Modifier.PRIVATE) {
  throw new NoSuchFieldException("Only public, protected and package members allowed");
 }
}

代码示例来源:origin: Tencent/tinker

public static Field findField(Class<?> originClazz, String name) throws NoSuchFieldException {
  for (Class<?> clazz = originClazz; clazz != null; clazz = clazz.getSuperclass()) {
    try {
      Field field = clazz.getDeclaredField(name);
      if (!field.isAccessible()) {
        field.setAccessible(true);
      }
      return field;
    } catch (NoSuchFieldException e) {
      // ignore and search next
    }
  }
  throw new NoSuchFieldException("Field " + name + " not found in " + originClazz);
}

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

public static Field getDeclaredField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
  for (Class<?> c : getClassHierarchy(clazz)) {
    for (Field field : c.getDeclaredFields()) {
      if (field.getName().equals(fieldName)) {
        return field;
      }
    }
  }
  throw new NoSuchFieldException("No such field: " + fieldName);
}

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

private Field findField(Field[] candidates, String name) throws NoSuchFieldException {
  for (Field f : candidates) {
    if (name.equals(f.getName())) {
      return f;
    }
  }
  throw new NoSuchFieldException(name);
}

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

public static Field getDeclaredField(Class<?> clazz, String fieldName) throws NoSuchFieldException {
  for (Class<?> c : getClassHierarchy(clazz)) {
    for (Field field : c.getDeclaredFields()) {
      if (field.getName().equals(fieldName)) {
        return field;
      }
    }
  }
  throw new NoSuchFieldException("No such field: " + fieldName);
}

代码示例来源:origin: Meituan-Dianping/Robust

private static Field getReflectStaticField(String name, Class clazz) throws NoSuchFieldException {
    try {
      Field field = clazz.getDeclaredField(name);
      if (!field.isAccessible()) {
        field.setAccessible(true);
      }
      return field;
    } catch (NoSuchFieldException e) {
      // ignore and search next
      e.printStackTrace();
    }
    throw new NoSuchFieldException("Field " + name + " not found in " + clazz);
  }
}

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

private static Field findField(Object instance, String name) throws NoSuchFieldException {
  for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
    try {
      Field field = clazz.getDeclaredField(name);
      if (!field.isAccessible()) {
        field.setAccessible(true);
      }
      return field;
    } catch (NoSuchFieldException e) {
      // ignore and search next
    }
  }
  throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
}

代码示例来源:origin: Tencent/tinker

/**
 * Locates a given field anywhere in the class inheritance hierarchy.
 *
 * @param instance an object to search the field into.
 * @param name     field name
 * @return a field object
 * @throws NoSuchFieldException if the field cannot be located
 */
public static Field findField(Object instance, String name) throws NoSuchFieldException {
  for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
    try {
      Field field = clazz.getDeclaredField(name);
      if (!field.isAccessible()) {
        field.setAccessible(true);
      }
      return field;
    } catch (NoSuchFieldException e) {
      // ignore and search next
    }
  }
  throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
}

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

public static Field getField(final Class clazz, String field_name, boolean throw_exception) throws NoSuchFieldException {
  if(clazz == null || field_name == null)
    return null;
  Field field=null;
  for(Class curr=clazz; curr != null; curr=curr.getSuperclass()) {
    try {
      return curr.getDeclaredField(field_name);
    }
    catch(NoSuchFieldException ignored) {
    }
  }
  if(field == null && throw_exception)
    throw new NoSuchFieldException(String.format("%s not found in %s or superclasses", field_name, clazz.getName()));
  return field;
}

代码示例来源:origin: Meituan-Dianping/Robust

public static Field getReflectField(String name, Object instance) throws NoSuchFieldException {
  for (Class<?> clazz = instance.getClass(); clazz != null; clazz = clazz.getSuperclass()) {
    try {
      Field field = clazz.getDeclaredField(name);
      if (!field.isAccessible()) {
        field.setAccessible(true);
      }
      return field;
    } catch (NoSuchFieldException e) {
      // ignore and search next
    }
  }
  throw new NoSuchFieldException("Field " + name + " not found in " + instance.getClass());
}

代码示例来源:origin: nutzam/nutz

/**
 * 获取一个字段。这个字段必须声明特殊的注解,第一遇到的对象会被返回
 * 
 * @param ann
 *            注解
 * @return 字段
 * @throws NoSuchFieldException
 */
public <AT extends Annotation> Field getField(Class<AT> ann) throws NoSuchFieldException {
  for (Field field : this.getFields()) {
    if (field.isAnnotationPresent(ann))
      return field;
  }
  throw new NoSuchFieldException(String.format("Can NOT find field [@%s] in class [%s] and it's parents classes",
                         ann.getName(),
                         klass.getName()));
}

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

@RuntimeType
  public static Object intercept(
      @Origin Method method,
      @AllArguments Object[] args,
      @This Object me,
      @FieldValue("liveObjectLiveMap") RMap<?, ?> map
  ) throws Exception {
    if (args.length >= 1 && String.class.isAssignableFrom(args[0].getClass())) {
      String name = ((String) args[0]).substring(0, 1).toUpperCase() + ((String) args[0]).substring(1);
      if ("get".equals(method.getName()) && args.length == 1) {
        try {
          return me.getClass().getMethod("get" + name).invoke(me);
        } catch (NoSuchMethodException noSuchMethodException) {
          throw new NoSuchFieldException((String) args[0]);
        }
      } else if ("set".equals(method.getName()) && args.length == 2) {
        Method m = ClassUtils.searchForMethod(me.getClass(), "set" + name, new Class[]{args[1].getClass()});
        if (m != null) {
          return m.invoke(me, args[1]);
        } else {
          throw new NoSuchFieldException((String) args[0]);
        }
      }
    }
    throw new NoSuchMethodException(method.getName() + " has wrong signature");

  }
}

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

@RuntimeType
  public static Object intercept(
      @Origin Method method,
      @AllArguments Object[] args,
      @This Object me,
      @FieldValue("liveObjectLiveMap") RMap<?, ?> map
  ) throws Exception {
    if (args.length >= 1 && String.class.isAssignableFrom(args[0].getClass())) {
      String name = ((String) args[0]).substring(0, 1).toUpperCase() + ((String) args[0]).substring(1);
      if ("get".equals(method.getName()) && args.length == 1) {
        try {
          return me.getClass().getMethod("get" + name).invoke(me);
        } catch (NoSuchMethodException noSuchMethodException) {
          throw new NoSuchFieldException((String) args[0]);
        }
      } else if ("set".equals(method.getName()) && args.length == 2) {
        Method m = ClassUtils.searchForMethod(me.getClass(), "set" + name, new Class[]{args[1].getClass()});
        if (m != null) {
          return m.invoke(me, args[1]);
        } else {
          throw new NoSuchFieldException((String) args[0]);
        }
      }
    }
    throw new NoSuchMethodException(method.getName() + " has wrong signature");

  }
}

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

public static Field[] helper2(Class<?> clazz, String s) throws NoSuchFieldException {
  callcount++;
  events.add("helper2(" + clazz.getName() + "," + s + ")");
  if (s.equals("foo")) {
    throw new NoSuchFieldException(s);
  }
  return null;
}

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

} while (clazz != null);
  throw new NoSuchFieldException();
} catch (NoSuchFieldException e) {
  return e;

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

protected void cleanupFailedPersistenceAttempt(Serializable instance) throws IllegalAccessException {
  //Remove the entity from ORM management - no further attempts to persist
  if (getPersistenceManager().getDynamicEntityDao().getStandardEntityManager().contains(instance)) {
    getPersistenceManager().getDynamicEntityDao().getStandardEntityManager().detach(instance);
  }
  //Remove the id field value, if it's set
  String idFieldName = (String) getPersistenceManager().getDynamicEntityDao().getIdMetadata(instance.getClass()).get("name");
  Field idField = FieldUtils.getField(instance.getClass(), idFieldName, true);
  if (idField == null) {
    throw ExceptionHelper.refineException(new NoSuchFieldException("Entity " + instance.getClass().getName() + " does not contain id field " + idFieldName));
  }
  idField.setAccessible(true);
  if (idField.get(instance) != null) {
    idField.set(instance, null);
  }
}

相关文章