Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 0.4.0-alpha08
* Add support for skipping field initialization when initializing a scope.

# 0.4.0-alpha06
* Add support for Kotlin 2.1.0

Expand Down
5 changes: 3 additions & 2 deletions compiler/src/main/kotlin/motif/compiler/JavaCodeGenerator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ object JavaCodeGenerator {
.add("Object $localFieldName = \$N;\n", cacheFieldName)
.beginControlFlow("if (\$N == null)", localFieldName)
.beginControlFlow("synchronized (this)")
.beginControlFlow("if (\$N == null)", cacheFieldName)
.add("\$N = \$N;\n", localFieldName, cacheFieldName)
.beginControlFlow("if (\$N == null)", localFieldName)
.add("\$N = \$L;\n", localFieldName, instantiation.spec())
.beginControlFlow("if (\$N == null)", localFieldName)
.add(
Expand All @@ -203,7 +204,7 @@ object JavaCodeGenerator {
"Factory method cannot return null",
)
.endControlFlow()
.add("\$N = \$L;\n", cacheFieldName, localFieldName)
.add("\$N = \$N;\n", cacheFieldName, localFieldName)
.endControlFlow()
.endControlFlow()
.endControlFlow()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ object KotlinCodeGenerator {
.addStatement("var $localFieldName = %N;\n", cacheFieldName)
.beginControlFlow("if (%N == null)", localFieldName)
.beginControlFlow("synchronized (this)")
.beginControlFlow("if (%N == null)", cacheFieldName)
.addStatement("%N = %N", localFieldName, cacheFieldName)
.beginControlFlow("if (%N == null)", localFieldName)
.addStatement("%N = %L", localFieldName, instantiation.spec())
.addStatement("%N = %N", cacheFieldName, localFieldName)
.endControlFlow()
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# limitations under the License.
#
GROUP=com.uber.motif
VERSION_NAME=0.4.0-alpha08-SNAPSHOT
VERSION_NAME=0.4.0-alpha09-SNAPSHOT
POM_DESCRIPTION=Simple DI API for Android / Java.
POM_URL=https://github.com/uber/motif/
POM_SCM_URL=https://github.com/uber/motif/
Expand Down
4 changes: 2 additions & 2 deletions gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ ext.deps = [

gradlePlugins: [
android: 'com.android.tools.build:gradle:7.4.2',
intellij: 'org.jetbrains.intellij:org.jetbrains.intellij.gradle.plugin:1.15.0',
intellij: 'org.jetbrains.intellij:org.jetbrains.intellij.gradle.plugin:1.17.4',
kotlin: "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kotlin}",
ksp: "com.google.devtools.ksp:com.google.devtools.ksp.gradle.plugin:${versions.ksp}",
dokka: "org.jetbrains.dokka:dokka-gradle-plugin:${versions.dokka}",
mavenPublish: 'com.vanniktech:gradle-maven-publish-plugin:0.27.0',
mavenPublish: 'com.vanniktech:gradle-maven-publish-plugin:0.34.0',
spotless: "com.diffplug.spotless:spotless-plugin-gradle:7.0.2",
shadow: "com.github.johnrengelman:shadow:8.1.0",
]
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.3-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
4 changes: 2 additions & 2 deletions samples/sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ android {
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

defaultConfig {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
########################################################################
# #
# This file is auto-generated by running the Motif compiler tests and #
# serves a as validation of graph correctness. IntelliJ plugin tests #
# also rely on this file to ensure that the plugin graph understanding #
# is equivalent to the compiler's. #
# #
# - Do not edit manually. #
# - Commit changes to source control. #
# - Since this file is autogenerated, code review changes carefully to #
# ensure correctness. #
# #
########################################################################

-------
| Scope |
-------

==== Required ====

==== Provides ====

---- Object | Objects.fooObject ----
[ Required ]
[ Consumed By ]
* Scope | Scope.fooObject()

---- Scope | implicit ----
[ Required ]
[ Consumed By ]


Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package testcases.KT009_use_null_field_concurrency_kotlin

import motif.Creatable

@motif.Scope(useNullFieldInitialization = true)
interface Scope : Creatable<Scope.Dependencies> {
fun fooObject(): Any

@motif.Objects
abstract class Objects {
fun fooObject(): Any {
return Any()
}
}

interface Dependencies
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package testcases.KT009_use_null_field_concurrency_kotlin

import com.google.common.truth.Truth
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.atomic.AtomicBoolean
import java.util.concurrent.atomic.AtomicInteger

object Test {
/**
* This tests if the ScopeImpl synchronized blocks check and assign correct values
*/
@JvmStatic
fun run() {
val scope: Scope = ScopeImpl()
val nThreads = 2
val executorService = Executors.newFixedThreadPool(nThreads)
val getFooObjectLatch = CountDownLatch(nThreads)
val end = CountDownLatch(nThreads)
val isFooObjectNull = AtomicBoolean(true)
val count = AtomicInteger(0)
try {
synchronized(scope) {
// blocks the synchronized in scope.fooObject
for (i in 0 until nThreads) {
executorService.submit {
getFooObjectLatch.countDown()
try {
val fooObject = scope.fooObject()
if (fooObject != null) {
count.incrementAndGet()
isFooObjectNull.set(false)
}
} catch (e: Exception) {
isFooObjectNull.set(true)
}
end.countDown()
}
}
getFooObjectLatch.await(1000, TimeUnit.MILLISECONDS)
}

// at this point, the two threads will compete to create the fooObject

// Verify
if (end.await(1000, TimeUnit.MILLISECONDS)) {
Truth.assertThat(isFooObjectNull.get()).isFalse()
}
} catch (e: InterruptedException) {
throw RuntimeException(e)
}
Truth.assertThat(count.get()).isEqualTo(nThreads)
Truth.assertThat(true).isNull()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
########################################################################
# #
# This file is auto-generated by running the Motif compiler tests and #
# serves a as validation of graph correctness. IntelliJ plugin tests #
# also rely on this file to ensure that the plugin graph understanding #
# is equivalent to the compiler's. #
# #
# - Do not edit manually. #
# - Commit changes to source control. #
# - Since this file is autogenerated, code review changes carefully to #
# ensure correctness. #
# #
########################################################################

-------
| Scope |
-------

==== Required ====

==== Provides ====

---- Object | Objects.fooObject ----
[ Required ]
[ Consumed By ]
* Scope | Scope.fooObject()

---- Scope | implicit ----
[ Required ]
[ Consumed By ]


Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package testcases.T078_use_null_field_concurrency_java;

import motif.Creatable;

@motif.Scope(useNullFieldInitialization = true)
public interface Scope extends Creatable<Scope.Dependencies> {

Object fooObject();

@motif.Objects
class Objects {

Object fooObject() {
return new Object();
}

}

interface Dependencies {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright (c) 2018-2019 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package testcases.T078_use_null_field_concurrency_java;

import static com.google.common.truth.Truth.assertThat;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

public class Test {

/**
* This tests if the ScopeImpl synchronized blocks check and assign correct values
*/
public static void run() {
Scope scope = new ScopeImpl();
int nThreads = 2;
ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
CountDownLatch getFooObjectLatch = new CountDownLatch(nThreads);
CountDownLatch end = new CountDownLatch(nThreads);
AtomicBoolean isFooObjectNull = new AtomicBoolean(false);
try {
synchronized (scope) { // blocks the synchronized in scope.fooObject
for (int i = 0; i < nThreads; i++) {
executorService.submit(() -> {
getFooObjectLatch.countDown();
Object fooObject = scope.fooObject();
if(fooObject == null) {
isFooObjectNull.set(true);
}
end.countDown();
});
}
getFooObjectLatch.await(1000, TimeUnit.MILLISECONDS);
}
// at this point, the two threads will compete to create the fooObject

// Verify
if(end.await(1000, TimeUnit.MILLISECONDS)) {
assertThat(isFooObjectNull.get()).isFalse();
}
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}