Skip to content
Merged
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
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
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_1_8
targetCompatibility JavaVersion.VERSION_1_8
sourceCompatibility JavaVersion.VERSION_11
targetCompatibility JavaVersion.VERSION_11
}

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 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;
import java.util.concurrent.atomic.AtomicInteger;


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();
try {
Object fooObject = scope.fooObject();
if(fooObject == null) {
isFooObjectNull.set(true);
}
} catch (Exception e) {
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);
}
}
}
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);
}
}
}