Skip to content

Commit

Permalink
chore: divorce UncheckedCasts API for Class
Browse files Browse the repository at this point in the history
  • Loading branch information
JarvisCraft committed Aug 18, 2021
1 parent 809c7d6 commit 4924d11
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package ru.progrm_jarvis.javacommons;

import lombok.experimental.UtilityClass;

/**
* Utilities mostly used by <b>java-commons</b> internals.
*/
@UtilityClass
public class JavaCommons {
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,13 @@ public class LegacyClassExtensions {
*
* @param type raw-typed array-class object
* @param <T> exact wanted type of array-class object
* @return the provided array-class object with its type case to the specific one
* @return the provided array-class object with its type cast to the specific one
*
* @apiNote this is effectively no-op
*/
// note: no nullability annotations are present on parameter and return type as cast of `null` is also safe
@Contract("_ -> param1")
@SuppressWarnings("unchecked")
private <T> Class<T[]> uncheckedArrayClassCast(final Class<?> type) {
public <T> Class<T[]> uncheckedArrayClassCast(final Class<?> type) {
return (Class<T[]>) type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ protected interface DelegateWrapperFactory<T> {
*
* @param type raw-typed delegate wrapper factory
* @param <T> exact wanted type of delegate wrapper factory
* @return the provided delegate wrapper factory with its type case to the specific one
* @return the provided delegate wrapper factory with its type cast to the specific one
*
* @apiNote this is effectively no-op
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public Optional<LookupFactory> getDefault() {
*
* @param type raw-typed constructor array
* @param <T> exact wanted type of constructor array
* @return the provided constructor array with its type case to the specific one
* @return the provided constructor array with its type cast to the specific one
*
* @apiNote this is effectively no-op
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package ru.progrm_jarvis.javacommons.unchecked;

import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.Contract;

/**
* Commonly used unchecked cast operations.
*
* @apiNote no nullability annotations are present on parameters and return types
* because casts of {@code null} are always safe
*/
@UtilityClass
public class UncheckedCasts {

/**
* Casts the given class object into the specific one.
*
* @param type raw-typed class object
* @param <T> exact wanted type of class object
* @return the provided class object with its type cast to the specific one
*
* @apiNote this is effectively no-op
*/
@Contract("_ -> param1")
@SuppressWarnings("unchecked")
public <T> Class<T> uncheckedClassCast(final Class<?> type) {
return (Class<T>) type;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package ru.progrm_jarvis.javacommons.util;

import lombok.experimental.UtilityClass;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.lang.annotation.*;

import static ru.progrm_jarvis.javacommons.unchecked.UncheckedCasts.uncheckedClassCast;

/**
* Utilities related to special hacks around Java's types, especially generics.
*/
Expand All @@ -26,22 +27,6 @@ public class TypeHints {
return uncheckedClassCast(typeHint.getClass().getComponentType());
}

/**
* Casts the given class object into the specific one.
*
* @param type raw-typed class object
* @param <T> exact wanted type of class object
* @return the provided class object with its type case to the specific one
*
* @apiNote this is effectively no-op
*/
// note: no nullability annotations are present on parameter and return type as cast of `null` is also safe
@Contract("_ -> param1")
@SuppressWarnings("unchecked")
private <T> Class<T> uncheckedClassCast(final Class<?> type) {
return (Class<T>) type;
}

/**
* Marks the specified method argument as a type hint.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class LegacyCollectorExtensions {
*
* @param raw raw-types list
* @param <T> exact wanted type of list elements
* @return the provided list with its type case to the specific one
* @return the provided list with its type cast to the specific one
*
* @apiNote this is effectively no-op
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ BiFunction.class, uncheckedConstructorCast(constructor)
*
* @param type raw-typed constructor
* @param <T> exact wanted type of constructor
* @return the provided constructor with its type case to the specific one
* @return the provided constructor with its type cast to the specific one
*
* @apiNote this is effectively no-op
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import ru.progrm_jarvis.javacommons.lazy.Lazy;
import ru.progrm_jarvis.javacommons.object.valuestorage.SimpleValueStorage;
import ru.progrm_jarvis.javacommons.object.valuestorage.ValueStorage;
import ru.progrm_jarvis.javacommons.unchecked.UncheckedCasts;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
Expand All @@ -32,6 +33,7 @@
import static org.objectweb.asm.Opcodes.*;
import static org.objectweb.asm.Type.*;
import static ru.progrm_jarvis.javacommons.bytecode.asm.AsmUtil.*;
import static ru.progrm_jarvis.javacommons.unchecked.UncheckedCasts.uncheckedClassCast;

/**
* Implementation of {@link TextModelFactory text model factory} which uses runtime class generation.
Expand Down Expand Up @@ -577,22 +579,6 @@ protected void endModification(final @NotNull StaticAsmNode<T> staticNode) {
}
}

/**
* Casts the given class object into the specific one.
*
* @param type raw-typed class object
* @param <T> exact wanted type of class object
* @return the provided class object with its type cast to the specific one
*
* @apiNote this is effectively no-op
*/
// note: no nullability annotations are present on parameter and return type as cast of `null` is also safe
@Contract("_ -> param1")
@SuppressWarnings("unchecked")
private static <T> Class<T> uncheckedClassCast(final Class<?> type) {
return (Class<T>) type;
}

/**
* Casts the given text model into the specific one.
*
Expand Down

0 comments on commit 4924d11

Please sign in to comment.