java.lang.Class.isSynthetic()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(11.0k)|赞(0)|评价(0)|浏览(252)

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

Class.isSynthetic介绍

[英]Tests whether this Class represents a synthetic type.
[中]测试此类是否表示合成类型。

代码示例

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

public static boolean callIsSynthetic(Class thiz)
{
  return thiz.isSynthetic();
}

代码示例来源:origin: oracle/helidon

/**
 * Returns the real class of this object, skipping proxies.
 *
 * @param object The object.
 * @return Its class.
 */
static Class<?> getRealClass(Object object) {
  Class<?> result = object.getClass();
  while (result.isSynthetic()) {
    result = result.getSuperclass();
  }
  return result;
}

代码示例来源:origin: oracle/helidon

/**
 * Returns the real class of this object, skipping proxies.
 *
 * @param object The object.
 * @return Its class.
 */
static Class<?> getRealClass(Object object) {
  Class<?> result = object.getClass();
  while (result.isSynthetic()) {
    result = result.getSuperclass();
  }
  return result;
}

代码示例来源:origin: com.thoughtworks.xstream/xstream

public static final boolean isLambdaType(final Class<?> type) {
  return type != null && type.isSynthetic() && lambdaPattern.matcher(type.getSimpleName()).matches();
}

代码示例来源:origin: org.codehaus.groovy/groovy

private static boolean classShouldBeIgnored(Class c, Collection<String> extraIgnoredPackages) {
  return ((c != null)
      && (c.isSynthetic()
        || (c.getPackage() != null
          && (IGNORED_PACKAGES.contains(c.getPackage().getName())
           || extraIgnoredPackages.contains(c.getPackage().getName())))));
}

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

private int computeRank(final T provider, final int rank) {
  if (rank > 0) {
    return rank;
  } else {
    Class<?> clazz = provider.getClass();
    // when provided instance is a proxy (from weld), we need to get the right class to check for
    // @Priority annotation - proxy doesn't propagate isAnnotationPresent to the parent class.
    while (clazz.isSynthetic()) {
      clazz = clazz.getSuperclass();
    }
    if (clazz.isAnnotationPresent(Priority.class)) {
      return clazz.getAnnotation(Priority.class).value();
    } else {
      return Priorities.USER;
    }
  }
}

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

private int computeRank(final T provider, final int rank) {
  if (rank > 0) {
    return rank;
  } else {
    Class<?> clazz = provider.getClass();
    // when provided instance is a proxy (from weld), we need to get the right class to check for
    // @Priority annotation - proxy doesn't propagate isAnnotationPresent to the parent class.
    while (clazz.isSynthetic()) {
      clazz = clazz.getSuperclass();
    }
    if (clazz.isAnnotationPresent(Priority.class)) {
      return clazz.getAnnotation(Priority.class).value();
    } else {
      return Priorities.USER;
    }
  }
}

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

private static boolean isJerseyOrDependencyType(final Class<?> clazz) {
  if (clazz.isPrimitive() || clazz.isSynthetic()) {
    return false;
  }
  final Package pkg = clazz.getPackage();
  if (pkg == null) { // Class.getPackage() could return null
    LOGGER.warning(String.format("Class %s has null package", clazz));
    return false;
  }
  final String pkgName = pkg.getName();
  return !clazz.isAnnotationPresent(JerseyVetoed.class)
      && (pkgName.contains("org.glassfish.hk2")
            || pkgName.contains("jersey.repackaged")
            || pkgName.contains("org.jvnet.hk2")
            || (pkgName.startsWith("org.glassfish.jersey")
                  && !pkgName.startsWith("org.glassfish.jersey.examples")
                  && !pkgName.startsWith("org.glassfish.jersey.tests"))
            || (pkgName.startsWith("com.sun.jersey")
                  && !pkgName.startsWith("com.sun.jersey.examples")
                  && !pkgName.startsWith("com.sun.jersey.tests")));
}

代码示例来源:origin: oldmanpushcart/greys-anatomy

/**
 * 计算ClassType
 *
 * @param targetClass 目标类
 * @return 计算出的ClassType
 */
public static int computeClassType(Class<?> targetClass) {
  int type = 0;
  if (targetClass.isAnnotation())
    type |= TYPE_ANNOTATION;
  if (targetClass.isAnonymousClass())
    type |= TYPE_ANONYMOUS;
  if (targetClass.isArray())
    type |= TYPE_ARRAY;
  if (targetClass.isEnum())
    type |= TYPE_ENUM;
  if (targetClass.isInterface())
    type |= TYPE_INTERFACE;
  if (targetClass.isLocalClass())
    type |= TYPE_LOCAL;
  if (targetClass.isMemberClass())
    type |= TYPE_MEMBER;
  if (targetClass.isPrimitive())
    type |= TYPE_PRIMITIVE;
  if (targetClass.isSynthetic())
    type |= TYPE_SYNTHETIC;
  return type;
}

代码示例来源:origin: HotswapProjects/HotswapAgent

public static Map<String, String> getNonSyntheticSignatureMap(Class<?> clazz) {
  Map<String, String> signatureMap = new HashMap<>();
  Class<?> parentClass = clazz.getSuperclass();
  while (parentClass.isSynthetic()) {
    parentClass = parentClass.getSuperclass();
  }
  addSignaturesToMap(parentClass, signatureMap);
  for (Class<?> intr : clazz.getInterfaces()) {
    addSignaturesToMap(intr, signatureMap);
  }
  return signatureMap;
}

代码示例来源:origin: looly/hutool

/**
 * 是否为标准的类<br>
 * 这个类必须:
 * 
 * <pre>
 * 1、非接口 
 * 2、非抽象类 
 * 3、非Enum枚举 
 * 4、非数组 
 * 5、非注解 
 * 6、非原始类型(int, long等)
 * </pre>
 * 
 * @param clazz 类
 * @return 是否为标准类
 */
public static boolean isNormalClass(Class<?> clazz) {
  return null != clazz //
      && false == clazz.isInterface() //
      && false == isAbstract(clazz) //
      && false == clazz.isEnum() //
      && false == clazz.isArray() //
      && false == clazz.isAnnotation() //
      && false == clazz.isSynthetic() //
      && false == clazz.isPrimitive();//
}

代码示例来源:origin: looly/hutool

/**
 * 是否为标准的类<br>
 * 这个类必须:
 * 
 * <pre>
 * 1、非接口 
 * 2、非抽象类 
 * 3、非Enum枚举 
 * 4、非数组 
 * 5、非注解 
 * 6、非原始类型(int, long等)
 * </pre>
 * 
 * @param clazz 类
 * @return 是否为标准类
 */
public static boolean isNormalClass(Class<?> clazz) {
  return null != clazz //
      && false == clazz.isInterface() //
      && false == isAbstract(clazz) //
      && false == clazz.isEnum() //
      && false == clazz.isArray() //
      && false == clazz.isAnnotation() //
      && false == clazz.isSynthetic() //
      && false == clazz.isPrimitive();//
}

代码示例来源:origin: qiujiayu/AutoLoadCache

@Override
public Object deepClone(Object obj, Type type) throws Exception {
  if (null == obj) {
    return null;
  }
  Class<?> clazz = obj.getClass();
  if (BeanUtil.isPrimitive(obj) || clazz.isEnum() || obj instanceof Class || clazz.isAnnotation() || clazz.isSynthetic()) {// 常见不会被修改的数据类型
    return obj;
  }
  if (obj instanceof Date) {
    return ((Date) obj).clone();
  } else if (obj instanceof Calendar) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(((Calendar) obj).getTime().getTime());
    return cal;
  }
  return deserialize(serialize(obj), null);
}

代码示例来源:origin: qiujiayu/AutoLoadCache

@Override
public Object deepClone(Object obj, final Type type) throws Exception {
  if (null == obj) {
    return obj;
  }
  Class<?> clazz = obj.getClass();
  if (BeanUtil.isPrimitive(obj) || clazz.isEnum() || obj instanceof Class || clazz.isAnnotation()
      || clazz.isSynthetic()) {// 常见不会被修改的数据类型
    return obj;
  }
  if (obj instanceof Date) {
    return ((Date) obj).clone();
  } else if (obj instanceof Calendar) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(((Calendar) obj).getTime().getTime());
    return cal;
  }
  return deserialize(serialize(obj), null);
}

代码示例来源:origin: baomidou/mybatis-plus

/**
 * 通过反序列化转换 lambda 表达式,该方法只能序列化 lambda 表达式,不能序列化接口实现或者正常非 lambda 写法的对象
 *
 * @param lambda lambda对象
 * @return 返回解析后的 SerializedLambda
 */
public static SerializedLambda resolve(SFunction lambda) {
  if (!lambda.getClass().isSynthetic()) {
    throw ExceptionUtils.mpe("该方法仅能传入 lambda 表达式产生的合成类");
  }
  try (ObjectInputStream objIn = new ObjectInputStream(new ByteArrayInputStream(SerializationUtils.serialize(lambda))) {
    @Override
    protected Class<?> resolveClass(ObjectStreamClass objectStreamClass) throws IOException, ClassNotFoundException {
      Class<?> clazz = super.resolveClass(objectStreamClass);
      return clazz == java.lang.invoke.SerializedLambda.class ? SerializedLambda.class : clazz;
    }
  }) {
    return (SerializedLambda) objIn.readObject();
  } catch (ClassNotFoundException | IOException e) {
    throw ExceptionUtils.mpe("This is impossible to happen", e);
  }
}

代码示例来源:origin: HotswapProjects/HotswapAgent

/**
 * Checks if the CtClass or one of its parents signature differs from the one already loaded by Java. Ignores
 * synthetic classes
 *
 * @param classBeingRedefined
 * @param cp
 * @return
 */
public static boolean isNonSyntheticPoolClassOrParentDifferent(Class<?> classBeingRedefined, ClassPool cp) {
  Class<?> clazz = classBeingRedefined.getSuperclass();
  while (clazz.isSynthetic() || clazz.getName().contains("$Enhancer")) {
    clazz = clazz.getSuperclass();
  }
  if (isPoolClassOrParentDifferent(clazz, cp))
    return true;
  Class<?>[] interfaces = classBeingRedefined.getInterfaces();
  for (Class<?> intr : interfaces) {
    if (isPoolClassOrParentDifferent(intr, cp))
      return true;
  }
  return false;
}

代码示例来源:origin: qiujiayu/AutoLoadCache

@Override
public Object deepClone(Object obj, final Type type) throws Exception {
  if (null == obj) {
    return null;
  }
  Class<?> clazz = obj.getClass();
  if (BeanUtil.isPrimitive(obj) || clazz.isEnum() || obj instanceof Class || clazz.isAnnotation()
      || clazz.isSynthetic()) {// 常见不会被修改的数据类型
    return obj;
  }
  if (obj instanceof Date) {
    return ((Date) obj).clone();
  } else if (obj instanceof Calendar) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(((Calendar) obj).getTime().getTime());
    return cal;
  }
  return deserialize(serialize(obj), null);
}

代码示例来源:origin: HotswapProjects/HotswapAgent

public boolean isReloadNeeded(Class<?> classBeingRedefined, byte[] classfileBuffer) {
  if (classBeingRedefined.isSynthetic() || isSyntheticClass(classBeingRedefined))
    return false;
  return classChangeNeedsReload(classBeingRedefined, classfileBuffer);
}

代码示例来源:origin: qiujiayu/AutoLoadCache

@Override
public Object deepClone(Object obj, final Type type) throws Exception {
  if (null == obj) {
    return null;
  }
  Class<?> clazz = obj.getClass();
  if (BeanUtil.isPrimitive(obj) || clazz.isEnum() || obj instanceof Class || clazz.isAnnotation()
      || clazz.isSynthetic()) {// 常见不会被修改的数据类型
    return obj;
  }
  if (obj instanceof Date) {
    return ((Date) obj).clone();
  } else if (obj instanceof Calendar) {
    Calendar cal = Calendar.getInstance();
    cal.setTimeInMillis(((Calendar) obj).getTime().getTime());
    return cal;
  }
  if (clazz.isArray()) {
    Object[] arr = (Object[]) obj;
    Object[] res = ((Object) clazz == (Object) Object[].class) ? (Object[]) new Object[arr.length]
        : (Object[]) Array.newInstance(clazz.getComponentType(), arr.length);
    for (int i = 0; i < arr.length; i++) {
      res[i] = deepClone(arr[i], null);
    }
    return res;
  }
  return cloner.deepClone(obj);
}

代码示例来源:origin: oldmanpushcart/greys-anatomy

private String drawClassInfo() {
  final CodeSource cs = clazz.getProtectionDomain().getCodeSource();
  final TTable tTable = new TTable(new TTable.ColumnDefine[]{
      new TTable.ColumnDefine(50, TTable.Align.RIGHT),
      new TTable.ColumnDefine(80, TTable.Align.LEFT)
  })
      .addRow("class-info", tranClassName(clazz))
      .addRow("code-source", getCodeSource(cs))
      .addRow("name", tranClassName(clazz))
      .addRow("isInterface", clazz.isInterface())
      .addRow("isAnnotation", clazz.isAnnotation())
      .addRow("isEnum", clazz.isEnum())
      .addRow("isAnonymousClass", clazz.isAnonymousClass())
      .addRow("isArray", clazz.isArray())
      .addRow("isLocalClass", clazz.isLocalClass())
      .addRow("isMemberClass", clazz.isMemberClass())
      .addRow("isPrimitive", clazz.isPrimitive())
      .addRow("isSynthetic", clazz.isSynthetic())
      .addRow("simple-name", clazz.getSimpleName())
      .addRow("modifier", tranModifier(clazz.getModifiers()))
      .addRow("annotation", drawAnnotation())
      .addRow("interfaces", drawInterface())
      .addRow("super-class", drawSuperClass())
      .addRow("class-loader", drawClassLoader());
  if (isPrintField) {
    tTable.addRow("fields", drawField());
  }
  return tTable.padding(1).rendering();
}

相关文章

Class类方法