Skip to content

Commit

Permalink
refactor: Apply @Nullable annotation to spoon.support.visitor (#5497
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tenax66 authored Nov 1, 2023
1 parent fdc73eb commit 0bcaeda
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.ArrayList;
import java.util.List;

import org.jspecify.annotations.Nullable;
import spoon.reflect.declaration.CtElement;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeInformation;
Expand Down Expand Up @@ -91,5 +92,5 @@ private CtTypeReference<?> adaptTypeParameterReferenceBoundingType(CtWildcardRef
* @return {@link CtTypeReference} or {@link CtTypeParameterReference} adapted to scope of this {@link GenericTypeAdapter}
* or null if `typeParam` cannot be adapted to target `scope`
*/
protected abstract CtTypeReference<?> adaptTypeParameter(CtTypeParameter typeParam);
protected abstract @Nullable CtTypeReference<?> adaptTypeParameter(CtTypeParameter typeParam);
}
11 changes: 6 additions & 5 deletions src/main/java/spoon/support/visitor/ClassTypingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package spoon.support.visitor;

import org.jspecify.annotations.Nullable;
import spoon.SpoonException;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtElement;
Expand Down Expand Up @@ -145,7 +146,7 @@ public boolean isSubtypeOf(CtTypeReference<?> superTypeRef) {
* whose actual type argument values has to be resolved in scope of `scope` type
* @return actual type arguments of `typeRef` in scope of `scope` element or null if typeRef is not a super type of `scope`
*/
public List<CtTypeReference<?>> resolveActualTypeArgumentsOf(CtTypeReference<?> typeRef) {
public @Nullable List<CtTypeReference<?>> resolveActualTypeArgumentsOf(CtTypeReference<?> typeRef) {
final String typeQualifiedName = typeRef.getQualifiedName();
List<CtTypeReference<?>> args = typeToArguments.get(typeQualifiedName);
if (args != null) {
Expand Down Expand Up @@ -335,7 +336,7 @@ static List<CtTypeReference<?>> getTypeReferences(List<? extends CtType<?>> type
* @param type the potential inner class, whose enclosing type should be returned
* @return enclosing type of a `type` is an inner type or null if `type` is explicitly or implicitly static or top level type
*/
private CtType<?> getEnclosingType(CtType<?> type) {
private @Nullable CtType<?> getEnclosingType(CtType<?> type) {
if (type.hasModifier(ModifierKind.STATIC)) {
return null;
}
Expand All @@ -354,7 +355,7 @@ private CtType<?> getEnclosingType(CtType<?> type) {
* @param typeRef the potential inner class, whose enclosing type should be returned
* @return enclosing type of a `type` is an inner type or null if `type` is explicitly or implicitly static or top level type
*/
private CtTypeReference<?> getEnclosingType(CtTypeReference<?> typeRef) {
private @Nullable CtTypeReference<?> getEnclosingType(CtTypeReference<?> typeRef) {
CtType<?> type = typeRef.getTypeDeclaration();
if (type != null) {
if (type.hasModifier(ModifierKind.STATIC)) {
Expand Down Expand Up @@ -386,7 +387,7 @@ private CtTypeReference<?> getEnclosingType(CtTypeReference<?> typeRef) {
* or null if `typeParam` cannot be adapted to target `scope`
*/
@Override
protected CtTypeReference<?> adaptTypeParameter(CtTypeParameter typeParam) {
protected @Nullable CtTypeReference<?> adaptTypeParameter(CtTypeParameter typeParam) {
if (typeParam == null) {
throw new SpoonException("You cannot adapt a null type parameter.");
}
Expand Down Expand Up @@ -517,7 +518,7 @@ private CtTypeReference<?> resolveTypeParameter(CtFormalTypeDeclarer declarer, C
return getValue(actualTypeArguments, typeParam, declarer);
}

private List<CtTypeReference<?>> getActualTypeArguments(String qualifiedName) {
private @Nullable List<CtTypeReference<?>> getActualTypeArguments(String qualifiedName) {
List<CtTypeReference<?>> actualTypeArguments = typeToArguments.get(qualifiedName);
if (actualTypeArguments != null) {
return actualTypeArguments;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/spoon/support/visitor/MethodTypingContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.List;
import java.util.Set;

import org.jspecify.annotations.Nullable;
import spoon.SpoonException;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtInvocation;
Expand Down Expand Up @@ -170,7 +171,7 @@ public MethodTypingContext setExecutableReference(CtExecutableReference<?> execR
* or null if `typeParam` cannot be adapted to target `scope`
*/
@Override
protected CtTypeReference<?> adaptTypeParameter(CtTypeParameter typeParam) {
protected @Nullable CtTypeReference<?> adaptTypeParameter(CtTypeParameter typeParam) {
if (typeParam == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.List;

import org.jspecify.annotations.Nullable;
import spoon.SpoonException;
import spoon.reflect.path.CtRole;
import spoon.support.visitor.java.reflect.RtMethod;
Expand Down Expand Up @@ -603,7 +605,7 @@ private static boolean isTopLevelType(Class<?> clazz) {
return clazz.getEnclosingClass() == null && clazz.getPackage() != null;
}

private static Class<?> getRecordClass() {
private static @Nullable Class<?> getRecordClass() {
try {
return Class.forName("java.lang.Record");
} catch (Exception e) {
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/spoon/support/visitor/java/MethodHandleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
*/
package spoon.support.visitor.java;

import org.jspecify.annotations.Nullable;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
Expand Down Expand Up @@ -53,7 +55,7 @@ public static boolean isRecord(Class<?> clazz) {
* Returns the class object of record component from the jdk if present.
* @return the class object of record component from the jdk if present, null otherwise.
*/
private static Class<?> lookupRecordComponentClass() {
private static @Nullable Class<?> lookupRecordComponentClass() {
try {
return Class.forName("java.lang.reflect.RecordComponent");
} catch (ClassNotFoundException e) {
Expand Down Expand Up @@ -91,7 +93,7 @@ public static List<AnnotatedElement> getRecordComponents(Class<?> clazz) {
* @param component the record component to get the name from.
* @return the name of the given record component, null otherwise.
*/
public static String getRecordComponentName(AnnotatedElement component) {
public static @Nullable String getRecordComponentName(AnnotatedElement component) {
try {
return (String) lookupRecordComponentName.invoke(component);
} catch (Throwable e) {
Expand All @@ -108,15 +110,15 @@ public static String getRecordComponentName(AnnotatedElement component) {
* @param component
* @return
*/
public static Type getRecordComponentType(AnnotatedElement component) {
public static @Nullable Type getRecordComponentType(AnnotatedElement component) {
try {
return (Type) lookupRecordComponentType.invoke(component);
} catch (Throwable e) {
return null;
}
}

public static Class<?>[] getPermittedSubclasses(Class<?> clazz) {
public static @Nullable Class<?>[] getPermittedSubclasses(Class<?> clazz) {
try {
return (Class<?>[]) lookupPermittedSubclasses.invoke(clazz);
} catch (Throwable e) {
Expand All @@ -125,38 +127,38 @@ public static Class<?>[] getPermittedSubclasses(Class<?> clazz) {
}


private static MethodHandle lookupRecord() {
private static @Nullable MethodHandle lookupRecord() {
try {
return MethodHandles.lookup().findVirtual(Class.class, "isRecord", MethodType.methodType(boolean.class));
} catch (Throwable e) {
return null;
}
}
private static MethodHandle lookupRecordComponents() {
private static @Nullable MethodHandle lookupRecordComponents() {
try {
MethodType arrayOfRecordComponentType = MethodType.methodType(Array.newInstance(recordComponent, 0).getClass());
return MethodHandles.lookup().findVirtual(Class.class, "getRecordComponents", arrayOfRecordComponentType);
} catch (Throwable e) {
return null;
}
}
private static MethodHandle lookupRecordComponentType() {
private static @Nullable MethodHandle lookupRecordComponentType() {
try {
return MethodHandles.lookup().findVirtual(recordComponent, "getGenericType", MethodType.methodType(Type.class));
} catch (Throwable e) {
return null;
}
}

private static MethodHandle lookupRecordComponentName() {
private static @Nullable MethodHandle lookupRecordComponentName() {
try {
return MethodHandles.lookup().findVirtual(recordComponent, "getName", MethodType.methodType(String.class));
} catch (Throwable e) {
return null;
}
}

private static MethodHandle lookupPermittedSubclasses() {
private static @Nullable MethodHandle lookupPermittedSubclasses() {
try {
return MethodHandles.lookup().findVirtual(Class.class, "getPermittedSubclasses", MethodType.methodType(Class[].class));
} catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package spoon.support.visitor.java.internal;

import org.jspecify.annotations.Nullable;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtEnumValue;
Expand Down Expand Up @@ -87,7 +88,7 @@ public void addRecordComponent(CtRecordComponent ctRecordComponent) {
}

@Override
public CtTypeParameter getTypeParameter(GenericDeclaration genericDeclaration, String string) {
public @Nullable CtTypeParameter getTypeParameter(GenericDeclaration genericDeclaration, String string) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package spoon.support.visitor.java.internal;

import org.jspecify.annotations.Nullable;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtConstructor;
import spoon.reflect.declaration.CtExecutable;
Expand Down Expand Up @@ -77,7 +78,7 @@ public void addFormalType(CtTypeParameter parameterRef) {
}

@Override
public CtTypeParameter getTypeParameter(GenericDeclaration genericDeclaration, String string) {
public @Nullable CtTypeParameter getTypeParameter(GenericDeclaration genericDeclaration, String string) {
return executable == genericDeclaration ? this.mapTypeParameters.get(string) : null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package spoon.support.visitor.java.internal;

import org.jspecify.annotations.Nullable;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtPackage;
import spoon.reflect.declaration.CtTypeParameter;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void addFormalType(CtTypeParameter parameterRef) {
}

@Override
public CtTypeParameter getTypeParameter(GenericDeclaration genericDeclaration, String string) {
public @Nullable CtTypeParameter getTypeParameter(GenericDeclaration genericDeclaration, String string) {
return type == genericDeclaration ? this.mapTypeParameters.get(string) : null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
package spoon.support.visitor.java.internal;

import org.jspecify.annotations.Nullable;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
Expand Down Expand Up @@ -106,7 +107,7 @@ public void addTypeReference(CtRole role, CtTypeReference<?> typeReference) {
}

@Override
public CtTypeParameter getTypeParameter(GenericDeclaration genericDeclaration, String string) {
public @Nullable CtTypeParameter getTypeParameter(GenericDeclaration genericDeclaration, String string) {
return rtType == genericDeclaration ? this.mapTypeParameters.get(string) : null;
}
}

0 comments on commit 0bcaeda

Please sign in to comment.