diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index 97e651b0f8d8d8ff777216e602c02b56a41bb16b..8a67148c8f843420dca6c6b10ac57712722c0da3 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTransformer.java +++ b/migrator/src/com/ohos/migrator/java/JavaTransformer.java @@ -307,7 +307,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { if ((javaModifiers & Modifier.STATIC) != 0) stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Static)); if ((javaModifiers & Modifier.NATIVE) != 0) stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Native)); } - + // Java tree: // TypeDeclaration: // A type declaration is the union of a class declaration and an interface declaration. // ClassDeclaration @@ -326,45 +326,41 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // [ permits Type { , Type } ] // { { InterfaceBodyDeclaration | ; } } // - // STS tree for interface: + // STS tree for class declaration: + // topDeclarationContext + // Export? + // ClassDeclarationContext + // TermminalNode <(static? (abstract | open) | (abstract | open)? static)?> ? + // TerminalNode + // TerminalNode + // TypeParametersContext ? + // ClassExtendsClauseContext ? + // ImplementsClauseContext ? + // ClassBodyContext + // TerminalNode <{> + // ClassMemberContext * + // clinit = CclassInitializerContext ? + // ClassMemberContext * + // TerminalNode <}> + // STS tree for interface declaration: // topDeclarationContext + // Export? // InterfaceDeclarationContext + // TermminalNode ? // TerminalNode - // TerminalNode + // TerminalNode + // TypeParametersContext ? + // InterfaceExtendsClauseContext ? // TerminalNode <{> // InterfaceBodyContext // null // TerminalNode <}> // - // STS src: class TestClassB extends TestClassA implements TestInterfaceA, TestInterfaceB {} - // Resulting tree: - // topDeclarationContext | ClassMemberContext | InterfaceMemberContext - // TerminalNode ? (for topDeclarationContext) - // | AccessibilityModifierContext? (for ClassMemberContext or InterfaceMemberContext) - // ClassDeclarationContext - // TerminalNode - // ClassExtendsClauseContext - // TerminalNode - // TypeReferenceContext - // QualifiedNameContext - // TerminalNode - // ImplementsClauseContext - // TerminalNode - // InterfaceTypeListContext - // TypeReferenceContext - // QualifiedNameContext - // TerminalNode - // TerminalNode <,> - // TypeReferenceContext - // QualifiedNameContext - // TerminalNode - // ClassBodyContext @Override public boolean visit(TypeDeclaration javaTypeDeclaration) { // Create appropriate member context to put declaration into. int javaMods = javaTypeDeclaration.getModifiers(); - ParserRuleContext stsMemberContext = createMemberContextWithAccessModifier(javaMods); - pushCurrent(stsMemberContext); + pushCurrent(createDeclarationOrMemberContextWithAccessModifier(javaMods)); // Create class or interface declaration context and select appropriate keyword. int terminalCode = StaticTSParser.Class; @@ -385,12 +381,8 @@ public class JavaTransformer extends ASTVisitor implements Transformer { stsCurrent.addChild(NodeBuilder.terminalIdentifier(javaTypeDeclaration.getName())); translateTypeParameters(javaTypeDeclaration.typeParameters()); - - translateSuperclassType(javaTypeDeclaration.getSuperclassType()); - - translateSuperInterfaceTypes(javaTypeDeclaration.superInterfaceTypes()); - - stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.OpenBrace)); // { + translateSuperclassType(javaTypeDeclaration.getSuperclassType()); // extends (not present for interface) + translateSuperInterfaceTypes(javaTypeDeclaration.superInterfaceTypes()); // implements (extends for interface) if (javaTypeDeclaration.isInterface()) { pushCurrent(new InterfaceBodyContext(stsCurrent, 0)); @@ -407,11 +399,9 @@ public class JavaTransformer extends ASTVisitor implements Transformer { addInstanceInitializersToCtors(javaTypeDeclaration); - stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.CloseBrace)); // } - - popCurrent(); // Interface/ClassBodyContext + popCurrent(); // InterfaceBodyContext or ClassBodyContext popCurrent(); // Interface/ClassDeclarationContext - popCurrent(); // stsMemberContext + popCurrent(); // DeclarationOrMemberContext ++countDeclTransformed; return false; @@ -451,9 +441,10 @@ public class JavaTransformer extends ASTVisitor implements Transformer { stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Extends)); } else { - pushCurrent(new ImplementsClauseContext(stsCurrent, 0)); - stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Implements)); + pushCurrent(new ImplementsClauseContext(stsCurrent, 0)); + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Implements)); } + pushCurrent(new InterfaceTypeListContext(stsCurrent, 0)); for (Type javaSuperInterfaceType : javaSuperInterfaceTypes) { @@ -478,15 +469,28 @@ public class JavaTransformer extends ASTVisitor implements Transformer { popCurrent(); // TypeParametersContext } + // Java tree: + // TypeParameter: { ExtendedModifier } Identifier [ extends Type { & Type } ] + // STS tree: + // typeParameter: Identifier constraint?; + // constraint: Extends (typeReference | intersectionType); + // typeReference: typeReferencePart (Dot typeReferencePart)* + // typeReferencePart: qualifiedName typeArguments? + // typeArguments: LessThan typeArgumentList? MoreThan + // intersectionType: OpenParen typeReference (BitAnd typeReference)+ CloseParen @Override public boolean visit(TypeParameter javaTypeParameter) { pushCurrent(new TypeParameterContext(stsCurrent, 0)); stsCurrent.addChild(NodeBuilder.terminalIdentifier(javaTypeParameter.getName())); + // ExtendedModifiers are ignored at the moment and seems do not need to be translated. + List javaTypeBounds = javaTypeParameter.typeBounds(); - if (javaTypeBounds != null && !javaTypeBounds.isEmpty()) { + + if (!javaTypeBounds.isEmpty()) { pushCurrent(new ConstraintContext(stsCurrent, 0)); + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Extends)); boolean isIntersectionTypeBound = javaTypeBounds.size() > 1; @@ -504,7 +508,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return false; } - private ParserRuleContext createMemberContextWithAccessModifier(int javaMods) { + private ParserRuleContext createDeclarationOrMemberContextWithAccessModifier(int javaMods) { boolean isInClassContext = stsCurrent instanceof ClassBodyContext; boolean isInInterfaceContext = stsCurrent instanceof InterfaceBodyContext; @@ -520,12 +524,14 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // Process access modifier. In top-level context, public translates to export, // everything else to none. In all other contexts, emit AccessibilityModifierContext. - // NOTE: In all cases, resulting node should NOT be a child of declaration node! if (isInClassContext) { AccessibilityModifierContext stsAccessMod = NodeBuilder.accessibilityModifier(javaMods); - if (stsAccessMod != null) {//stsMemberContext = null; - stsMemberContext.addChild(stsAccessMod).setParent(stsMemberContext); - } + if (stsAccessMod != null) stsMemberContext.addChild(stsAccessMod).setParent(stsMemberContext); + } + else if (isInInterfaceContext) { + // Note: 'public' modifier is not permitted for interface's members. + AccessibilityModifierContext stsAccessMod = NodeBuilder.accessibilityModifier(javaMods & ~Modifier.PUBLIC); + if (stsAccessMod != null) stsMemberContext.addChild(stsAccessMod).setParent(stsMemberContext); } else if ((javaMods & Modifier.PUBLIC) != 0) stsMemberContext.addChild(NodeBuilder.terminalNode(StaticTSParser.Export)); @@ -611,7 +617,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { List javaVarDeclFragments = javaFieldDecl.fragments(); for (VariableDeclarationFragment javaVarDeclFragment : javaVarDeclFragments) { - pushCurrent(createMemberContextWithAccessModifier(javaMods)); + pushCurrent(createDeclarationOrMemberContextWithAccessModifier(javaMods)); ParserRuleContext stsClassOrInterField = isInClassContext ? new ClassFieldDeclarationContext(stsCurrent, 0) : new InterfaceFieldContext((InterfaceMemberContext)stsCurrent); @@ -840,17 +846,8 @@ public class JavaTransformer extends ASTVisitor implements Transformer { assert(lastChild instanceof TypeReferencePartContext && lastChild.getChildCount() == 1); pushCurrent((TypeReferencePartContext)lastChild, false); - pushCurrent(new TypeArgumentsContext(stsCurrent, 0)); - pushCurrent(new TypeArgumentListContext(stsCurrent, 0)); + translateTypeArguments(javaTypeArgs); - for (Type javaTypeArg : javaTypeArgs) { - pushCurrent(new TypeArgumentContext(stsCurrent, 0)); - javaTypeArg.accept(this); - popCurrent(); // TypeArgumentContext - } - - popCurrent(); // TypeArgumentListContext - popCurrent(); // TypeArgumentsContext popCurrent(); // (TypeReferencePartContext)lastChild popCurrent(); // (TypeReferenceContext)lastChild } @@ -1320,7 +1317,9 @@ public class JavaTransformer extends ASTVisitor implements Transformer { pushCurrent(new TypeArgumentListContext(stsCurrent, 0)); for (Type javaTypeArg : javaTypeArgs) { + pushCurrent(new TypeArgumentContext(stsCurrent, 0)); javaTypeArg.accept(this); + popCurrent(); // TypeArgumentContext } popCurrent(); // TypeArgumentList @@ -1396,7 +1395,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { ParserRuleContext enclosingContext = stsCurrent.getParent(); int javaMods = javaMethodDeclaration.getModifiers(); - pushCurrent(createMemberContextWithAccessModifier(javaMods)); + pushCurrent(createDeclarationOrMemberContextWithAccessModifier(javaMods)); Block javaBlock = javaMethodDeclaration.getBody(); @@ -1549,6 +1548,8 @@ public class JavaTransformer extends ASTVisitor implements Transformer { popCurrent(); // TypeAnnotationContext + // TODO: { Dimension } + //javaSingleVariableDeclaration. // TODO: [= Expression ] // Expression javaExpression = javaSingleVariableDeclaration.getInitializer(); // if (javaExpression != null) { @@ -1560,6 +1561,9 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return false; } + // NOTE: All Java enums are translated into STS classes because of + // built-in methods values() and valueOf() available to the former! + // // Java tree: // VariableDeclarationFragment: // Identifier { Dimension } [ = Expression ] @@ -1636,7 +1640,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { public boolean visit(EnumDeclaration javaEnumDeclaration) { // Create appropriate member context to put declaration into. int javaEnumMods = javaEnumDeclaration.getModifiers(); - pushCurrent(createMemberContextWithAccessModifier(javaEnumMods)); + pushCurrent(createDeclarationOrMemberContextWithAccessModifier(javaEnumMods)); // Create class declaration context ClassDeclarationContext stsClassDecl = new ClassDeclarationContext(stsCurrent, 0); @@ -1735,6 +1739,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { ++countDeclTransformed; return false; } + private void createEnumDefaultCtor() { pushCurrent(new ClassMemberContext(stsCurrent, 0)); stsCurrent.addChild(NodeBuilder.accessibilityModifier(Modifier.PRIVATE)).setParent(stsCurrent); @@ -1791,6 +1796,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { stsEnumCtorCallArg.setParent(stsExprSeq); } } + private void createEnumExtendsClause(String javaEnumName) { // Note: A Java enum extends Enum class. pushCurrent(new ClassExtendsClauseContext(stsCurrent, 0)); @@ -1809,6 +1815,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { popCurrent(); // TypeReferenceContext popCurrent(); // ClassExtendsClauseContext } + private void pushEnumBuiltinMethod() { // Create class member context and add public modifier pushCurrent(new ClassMemberContext(stsCurrent, 0)); @@ -1819,6 +1826,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { pushCurrent(new ClassMethodWithBodyContext((ClassMethodDeclarationContext)stsCurrent)); stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Static)); } + private void popEnumBuiltinMethod() { popCurrent(); // ClassMethodWithBodyContext popCurrent(); // ClassMethodDeclarationContext @@ -1932,6 +1940,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { popEnumBuiltinMethod(); } + @Override public boolean visit(EnumConstantDeclaration javaEnumConstant) { // Create class member context and add public modifier @@ -2590,10 +2599,6 @@ public class JavaTransformer extends ASTVisitor implements Transformer { javaArrayInitializer.accept(this); } -// popSingleExpression(); // NewArrayExpression - - // TODO: Needs reworking. - // ++countExprTransformed. return false; } @@ -2715,7 +2720,6 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return false; } - // Java tree: // ForStatement: // for ( @@ -3288,6 +3292,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // statement, so that variables are only visible in context of switch statement. List javaVarFragments = javaVarDeclStmt.fragments(); + for (VariableDeclarationFragment javaVarFragment : javaVarFragments) { if (isUsedInAnotherCaseClause(javaVarFragment, javaCurrentSwitchCase, javaSwitchStmt)) { javaVariablesToMove.add(javaVarFragment); @@ -3446,14 +3451,24 @@ public class JavaTransformer extends ASTVisitor implements Transformer { translateBlockStatements((Block)javaBody); popCurrent(); // BlockContext } - popCurrent(); // LambdaBodyContext + popCurrent(); // LambdaBodyContext popSingleExpression(); // LambdaExpressionContext ++countExprTransformed; return false; } + @Override + public boolean visit(ExpressionMethodReference javaExprMethodRef) { + // TODO: To be implemented + // Emit __untranslated_expression call with commented-out original syntax as argument for now. + // This is done to avoid building invalid STS AST which causes exceptions in StaticTSWriter. + stsCurrent.addChild(NodeBuilder.untranslatedExpression(javaExprMethodRef)).setParent(stsCurrent); + + return false; + } + // Java tree: // CreationReference: // Type :: @@ -3496,16 +3511,6 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return false; } - @Override - public boolean visit(ExpressionMethodReference javaExprMethodRef) { - // TODO: To be implemented - // Emit __untranslated_expression call with commented-out original syntax as argument for now. - // This is done to avoid building invalid STS AST which causes exceptions in StaticTSWriter. - stsCurrent.addChild(NodeBuilder.untranslatedExpression(javaExprMethodRef)).setParent(stsCurrent); - - return false; - } - // Java AST: // TryStatement: // try Block Catches @@ -3696,9 +3701,6 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // The list of not yet translated Java Expressions: // CreationReference, - //?? ExpressionMethodReference, // SuperMethodReference, // TypeMethodReference - // TryStatement - // ThrowStatement -} \ No newline at end of file +} diff --git a/migrator/test/java/generic_class_1.java b/migrator/test/java/generic_class_1.java new file mode 100644 index 0000000000000000000000000000000000000000..1b15fde1eb9d9093283c2da8055ab1e6ed3d5d71 --- /dev/null +++ b/migrator/test/java/generic_class_1.java @@ -0,0 +1,28 @@ +/* + * 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; + +// Java specification Example 8.1.2-1. Mutually Recursive Type Variable Bounds: + +interface ConvertibleTo { + T convert(); +} + +class ReprChange, S extends ConvertibleTo> { + T t; + void set(S s) { t = s.convert(); } + S get() { return t.convert(); } + } \ No newline at end of file diff --git a/migrator/test/java/generic_class_1.java.sts b/migrator/test/java/generic_class_1.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..192ed6b394cc627b6416df56cf5d0930f78d0f44 --- /dev/null +++ b/migrator/test/java/generic_class_1.java.sts @@ -0,0 +1,30 @@ +/* + * 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; + +interface ConvertibleTo { + convert(): T ; +} + +open class ReprChange, S extends ConvertibleTo> { + t : T ; + open set(s : S): void { + t = s.convert(); + } + open get(): S { + return t.convert(); + } +} + diff --git a/migrator/test/java/generic_class_2.java b/migrator/test/java/generic_class_2.java new file mode 100644 index 0000000000000000000000000000000000000000..6959f15b3f7de86b55f9279958e6fa35d8874f61 --- /dev/null +++ b/migrator/test/java/generic_class_2.java @@ -0,0 +1,57 @@ +/* + * 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; + +// Java specification Example 8.1.2-2. Nested Generic Classes + +class Seq { + T head; + Seq tail; + Seq() { this(null, null); } + Seq(T head, Seq tail) { + this.head = head; + this.tail = tail; + } + + boolean isEmpty() { return tail == null; } + + class Zipper { + Seq> zip(Seq that) { + if (isEmpty() || that.isEmpty()) { + return new Seq>(); + } else { + Seq.Zipper tailZipper = tail.new Zipper(); + + return new Seq>(new Pair(head, that.head), tailZipper.zip(that.tail)); + } + } + } +} + +class Pair { + T fst; + S snd; + Pair(T f, S s) { fst = f; snd = s; } +} + +class Test { + public static void main(String[] args) { + Seq strs = new Seq("a", new Seq("b", new Seq())); + Seq nums = new Seq(new Integer(1), new Seq(new Double(1.5), new Seq())); + Seq.Zipper zipper = strs.new Zipper(); + Seq> combined = zipper.zip(nums); + } +} \ No newline at end of file diff --git a/migrator/test/java/generic_class_2.java.sts b/migrator/test/java/generic_class_2.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..1bd7124f446c87af7f0500a18e8e3dbb27c2a382 --- /dev/null +++ b/migrator/test/java/generic_class_2.java.sts @@ -0,0 +1,64 @@ +/* + * 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 Seq { + head : T ; + tail : Seq ; + constructor() { + this(null, null); + } + + constructor(head : T, tail : Seq) { + this.head = head; + this.tail = tail; + } + + open isEmpty(): boolean { + return tail == null; + } + open class Zipper { + open zip(that : Seq): Seq> { + if (isEmpty() || that.isEmpty()) { + return new Seq>(); + } + else { + let tailZipper : Seq.Zipper = new Zipper(); + return new Seq>(new Pair(head, that.head), tailZipper.zip(that.tail)); + } + } + } + +} + +open class Pair { + fst : T ; + snd : S ; + constructor(f : T, s : S) { + fst = f; + snd = s; + } + +} + +open class Test { + public static main(args : String[]): void { + let strs : Seq = new Seq("a", new Seq("b", new Seq())); + let nums : Seq = new Seq(new Integer(1), new Seq(new Double(1.5), new Seq())); + let zipper : Seq.Zipper = new Zipper(); + let combined : Seq> = zipper.zip(nums); + } +} + diff --git a/migrator/test/java/generic_class_3.java b/migrator/test/java/generic_class_3.java new file mode 100644 index 0000000000000000000000000000000000000000..ef050c1b02b35f6d3ece8c8b1132ed2b353aff51 --- /dev/null +++ b/migrator/test/java/generic_class_3.java @@ -0,0 +1,54 @@ +/* + * 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; + +interface Fun { R apply(T arg); } + +class C { + Integer size() { return 0; } + + Integer size(Object arg) { return 1; } + + Integer size(C arg) { return 2; } + + static class H { + Integer size() {return 3;} + } + + H h = new H(); + + private interface Func { + R apply(C c); + } + + void test1() { + Func f1 = (C c) -> c.size(); + System.out.print(f1.apply(this)); + } + + void test2() { + Func f1 = (H h) -> h.size(); + System.out.print(f1.apply(h)); + } +} + +class Main { + public static void main(String args[]) { + C c = new C(); + c.test1(); + c.test2(); + } +} \ No newline at end of file diff --git a/migrator/test/java/generic_class_3.java.sts b/migrator/test/java/generic_class_3.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..4fa4facde40573217f5846e6f9b6e8a0bc3f302b --- /dev/null +++ b/migrator/test/java/generic_class_3.java.sts @@ -0,0 +1,60 @@ +/* + * 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; + +interface Fun { + apply(arg : T): R ; +} + +open class C { + open size(): Integer { + return 0; + } + open size(arg : Object): Integer { + return 1; + } + open size(arg : C): Integer { + return 2; + } + static open class H { + open size(): Integer { + return 3; + } + } + + h : H = new H(); + private interface Func { + apply(c : C): R ; + } + + open test1(): void { + let f1 : Func = (c : C): java.lang.Integer =>c.size(); + System.out.print(f1.apply(this)); + } + open test2(): void { + let f1 : Func = (h : H): java.lang.Integer =>h.size(); + System.out.print(f1.apply(h)); + } +} + +open class Main { + public static main(args : String[]): void { + let c : C = new C(); + c.test1(); + c.test2(); + } +} + diff --git a/migrator/test/java/generic_class_4.java b/migrator/test/java/generic_class_4.java new file mode 100644 index 0000000000000000000000000000000000000000..6b48d1b69feabd459123b71c0278e8f5f4bb4339 --- /dev/null +++ b/migrator/test/java/generic_class_4.java @@ -0,0 +1,22 @@ +/* + * 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 test_generic { + Class choose(boolean b, Class c1, Class c2) { + return b ? c1 : c2; + } +} \ No newline at end of file diff --git a/migrator/test/java/generic_class_4.java.sts b/migrator/test/java/generic_class_4.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..ebec1d974c33dcb10d6a87de57331266f195533c --- /dev/null +++ b/migrator/test/java/generic_class_4.java.sts @@ -0,0 +1,23 @@ +/* + * 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 test_generic { + open choose(b : boolean, c1 : Class, c2 : Class): Class { + return b ? c1 : c2; + } +} + diff --git a/migrator/test/java/generic_interface_1.java b/migrator/test/java/generic_interface_1.java new file mode 100644 index 0000000000000000000000000000000000000000..3f8d042da91ce806640ba288a25ebdf7da57fe85 --- /dev/null +++ b/migrator/test/java/generic_interface_1.java @@ -0,0 +1,26 @@ +/* + * 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; + +// Java specification Example 9.8-3. Generic Functional Interfaces + +//interface I { Object m(Class c); } + +interface J { S m(Class c); } + +interface K { T m(Class c); } + +//interface Functional extends I, J, K {} \ No newline at end of file diff --git a/migrator/test/java/generic_interface_1.java.sts b/migrator/test/java/generic_interface_1.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..867ca6aa4c6461a44099d9fd93719ccb41a0c0ac --- /dev/null +++ b/migrator/test/java/generic_interface_1.java.sts @@ -0,0 +1,25 @@ +/* + * 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; + +interface J { + m(c : Class): S ; +} + +interface K { + m(c : Class): T ; +} + diff --git a/migrator/test/java/generic_interface_2.java b/migrator/test/java/generic_interface_2.java new file mode 100644 index 0000000000000000000000000000000000000000..9c82acd990134c48f3214b4f64b4bdee42587b24 --- /dev/null +++ b/migrator/test/java/generic_interface_2.java @@ -0,0 +1,28 @@ +/* + * 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; + +// Java specification Example 9.9-2. Generic Function Types + +interface G1 { + Object m() throws E; +} + +interface G2 { + String m() throws Exception; +} + +interface G extends G1, G2 {} \ No newline at end of file diff --git a/migrator/test/java/generic_interface_2.java.sts b/migrator/test/java/generic_interface_2.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..9bd613d2f5af512579e0cf9c316193b6050bdba7 --- /dev/null +++ b/migrator/test/java/generic_interface_2.java.sts @@ -0,0 +1,27 @@ +/* + * 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; + +interface G1 { + m(): Object ; +} + +interface G2 { + m(): String ; +} + +interface G extends G1, G2 { +} + diff --git a/migrator/test/java/interface_public.java.sts b/migrator/test/java/interface_public.java.sts index 8e06462c995b1befb9326b40f3b71c3622b9eb21..06409cf69c1e8fb3b9f3629abcb3d3039d78b6c1 100644 --- a/migrator/test/java/interface_public.java.sts +++ b/migrator/test/java/interface_public.java.sts @@ -22,7 +22,7 @@ export interface interface_public { foo(): void ; static foo(i : int): void { } - foo(b : boolean): void { + private foo(b : boolean): void { } foo(s : String): void ; static Pi(): double { diff --git a/migrator/test/java/test_interface.java.sts b/migrator/test/java/test_interface.java.sts index 1ad4abae3647c4e8347bc8f057c95354938f73d9..d90b309421aa877ee5dba590e8b12a2f746b5656 100644 --- a/migrator/test/java/test_interface.java.sts +++ b/migrator/test/java/test_interface.java.sts @@ -22,7 +22,7 @@ interface test_interface { foo(): void ; static foo(i : int): void { } - foo(b : boolean): void { + private foo(b : boolean): void { } foo(s : String): void ; }