Skip to content

Commit b8ba1d7

Browse files
authored
Module self depend on detection (#990)
1 parent 8aabbfd commit b8ba1d7

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

sofa-boot-project/sofa-boot-core/isle-sofa-boot/src/main/java/com/alipay/sofa/isle/deployment/DependencyTree.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package com.alipay.sofa.isle.deployment;
1818

19+
import com.alipay.sofa.boot.error.ErrorCode;
20+
1921
import java.util.ArrayList;
2022
import java.util.Arrays;
2123
import java.util.Collection;
@@ -251,6 +253,9 @@ public final void addDependsOnMe(Entry<K, T> entry) {
251253
if (dependsOnMe == null) {
252254
dependsOnMe = new HashSet<>();
253255
}
256+
if (this.equals(entry)) {
257+
throw new IllegalArgumentException(ErrorCode.convert("01-12001", this.getKey()));
258+
}
254259
dependsOnMe.add(entry);
255260
}
256261

@@ -262,6 +267,9 @@ public void addDependency(Entry<K, T> entry) {
262267
if (dependencies == null) {
263268
dependencies = new CopyOnWriteArraySet<>();
264269
}
270+
if (this.equals(entry)) {
271+
throw new IllegalArgumentException(ErrorCode.convert("01-12001", this.getKey()));
272+
}
265273
dependencies.add(entry);
266274
}
267275

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.alipay.sofa.isle.test;
18+
19+
import com.alipay.sofa.isle.deployment.DependencyTree;
20+
import org.junit.Assert;
21+
import org.junit.Test;
22+
23+
/**
24+
* @author huzijie
25+
* @version DependencyTreeTest.java, v 0.1 2022年06月22日 11:45 AM huzijie Exp $
26+
*/
27+
public class DependencyTreeTest {
28+
29+
@Test
30+
public void testSelfDependencyCheck() {
31+
DependencyTree<String, String> dependencyTree = new DependencyTree<>();
32+
33+
try {
34+
dependencyTree.add("A", "", "A");
35+
Assert.fail();
36+
} catch (Throwable e) {
37+
Assert.assertTrue(e.getMessage().contains("01-12001"));
38+
}
39+
}
40+
}

sofa-boot-project/sofa-boot/src/main/resources/sofa-boot/log-codes.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262

6363
##Module dependency error
6464
01-12000=Modules that could not install(Mainly due to module dependency not satisfied)
65+
01-12001=Module could not depend on itself: [%s]
6566

6667
##Module profile error
6768
01-13000=Must specify at least one sofa module profile,at least one profile value is %s

0 commit comments

Comments
 (0)