Skip to content

Commit

Permalink
Add BuildServices interface
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher authored and Ladicek committed Aug 11, 2021
1 parent 6639f58 commit da6d28e
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public interface AnnotationBuilder {
* @return a new {@code AnnotationBuilder}, never {@code null}
*/
static AnnotationBuilder of(Class<? extends Annotation> annotationType) {
return AnnotationBuilderFactoryResolver.get().create(annotationType);
return BuildServicesResolver.get().annotationBuilderFactory().create(annotationType);
}

/**
Expand All @@ -36,7 +36,7 @@ static AnnotationBuilder of(Class<? extends Annotation> annotationType) {
* @return a new {@code AnnotationBuilder}
*/
static AnnotationBuilder of(ClassInfo annotationType) {
return AnnotationBuilderFactoryResolver.get().create(annotationType);
return BuildServicesResolver.get().annotationBuilderFactory().create(annotationType);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package jakarta.enterprise.inject.build.compatible.spi;

import jakarta.enterprise.inject.spi.Prioritized;
import jakarta.enterprise.lang.model.declarations.ClassInfo;

import java.lang.annotation.Annotation;
Expand All @@ -9,7 +8,7 @@
* Service provider interface that supports creating {@link AnnotationBuilder}.
* Should not be called directly by users; the static methods on {@link AnnotationBuilder} are preferred.
*/
public interface AnnotationBuilderFactory extends Prioritized {
public interface AnnotationBuilderFactory {
/**
* Returns a new {@link AnnotationBuilder} for given annotation type.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package jakarta.enterprise.inject.build.compatible.spi;

import jakarta.enterprise.inject.spi.Prioritized;

/**
* Service facade for various services needed by {@link BuildCompatibleExtension} implementations.
*/
public interface BuildServices extends Prioritized {
/**
* @return The {@link AnnotationBuilderFactory} instance, never {@code null}
*/
AnnotationBuilderFactory annotationBuilderFactory();

/**
*
* @return The {@link ClassInfoFactory} instance, never {@code null}
*/
ClassInfoFactory classInfoFactory();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package jakarta.enterprise.inject.build.compatible.spi;

import java.util.Collections;
import java.util.Comparator;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.TreeSet;

final class BuildServicesResolver {
private static final Object lock = new Object();
private static volatile Set<BuildServices> discoveredBuildServices;
private static volatile BuildServices configuredBuildServices;

static BuildServices get() {
if (configuredBuildServices != null) {
return configuredBuildServices;
}

if (discoveredBuildServices == null) {
synchronized (lock) {
if (discoveredBuildServices == null) {
discoverFactories();
}
}
}

configuredBuildServices = discoveredBuildServices.iterator().next();

return configuredBuildServices;
}

private static void discoverFactories() {
Set<BuildServices> factories = new TreeSet<>(
Comparator.comparingInt(BuildServices::getPriority).reversed());

ServiceLoader<BuildServices> loader = SecurityActions.loadService(
BuildServices.class, BuildServicesResolver.class.getClassLoader());

if (!loader.iterator().hasNext()) {
throw new IllegalStateException("Unable to locate AnnotationBuilderFactory implementation");
}

try {
for (BuildServices buildServicies : loader) {
factories.add(buildServicies);
}
} catch (ServiceConfigurationError e) {
throw new IllegalStateException(e);
}

BuildServicesResolver.discoveredBuildServices = Collections.unmodifiableSet(factories);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jakarta.enterprise.inject.build.compatible.spi;

import jakarta.enterprise.lang.model.declarations.ClassInfo;

/**
* A factory for resolving instances of {@link jakarta.enterprise.lang.model.declarations.ClassInfo}.
*/
public interface ClassInfoFactory {
/**
* Allows resolving instances of {@link ClassInfo} from the application class path using the {@link Class#getName() binary name} of the class.
*
* @param name The binary name of the class, must not be {@code null}
* @return A {@link ClassInfo} instance or {@code null} if it does not exist
*/
ClassInfo resolve(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,9 @@
*/
public interface Types {

/**
* Returns the type for the given canonical name as defined by {@link Class#getCanonicalName()} or {@code null}
* if the type is not present on the classpath or cannot be resolved.
*
* @param name The name of the type, never {@code null}
* @return {@link Type} object representing the type or {@code null}
*/
Type forName(String name);

/**
* Returns a type from the given class literal.
*
*
* For example:
* <ul>
* <li>{@code of(void.class)}: same as {@code ofVoid()}</li>
Expand All @@ -34,7 +25,7 @@ public interface Types {
* <li>{@code of(String[][].class)}: same as {@code ofArray(ofClass(... ClassInfo for String ...), 2)}</li>
* </ul>
*
* @param clazz class literal
* @param clazz class literal, must not be {@code null}
* @return {@link Type} object representing the given class literal
*/
Type of(Class<?> clazz);
Expand All @@ -46,21 +37,21 @@ public interface Types {

/**
* Returns a {@link PrimitiveType} for the given {@link jakarta.enterprise.lang.model.types.PrimitiveType.PrimitiveKind kind}.
* @param kind The kind, never {@code null}
* @param kind The kind, must not be {@code null}
* @return The {@link PrimitiveType}, never {@code null}
*/
PrimitiveType ofPrimitive(PrimitiveType.PrimitiveKind kind);

/**
* Returns a {@link Type} for the given {@link ClassInfo clazz}.
* @param clazz The {@link ClassInfo}, never {@code null}
* @param clazz The {@link ClassInfo}, must not be {@code null}
* @return A {@link Type}, never {@code null}
*/
Type ofClass(ClassInfo clazz);

/**
* Returns an {@link ArrayType} for the given {@link Type componentType} and dimensions
* @param componentType The component {@link Type}
* @param componentType The component {@link Type}, must not be {@code null}
* @param dimensions The dimensions
* @return The {@link ArrayType}, never {@code null}
*/
Expand All @@ -69,7 +60,7 @@ public interface Types {
/**
* Returns a parameterized {@link Type} for the given type and type arguments.
*
* @param parameterizedType The type to parametrize, never {@code null}
* @param parameterizedType The type to parametrize, must not be {@code null}
* @param typeArguments Zero or more type arguments
* @return A potentially parameterized type, never {@code null}
*/
Expand All @@ -78,7 +69,7 @@ public interface Types {
/**
* Returns a parameterized {@link Type} for the given type and type arguments.
*
* @param parameterizedType The type to parametrize, never {@code null}
* @param parameterizedType The type to parametrize, must not be {@code null}
* @param typeArguments Zero or more type arguments
* @return A potentially parameterized type, never {@code null}
*/
Expand All @@ -87,7 +78,7 @@ public interface Types {
/**
* Returns a parameterized {@link Type} for the given type and type arguments.
*
* @param parameterizedType The type to parametrize, never {@code null}
* @param parameterizedType The type to parametrize, must not be {@code null}
* @param typeArguments Zero or more type arguments
* @return A potentially parameterized type, never {@code null}
*/
Expand All @@ -96,15 +87,15 @@ public interface Types {
/**
* Equivalent of {@code ? extends upperBound}.
*
* @param upperBound upper bound type
* @param upperBound upper bound type, must not be {@code null}
* @return {@link Type} object representing a wildcard type with given upper bound
*/
Type wildcardWithUpperBound(Type upperBound);

/**
* Equivalent of {@code ? super lowerBound}.
*
* @param lowerBound lower bound type
* @param lowerBound lower bound type, must not be {@code null}
* @return {@link Type} object representing a wildcard type with given lower bound
*/
Type wildcardWithLowerBound(Type lowerBound);
Expand Down

0 comments on commit da6d28e

Please sign in to comment.