diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index b0eb95d5a4462a485122bc9190497df411651e8c..70bb38059646b195550297343a59d033eeec1729 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTransformer.java +++ b/migrator/src/com/ohos/migrator/java/JavaTransformer.java @@ -416,9 +416,24 @@ public class JavaTransformer extends ASTVisitor implements Transformer { public boolean visit(TypeParameter javaTypeParameter) { pushCurrent(new TypeParameterContext(stsCurrent, 0)); - // TODO: Add constraints! stsCurrent.addChild(NodeBuilder.terminalIdentifier(javaTypeParameter.getName())); + List javaTypeBounds = javaTypeParameter.typeBounds(); + if (javaTypeBounds != null && !javaTypeBounds.isEmpty()) { + pushCurrent(new ConstraintContext(stsCurrent, 0)); + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Extends)); + + boolean isIntersectionTypeBound = javaTypeBounds.size() > 1; + if (isIntersectionTypeBound) pushCurrent(new IntersectionTypeContext(stsCurrent, 0)); + + for (Type javaTypeBound : javaTypeBounds) { + javaTypeBound.accept(this); + } + + if (isIntersectionTypeBound) popCurrent(); // IntersectionTypeContext + popCurrent(); // ConstraintContext + } + popCurrent(); // TypeParameterContext return false; } @@ -797,7 +812,18 @@ public class JavaTransformer extends ASTVisitor implements Transformer { @Override public boolean visit(IntersectionType javaIntersectionType) { - // TODO: + boolean needPrimaryType = isInPrimaryTypeContext(); + if (needPrimaryType) pushCurrent(new PrimaryTypeContext(stsCurrent, 0)); + + pushCurrent(new IntersectionTypeContext(stsCurrent, 0)); + + List javaTypeList = javaIntersectionType.types(); + for (Type javaType : javaTypeList) + javaType.accept(this); + + popCurrent(); // IntersectionTypeContext + + if (needPrimaryType) popCurrent(); // PrimaryTypeContext return false; } diff --git a/migrator/test/java/intersection-type.java b/migrator/test/java/intersection-type.java new file mode 100644 index 0000000000000000000000000000000000000000..a1bdbbe67f7ebf83f5ac4d4ac9b68f68d7822211 --- /dev/null +++ b/migrator/test/java/intersection-type.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * 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 com.ohos.migrator.test.java; + +class intersection_type { + public void bar(auxilliary arg) { + Runnable r = (Runnable & Something)arg; + r.run(); + + Something s = (Something & Runnable)arg; + s.foo(); + } +} + +class auxilliary implements Runnable, Something { + public void run() { } + public void foo() { } +} + +interface Something { + void foo(); +} diff --git a/migrator/test/java/intersection-type.java.sts b/migrator/test/java/intersection-type.java.sts new file mode 100755 index 0000000000000000000000000000000000000000..96a82304a83561c6e3ebdfba83aed1e1f43db65a --- /dev/null +++ b/migrator/test/java/intersection-type.java.sts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * 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 com.ohos.migrator.test.java; + +open class intersection_type { + public open bar(arg : auxilliary): void { + let r : Runnable = arg as (Runnable & Something); + r.run(); + let s : Something = arg as (Something & Runnable); + s.foo(); + } +} + +open class auxilliary implements Runnable, Something { + public open run(): void { + } + public open foo(): void { + } +} + +interface Something { + foo(): void ; +} +