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

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

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

Class.getEnclosingClass介绍

[英]Returns the enclosing Class of this Class. If there is no enclosing class the method returns null.
[中]返回该类的封闭类。如果没有封闭类,则该方法返回null。

代码示例

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

@Override
public boolean hasEnclosingClass() {
  return (this.introspectedClass.getEnclosingClass() != null);
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * <p>Is the specified class an inner class or static nested class.</p>
 *
 * @param cls  the class to check, may be null
 * @return {@code true} if the class is an inner or static nested class,
 *  false if not or {@code null}
 */
public static boolean isInnerClass(final Class<?> cls) {
  return cls != null && cls.getEnclosingClass() != null;
}

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

@Override
@Nullable
public String getEnclosingClassName() {
  Class<?> enclosingClass = this.introspectedClass.getEnclosingClass();
  return (enclosingClass != null ? enclosingClass.getName() : null);
}

代码示例来源:origin: org.mockito/mockito-core

private static boolean typeIsNonStaticInnerClass(Class<?> type, int modifiers) {
  return !Modifier.isStatic(modifiers) && type.getEnclosingClass() != null;
}

代码示例来源:origin: org.mockito/mockito-core

private static boolean typeIsPrivateAbstractInnerClass(Class<?> type, int modifiers) {
  return Modifier.isPrivate(modifiers) && Modifier.isAbstract(modifiers) && type.getEnclosingClass() != null;
}

代码示例来源:origin: google/guava

@Override
 @Nullable
 Class<?> getOwnerType(Class<?> rawType) {
  return rawType.getEnclosingClass();
 }
},

代码示例来源:origin: google/guava

@Override
 @Nullable
 Class<?> getOwnerType(Class<?> rawType) {
  if (rawType.isLocalClass()) {
   return null;
  } else {
   return rawType.getEnclosingClass();
  }
 }
};

代码示例来源:origin: google/guava

/**
 * Returns the owner type of a {@link ParameterizedType} or enclosing class of a {@link Class}, or
 * null otherwise.
 */
private @Nullable Type getOwnerTypeIfPresent() {
 if (runtimeType instanceof ParameterizedType) {
  return ((ParameterizedType) runtimeType).getOwnerType();
 } else if (runtimeType instanceof Class<?>) {
  return ((Class<?>) runtimeType).getEnclosingClass();
 } else {
  return null;
 }
}

代码示例来源:origin: junit-team/junit4

private Class<?> getEnclosingClassForNonStaticMemberClass(Class<?> currentTestClass) {
  if (currentTestClass.isMemberClass() && !Modifier.isStatic(currentTestClass.getModifiers())) {
    return currentTestClass.getEnclosingClass();
  } else {
    return null;
  }
}

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

Class<?> enclosingClass = getClass().getEnclosingClass();
if (enclosingClass != null) {
 System.out.println(enclosingClass.getName());
} else {
 System.out.println(getClass().getName());
}

代码示例来源:origin: google/guava

/**
 * Verifies that {@code ctor} produces a {@link NullPointerException} or {@link
 * UnsupportedOperationException} whenever <i>any</i> of its non-nullable parameters are null.
 */
public void testConstructor(Constructor<?> ctor) {
 Class<?> declaringClass = ctor.getDeclaringClass();
 checkArgument(
   Modifier.isStatic(declaringClass.getModifiers())
     || declaringClass.getEnclosingClass() == null,
   "Cannot test constructor of non-static inner class: %s",
   declaringClass.getName());
 Class<?>[] types = ctor.getParameterTypes();
 for (int nullIndex = 0; nullIndex < types.length; nullIndex++) {
  testConstructorParameter(ctor, nullIndex);
 }
}

代码示例来源:origin: google/guava

@Override
Type[] getGenericParameterTypes() {
 Type[] types = constructor.getGenericParameterTypes();
 if (types.length > 0 && mayNeedHiddenThis()) {
  Class<?>[] rawParamTypes = constructor.getParameterTypes();
  if (types.length == rawParamTypes.length
    && rawParamTypes[0] == getDeclaringClass().getEnclosingClass()) {
   // first parameter is the hidden 'this'
   return Arrays.copyOfRange(types, 1, types.length);
  }
 }
 return types;
}

代码示例来源:origin: eclipse-vertx/vert.x

/**
 * @deprecated see https://github.com/eclipse-vertx/vert.x/issues/2774
 */
@Deprecated
public static Logger getLogger(final Class<?> clazz) {
 String name = clazz.isAnonymousClass() ?
  clazz.getEnclosingClass().getCanonicalName() :
  clazz.getCanonicalName();
 return getLogger(name);
}

代码示例来源:origin: prestodb/presto

@NullableDecl
 @Override
 Class<?> getOwnerType(Class<?> rawType) {
  return rawType.getEnclosingClass();
 }
},

代码示例来源:origin: prestodb/presto

@NullableDecl
 @Override
 Class<?> getOwnerType(Class<?> rawType) {
  if (rawType.isLocalClass()) {
   return null;
  } else {
   return rawType.getEnclosingClass();
  }
 }
};

代码示例来源:origin: prestodb/presto

/**
 * @since 2.7
 */
public static Class<?> getEnclosingClass(Class<?> cls) {
  // Caching does not seem worthwhile, as per profiling
  return isObjectOrPrimitive(cls) ? null : cls.getEnclosingClass();
}

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

/**
 * {@inheritDoc}
 */
public TypeDescription getEnclosingType() {
  Class<?> enclosingType = type.getEnclosingClass();
  return enclosingType == null
      ? TypeDescription.UNDEFINED
      : ForLoadedType.of(enclosingType);
}

代码示例来源:origin: google/guava

private boolean mayNeedHiddenThis() {
  Class<?> declaringClass = constructor.getDeclaringClass();
  if (declaringClass.getEnclosingConstructor() != null) {
   // Enclosed in a constructor, needs hidden this
   return true;
  }
  Method enclosingMethod = declaringClass.getEnclosingMethod();
  if (enclosingMethod != null) {
   // Enclosed in a method, if it's not static, must need hidden this.
   return !Modifier.isStatic(enclosingMethod.getModifiers());
  } else {
   // Strictly, this doesn't necessarily indicate a hidden 'this' in the case of
   // static initializer. But there seems no way to tell in that case. :(
   // This may cause issues when an anonymous class is created inside a static initializer,
   // and the class's constructor's first parameter happens to be the enclosing class.
   // In such case, we may mistakenly think that the class is within a non-static context
   // and the first parameter is the hidden 'this'.
   return declaringClass.getEnclosingClass() != null
     && !Modifier.isStatic(declaringClass.getModifiers());
  }
 }
}

代码示例来源:origin: square/retrofit

ParameterizedTypeImpl(@Nullable Type ownerType, Type rawType, Type... typeArguments) {
 // Require an owner type if the raw type needs it.
 if (rawType instanceof Class<?>
   && (ownerType == null) != (((Class<?>) rawType).getEnclosingClass() == null)) {
  throw new IllegalArgumentException();
 }
 for (Type typeArgument : typeArguments) {
  checkNotNull(typeArgument, "typeArgument == null");
  checkNotPrimitive(typeArgument);
 }
 this.ownerType = ownerType;
 this.rawType = rawType;
 this.typeArguments = typeArguments.clone();
}

代码示例来源:origin: google/guava

/**
 * Returns a type where {@code rawType} is parameterized by {@code arguments} and is owned by
 * {@code ownerType}.
 */
static ParameterizedType newParameterizedTypeWithOwner(
  @Nullable Type ownerType, Class<?> rawType, Type... arguments) {
 if (ownerType == null) {
  return newParameterizedType(rawType, arguments);
 }
 // ParameterizedTypeImpl constructor already checks, but we want to throw NPE before IAE
 checkNotNull(arguments);
 checkArgument(rawType.getEnclosingClass() != null, "Owner type for unenclosed %s", rawType);
 return new ParameterizedTypeImpl(ownerType, rawType, arguments);
}

相关文章

Class类方法