Browse Source

Add annotations helping with kotlin platform types

master
Paweł Płazieński 2 months ago
parent
commit
fed79beb8a
12 changed files with 67 additions and 1 deletions
  1. +9
    -0
      pom.xml
  2. +2
    -0
      src/main/java/org/perfectable/introspection/FunctionalReference.java
  3. +35
    -0
      src/main/java/org/perfectable/introspection/TypesAreNotNullableByDefault.java
  4. +3
    -0
      src/main/java/org/perfectable/introspection/bean/package-info.java
  5. +1
    -0
      src/main/java/org/perfectable/introspection/package-info.java
  6. +2
    -0
      src/main/java/org/perfectable/introspection/proxy/package-info.java
  7. +2
    -0
      src/main/java/org/perfectable/introspection/query/AbstractQuery.java
  8. +2
    -0
      src/main/java/org/perfectable/introspection/query/package-info.java
  9. +2
    -0
      src/main/java/org/perfectable/introspection/type/ClassView.java
  10. +3
    -1
      src/main/java/org/perfectable/introspection/type/ParameterizedTypeView.java
  11. +4
    -0
      src/main/java/org/perfectable/introspection/type/WildcardTypeView.java
  12. +2
    -0
      src/main/java/org/perfectable/introspection/type/package-info.java

+ 9
- 0
pom.xml View File

@@ -47,6 +47,10 @@
<groupId>com.google.errorprone</groupId>
<artifactId>error_prone_annotations</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
</dependency>

<dependency>
<groupId>org.javassist</groupId>
@@ -69,6 +73,11 @@
<artifactId>checker-qual</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-annotations-jvm</artifactId>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.assertj</groupId>


+ 2
- 0
src/main/java/org/perfectable/introspection/FunctionalReference.java View File

@@ -9,6 +9,7 @@ import java.util.List;
import java.util.Set;

import com.google.errorprone.annotations.CanIgnoreReturnValue;
import kotlin.annotations.jvm.ReadOnly;

/**
* Interface that allows passing compile-time safe information around.
@@ -218,6 +219,7 @@ public interface FunctionalReference extends Serializable {
* @param index index of parameter to extract, counting from 0
* @return annotations on parameter type
*/
@ReadOnly
Set<Annotation> parameterAnnotations(int index);

/**


+ 35
- 0
src/main/java/org/perfectable/introspection/TypesAreNotNullableByDefault.java View File

@@ -0,0 +1,35 @@
package org.perfectable.introspection;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import javax.annotation.Nonnull;
import javax.annotation.meta.TypeQualifierDefault;

/**
* Annotation understood by Kotlin compiler for replacing platform types with non-null ones.
*
* <p>This annotation is needed because there's no default or popular annotation that would apply to anything else than
* parameters. For parameters there is {@link javax.annotation.ParametersAreNonnullByDefault}, but this is not enough,
* as for example it does not mark return types.
*
* <p>WARNING: This annotation was created only for internal usage in all subpackages, and it can be replaced with
* official one at any time. You should not use this in your code.
*/
@Nonnull
@TypeQualifierDefault({
ElementType.ANNOTATION_TYPE,
ElementType.CONSTRUCTOR,
ElementType.FIELD,
ElementType.LOCAL_VARIABLE,
ElementType.METHOD,
ElementType.PACKAGE,
ElementType.PARAMETER,
ElementType.TYPE
})
@Target(ElementType.PACKAGE)
@Retention(RetentionPolicy.RUNTIME)
public @interface TypesAreNotNullableByDefault {
// nothing
}

+ 3
- 0
src/main/java/org/perfectable/introspection/bean/package-info.java View File

@@ -1,4 +1,7 @@
/**
* Elements allowing treating objects as Java Beans.
*/
@TypesAreNotNullableByDefault
package org.perfectable.introspection.bean;

import org.perfectable.introspection.TypesAreNotNullableByDefault;

+ 1
- 0
src/main/java/org/perfectable/introspection/package-info.java View File

@@ -14,6 +14,7 @@
TypeUseLocation.LOCAL_VARIABLE,
TypeUseLocation.IMPLICIT_LOWER_BOUND
})
@TypesAreNotNullableByDefault
package org.perfectable.introspection;

import org.checkerframework.checker.nullness.qual.NonNull;


+ 2
- 0
src/main/java/org/perfectable/introspection/proxy/package-info.java View File

@@ -3,5 +3,7 @@
*
* <p>Use {@link org.perfectable.introspection.proxy.ProxyBuilder} as an entry point.
*/
@TypesAreNotNullableByDefault
package org.perfectable.introspection.proxy;

import org.perfectable.introspection.TypesAreNotNullableByDefault;

+ 2
- 0
src/main/java/org/perfectable/introspection/query/AbstractQuery.java View File

@@ -8,6 +8,7 @@ import java.util.stream.Stream;

import com.google.common.collect.Iterators;
import com.google.errorprone.annotations.CompatibleWith;
import kotlin.annotations.jvm.ReadOnly;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

@@ -91,6 +92,7 @@ abstract class AbstractQuery<E extends @NonNull Object, Q extends AbstractQuery<
return Optional.of(element);
}

@ReadOnly
@Override
public Iterator<E> iterator() {
return stream().iterator();


+ 2
- 0
src/main/java/org/perfectable/introspection/query/package-info.java View File

@@ -3,5 +3,7 @@
*
* <p>Different aspects of program can be introspected by a different query.
*/
@TypesAreNotNullableByDefault
package org.perfectable.introspection.query;

import org.perfectable.introspection.TypesAreNotNullableByDefault;

+ 2
- 0
src/main/java/org/perfectable/introspection/type/ClassView.java View File

@@ -7,6 +7,7 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import kotlin.annotations.jvm.ReadOnly;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

@@ -70,6 +71,7 @@ public final class ClassView<X> extends AbstractTypeView<Class<X>> {
*
* @return variables declared by class
*/
@ReadOnly
public List<TypeVariableView<Class<X>>> parameters() {
return Stream.of(type.getTypeParameters())
.map(TypeView::of)


+ 3
- 1
src/main/java/org/perfectable/introspection/type/ParameterizedTypeView.java View File

@@ -10,6 +10,7 @@ import java.util.stream.Stream;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import kotlin.annotations.jvm.ReadOnly;
import org.checkerframework.checker.nullness.qual.EnsuresNonNull;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -61,6 +62,7 @@ public final class ParameterizedTypeView extends AbstractTypeView<ParameterizedT
*
* @return types used as arguments for this parameterized type.
*/
@ReadOnly
@EnsuresNonNull("correctedArguments")
public List<TypeView> arguments() {
if (correctedArguments == null) {
@@ -89,7 +91,7 @@ public final class ParameterizedTypeView extends AbstractTypeView<ParameterizedT
*
* @return interfaces wrapped in {@link TypeView}
*/
@SuppressWarnings("MutableMethodReturnType")
@ReadOnly
public List<ParameterizedTypeView> interfaces() {
ImmutableList.Builder<ParameterizedTypeView> builder = ImmutableList.builder();
for (Type genericInterface : erasure().getGenericInterfaces()) {


+ 4
- 0
src/main/java/org/perfectable/introspection/type/WildcardTypeView.java View File

@@ -10,6 +10,8 @@ import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import kotlin.annotations.jvm.ReadOnly;

/**
* {@link TypeView} that handles {@link WildcardType}.
*/
@@ -48,6 +50,7 @@ public final class WildcardTypeView extends AbstractTypeView<WildcardType> {
*
* @return upper bounds of this wildcard, wrapped in {@link TypeView}
*/
@ReadOnly
public Collection<TypeView> upperBounds() {
return upperBoundsStream().collect(Collectors.toList());
}
@@ -64,6 +67,7 @@ public final class WildcardTypeView extends AbstractTypeView<WildcardType> {
*
* @return lower bounds of this wildcard, wrapped in {@link TypeView}
*/
@ReadOnly
public Collection<TypeView> lowerBounds() {
return lowerBoundsStream().collect(Collectors.toList());
}


+ 2
- 0
src/main/java/org/perfectable/introspection/type/package-info.java View File

@@ -1,5 +1,7 @@
/**
* Elements that allow manipulating of {@link java.lang.reflect.Type} objects.
*/
@TypesAreNotNullableByDefault
package org.perfectable.introspection.type;

import org.perfectable.introspection.TypesAreNotNullableByDefault;

Loading…
Cancel
Save