@Override public boolean hasEnclosingClass() { return (this.introspectedClass.getEnclosingClass() != null); }
/** * <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; }
@Override @Nullable public String getEnclosingClassName() { Class<?> enclosingClass = this.introspectedClass.getEnclosingClass(); return (enclosingClass != null ? enclosingClass.getName() : null); }
private static boolean typeIsNonStaticInnerClass(Class<?> type, int modifiers) { return !Modifier.isStatic(modifiers) && type.getEnclosingClass() != null; }
private static boolean typeIsPrivateAbstractInnerClass(Class<?> type, int modifiers) { return Modifier.isPrivate(modifiers) && Modifier.isAbstract(modifiers) && type.getEnclosingClass() != null; }
@Override @Nullable Class<?> getOwnerType(Class<?> rawType) { return rawType.getEnclosingClass(); } },
@Override @Nullable Class<?> getOwnerType(Class<?> rawType) { if (rawType.isLocalClass()) { return null; } else { return rawType.getEnclosingClass(); } } };
/** * 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; } }
private Class<?> getEnclosingClassForNonStaticMemberClass(Class<?> currentTestClass) { if (currentTestClass.isMemberClass() && !Modifier.isStatic(currentTestClass.getModifiers())) { return currentTestClass.getEnclosingClass(); } else { return null; } }
Class<?> enclosingClass = getClass().getEnclosingClass(); if (enclosingClass != null) { System.out.println(enclosingClass.getName()); } else { System.out.println(getClass().getName()); }
/** * 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); } }
@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; }
/** * @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); }
@NullableDecl @Override Class<?> getOwnerType(Class<?> rawType) { return rawType.getEnclosingClass(); } },
@NullableDecl @Override Class<?> getOwnerType(Class<?> rawType) { if (rawType.isLocalClass()) { return null; } else { return rawType.getEnclosingClass(); } } };
/** * @since 2.7 */ public static Class<?> getEnclosingClass(Class<?> cls) { // Caching does not seem worthwhile, as per profiling return isObjectOrPrimitive(cls) ? null : cls.getEnclosingClass(); }
/** * {@inheritDoc} */ public TypeDescription getEnclosingType() { Class<?> enclosingType = type.getEnclosingClass(); return enclosingType == null ? TypeDescription.UNDEFINED : ForLoadedType.of(enclosingType); }
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()); } } }
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(); }
/** * 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); }