Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main-1.x.y'
Browse files Browse the repository at this point in the history
# Conflicts:
#	api/public/src/main/kotlin/com/yandex/yatagan/internal/YataganGenerated.kt
  • Loading branch information
Jeffset committed Apr 17, 2024
2 parents b01e3b2 + 31f7fe4 commit ecada0d
Show file tree
Hide file tree
Showing 109 changed files with 577 additions and 95 deletions.
2 changes: 2 additions & 0 deletions api/public/api/public.api
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ public final class com/yandex/yatagan/Optional {
public static final fun of (Ljava/lang/Object;)Lcom/yandex/yatagan/Optional;
public static final fun ofNullable (Ljava/lang/Object;)Lcom/yandex/yatagan/Optional;
public final fun orElse (Ljava/lang/Object;)Ljava/lang/Object;
public final fun orElse (Ljavax/inject/Provider;)Ljava/lang/Object;
public final synthetic fun orElse (Lkotlin/jvm/functions/Function0;)Ljava/lang/Object;
public final fun orNull ()Ljava/lang/Object;
}

Expand Down
20 changes: 20 additions & 0 deletions api/public/src/main/kotlin/com/yandex/yatagan/Optional.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.yandex.yatagan

import javax.inject.Provider
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

Expand Down Expand Up @@ -101,6 +102,14 @@ public class Optional<out T : Any> private constructor(
@JvmName("map")
public fun <U : Any> mapJava(mapper: Function<T, U?>): Optional<U> = map(mapper::apply)

/**
* @see [orElse]
*/
@JvmName("orElse")
public fun orElseJava(alternative: Provider<@UnsafeVariance T>): T {
return value ?: alternative.get()
}

// endregion Java API

// region Kotlin API
Expand Down Expand Up @@ -140,6 +149,17 @@ public class Optional<out T : Any> private constructor(
return if (value != null) ofNullable(mapper(value)) else empty()
}

/**
* Stored value access with alternative.
*
* @return stored value if present. Otherwise, returns [alternative].
*/
@JvmSynthetic
public inline fun orElse(alternative: () -> @UnsafeVariance T): T {
contract { callsInPlace(alternative, InvocationKind.AT_MOST_ONCE) }
return value ?: alternative()
}

// endregion Kotlin API

public companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.yandex.yatagan.internal

/**
* Marks top-level classes (currently top-level component implementations) in generated code.
* May reliably be used in tooling to determine whether the class was generated by Yatagan.
*/
@MustBeDocumented
@Retention(AnnotationRetention.BINARY)
@Target(AnnotationTarget.CLASS)
public annotation class YataganGenerated
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal class ComponentGenerator @Inject constructor(
val enableProvisionNullChecks: Boolean,
val enableThreadChecks: Boolean,
val sortMethodsForTesting: Boolean,
val generatedAnnotationClassName: ClassName?,
)

init {
Expand All @@ -69,12 +70,19 @@ internal class ComponentGenerator @Inject constructor(
fun generate(): TypeSpec = buildClass(generatedClassName) {
val componentInterface = graph.model.typeName()

annotation<SuppressWarnings> { stringValues("unchecked", "rawtypes", "NullableProblems") }
modifiers(FINAL)
if (!graph.isRoot) {
modifiers(/*package-private*/ STATIC)
} else {
modifiers(PUBLIC)
annotation<SuppressWarnings> { stringValues("unchecked", "rawtypes", "NullableProblems") }
annotation(Names.YataganGenerated)
options.generatedAnnotationClassName?.let {
annotation(it) {
// We don't use the actual processor name here, as we want the code to be the same across backends.
stringValue(value = "com.yandex.yatagan.codegen.impl.ComponentGenerator")
}
}
}
implements(componentInterface)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package com.yandex.yatagan.codegen.impl

import com.squareup.javapoet.ClassName
import com.squareup.javapoet.JavaFile
import com.yandex.yatagan.Yatagan
import com.yandex.yatagan.core.graph.BindingGraph
import com.yandex.yatagan.lang.langFactory

class ComponentGeneratorFacade(
graph: BindingGraph,
Expand All @@ -27,13 +29,15 @@ class ComponentGeneratorFacade(
enableProvisionNullChecks: Boolean,
sortMethodsForTesting: Boolean,
) {
private val langFactory = graph.model.type.ext.langFactory
private val component = Yatagan.builder(GeneratorComponent.Factory::class.java).create(
graph = graph,
options = ComponentGenerator.Options(
maxSlotsPerSwitch = maxSlotsPerSwitch,
enableProvisionNullChecks = enableProvisionNullChecks,
enableThreadChecks = enableThreadChecks,
sortMethodsForTesting = sortMethodsForTesting,
generatedAnnotationClassName = generatedAnnotationClassName(),
),
)

Expand All @@ -48,4 +52,12 @@ class ComponentGeneratorFacade(
.build()
.writeTo(out)
}

private fun generatedAnnotationClassName(): ClassName? {
return Names.GeneratedJava8.takeIf { it.exists() } ?: Names.GeneratedJava9Plus.takeIf { it.exists() }
}

private fun ClassName.exists(): Boolean {
return langFactory.getTypeDeclaration(packageName(), simpleName()) != null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ internal object Names {
val AutoBuilder: ClassName = ClassName.get("com.yandex.yatagan", "AutoBuilder")
val ThreadAssertions: ClassName = ClassName.get("com.yandex.yatagan.internal", "ThreadAssertions")
val Checks: ClassName = ClassName.get("com.yandex.yatagan.internal", "Checks")
val YataganGenerated = ClassName.get("com.yandex.yatagan.internal", "YataganGenerated")

val AssertionError: ClassName = ClassName.get(java.lang.AssertionError::class.java)
val Class: ClassName = ClassName.get(java.lang.Class::class.java)
Expand All @@ -33,4 +34,7 @@ internal object Names {
val ArrayList: ClassName = ClassName.get(java.util.ArrayList::class.java)
val HashMap: ClassName = ClassName.get(java.util.HashMap::class.java)
val HashSet: ClassName = ClassName.get(java.util.HashSet::class.java)

val GeneratedJava8: ClassName = ClassName.get("javax.annotation", "Generated")
val GeneratedJava9Plus: ClassName = ClassName.get("javax.annotation.processing", "Generated")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

package com.yandex.yatagan.codegen.poetry

import com.squareup.javapoet.ClassName
import javax.lang.model.element.AnnotationMirror
import kotlin.reflect.KClass

interface AnnotatibleBuilder {
fun annotation(mirror: AnnotationMirror)

fun annotation(clazz: KClass<out Annotation>, block: AnnotationSpecBuilder.() -> Unit)

fun annotation(className: ClassName, block: AnnotationSpecBuilder.() -> Unit)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ import com.squareup.javapoet.ClassName
import kotlin.reflect.KClass

@JavaPoetry
class AnnotationSpecBuilder(
clazz: KClass<out Annotation>
) {
class AnnotationSpecBuilder private constructor(
@PublishedApi
internal val impl: AnnotationSpec.Builder = AnnotationSpec.builder(clazz.java)
internal val impl: AnnotationSpec.Builder,
) {
constructor(clazz: KClass<out Annotation>) : this(AnnotationSpec.builder(clazz.java))
constructor(className: ClassName) : this(AnnotationSpec.builder(className))

inline fun <reified E : Enum<E>> enumValue(value: E, name: String = "value") {
impl.addMember(name, "\$T.\$N", E::class.java, value.name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ abstract class MethodSpecBuilder : CodeBuilder(), AnnotatibleBuilder {
impl.addAnnotation(AnnotationSpecBuilder(clazz).apply(block).impl.build())
}

override fun annotation(className: ClassName, block: AnnotationSpecBuilder.() -> Unit) {
impl.addAnnotation(AnnotationSpecBuilder(className).apply(block).impl.build())
}

fun returnType(type: TypeName) {
impl.returns(type)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.yandex.yatagan.codegen.poetry

import com.squareup.javapoet.AnnotationSpec
import com.squareup.javapoet.ClassName
import com.squareup.javapoet.ParameterSpec
import com.squareup.javapoet.TypeName
import javax.lang.model.element.AnnotationMirror
Expand All @@ -43,4 +44,11 @@ class ParameterSpecBuilder constructor(type: TypeName, name: String) : Annotatib
) {
impl.addAnnotation(AnnotationSpecBuilder(clazz).apply(block).impl.build())
}

override fun annotation(
className: ClassName,
block: AnnotationSpecBuilder.() -> Unit,
) {
impl.addAnnotation(AnnotationSpecBuilder(className).apply(block).impl.build())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ abstract class TypeSpecBuilder : AnnotatibleBuilder {
impl.addAnnotation(AnnotationSpecBuilder(clazz).apply(block).impl.build())
}

override fun annotation(
className: ClassName,
block: AnnotationSpecBuilder.() -> Unit,
) {
impl.addAnnotation(AnnotationSpecBuilder(className).apply(block).impl.build())
}

fun annotation(name: ClassName) {
impl.addAnnotation(name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ package test;
import com.yandex.yatagan.AutoBuilder;
import com.yandex.yatagan.Optional;
import com.yandex.yatagan.internal.Checks;
import com.yandex.yatagan.internal.YataganGenerated;
import java.lang.Class;
import java.lang.Override;
import java.lang.SuppressWarnings;
import java.util.Collections;
import javax.annotation.processing.Generated;

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@YataganGenerated
@Generated("com.yandex.yatagan.codegen.impl.ComponentGenerator")
public final class Yatagan$TestComponent implements TestComponent {
final boolean mMyFeatureSEnabled = MyFeature.sEnabled;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ package test;

import com.yandex.yatagan.AutoBuilder;
import com.yandex.yatagan.internal.Checks;
import com.yandex.yatagan.internal.YataganGenerated;
import java.lang.Class;
import java.lang.Override;
import java.lang.SuppressWarnings;
import java.util.Collections;
import javax.annotation.processing.Generated;

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@YataganGenerated
@Generated("com.yandex.yatagan.codegen.impl.ComponentGenerator")
public final class Yatagan$RootComponent2 implements RootComponent2 {
private Yatagan$RootComponent2() {
}
Expand All @@ -23,7 +27,6 @@ public final class Yatagan$RootComponent2 implements RootComponent2 {
return new AutoBuilderImpl();
}

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
static final class SubComponent1Impl implements SubComponent1 {
final MyDependencies mMyDependencies;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ package test;

import com.yandex.yatagan.AutoBuilder;
import com.yandex.yatagan.internal.Checks;
import com.yandex.yatagan.internal.YataganGenerated;
import java.lang.Class;
import java.lang.Override;
import java.lang.SuppressWarnings;
import java.util.Collections;
import javax.annotation.processing.Generated;

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@YataganGenerated
@Generated("com.yandex.yatagan.codegen.impl.ComponentGenerator")
public final class Yatagan$AnotherRootComponent implements AnotherRootComponent {
private Yatagan$AnotherRootComponent() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
Name: test/Yatagan$MyApplicationComponent.java
package test;

import com.yandex.yatagan.internal.YataganGenerated;
import java.lang.Override;
import java.lang.SuppressWarnings;
import javax.annotation.processing.Generated;

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@YataganGenerated
@Generated("com.yandex.yatagan.codegen.impl.ComponentGenerator")
public final class Yatagan$MyApplicationComponent implements MyApplicationComponent {
Yatagan$MyApplicationComponent() {
}
Expand Down Expand Up @@ -34,13 +38,17 @@ package test;
import com.yandex.yatagan.Lazy;
import com.yandex.yatagan.internal.Checks;
import com.yandex.yatagan.internal.ThreadAssertions;
import com.yandex.yatagan.internal.YataganGenerated;
import java.lang.AssertionError;
import java.lang.Object;
import java.lang.Override;
import java.lang.SuppressWarnings;
import javax.annotation.processing.Generated;
import javax.inject.Provider;

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@YataganGenerated
@Generated("com.yandex.yatagan.codegen.impl.ComponentGenerator")
public final class Yatagan$MyActivityComponent implements MyActivityComponent {
final MyApplicationComponent mMyApplicationComponent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
Name: test/Yatagan$MyApplicationComponent.java
package test;

import com.yandex.yatagan.internal.YataganGenerated;
import java.lang.Override;
import java.lang.SuppressWarnings;
import javax.annotation.processing.Generated;

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@YataganGenerated
@Generated("com.yandex.yatagan.codegen.impl.ComponentGenerator")
public final class Yatagan$MyApplicationComponent implements MyApplicationComponent {
Yatagan$MyApplicationComponent() {
}
Expand All @@ -29,13 +33,17 @@ package test;
import com.yandex.yatagan.Lazy;
import com.yandex.yatagan.internal.Checks;
import com.yandex.yatagan.internal.ThreadAssertions;
import com.yandex.yatagan.internal.YataganGenerated;
import java.lang.AssertionError;
import java.lang.Object;
import java.lang.Override;
import java.lang.SuppressWarnings;
import javax.annotation.processing.Generated;
import javax.inject.Provider;

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@YataganGenerated
@Generated("com.yandex.yatagan.codegen.impl.ComponentGenerator")
public final class Yatagan$MyActivityComponent implements MyActivityComponent {
final MyApplicationComponent mMyApplicationComponent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ Name: test/Yatagan$MyComponent.java
package test;

import com.yandex.yatagan.internal.Checks;
import com.yandex.yatagan.internal.YataganGenerated;
import java.lang.Override;
import java.lang.SuppressWarnings;
import javax.annotation.processing.Generated;

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@YataganGenerated
@Generated("com.yandex.yatagan.codegen.impl.ComponentGenerator")
public final class Yatagan$MyComponent implements MyComponent {
final Dependencies mDependencies;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ Name: test/Yatagan$TestComponent.java
package test;

import com.yandex.yatagan.internal.Checks;
import com.yandex.yatagan.internal.YataganGenerated;
import java.lang.Override;
import java.lang.SuppressWarnings;
import javax.annotation.processing.Generated;

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
@YataganGenerated
@Generated("com.yandex.yatagan.codegen.impl.ComponentGenerator")
public final class Yatagan$TestComponent implements TestComponent {
final MyImpl mImpl;

Expand All @@ -23,7 +27,6 @@ public final class Yatagan$TestComponent implements TestComponent {
return new ComponentFactoryImpl();
}

@SuppressWarnings({"unchecked", "rawtypes", "NullableProblems"})
static final class TestSubComponentImpl implements TestSubComponent {
final Yatagan$TestComponent mTestComponent;

Expand Down
Loading

0 comments on commit ecada0d

Please sign in to comment.