diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index 08ab0105da632071e6e5815e607d2759604e5a8c..25cd45f53897cec1b861f439a94220a72b5cdf60 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTransformer.java +++ b/migrator/src/com/ohos/migrator/java/JavaTransformer.java @@ -898,6 +898,26 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return false; } + private void translateType(ITypeBinding javaType) { + boolean needPrimaryType = isInPrimaryTypeContext(); + if (needPrimaryType) pushCurrent(new PrimaryTypeContext(stsCurrent, 0)); + + // NOTE: Method "ITypeBinding.getQualifiedName()" constructs fully-qualified name + // of the corresponding type, including its generic part, array brackets, etc., + // and returns it as a string value. Using this method, we can avoid constructing + // entire STS tree for the corresponding type, simply making an identifier token + // with string value of type's fully-qualified name and wrapping it with a single + // TypeReferenceContext node. + // + // See the following link for details: + // https://help.eclipse.org/latest/topic/org.eclipse.jdt.doc.isv/reference/api/org/eclipse/jdt/core/dom/ITypeBinding.html#getQualifiedName() + + TypeReferenceContext stsTypeRef = NodeBuilder.typeReference(javaType.getQualifiedName()); + stsCurrent.addChild(stsTypeRef).setParent(stsCurrent); + + if (needPrimaryType) popCurrent(); // PrimaryTypeContext + } + // Java tree: // Expression: NullLiteral // STS tree: @@ -997,9 +1017,6 @@ public class JavaTransformer extends ASTVisitor implements Transformer { else if (javaOp == InfixExpression.Operator.REMAINDER) stsOperator = StaticTSParser.Modulus; else if (javaOp == InfixExpression.Operator.PLUS) stsOperator = StaticTSParser.Plus; else if (javaOp == InfixExpression.Operator.MINUS) stsOperator = StaticTSParser.Minus; - else if (javaOp == InfixExpression.Operator.LEFT_SHIFT) stsOperator = StaticTSParser.LeftShiftArithmetic; - else if (javaOp == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) stsOperator = StaticTSParser.RightShiftArithmetic; - else if (javaOp == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) stsOperator = StaticTSParser.RightShiftLogical; else if (javaOp == InfixExpression.Operator.LESS) stsOperator = StaticTSParser.LessThan; else if (javaOp == InfixExpression.Operator.GREATER) stsOperator = StaticTSParser.MoreThan; else if (javaOp == InfixExpression.Operator.LESS_EQUALS) stsOperator = StaticTSParser.LessThanEquals; @@ -1017,6 +1034,19 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return stsOperator; } + private ParseTree createStsInfixOperator(InfixExpression.Operator javaOp) { + if (isShiftOperator(javaOp)) + return NodeBuilder.shiftOperator(javaOp); + else + return NodeBuilder.terminalNode(stsOperatorType(javaOp)); + } + + private boolean isShiftOperator(InfixExpression.Operator javaInfixOp) { + return javaInfixOp == InfixExpression.Operator.LEFT_SHIFT + || javaInfixOp == InfixExpression.Operator.RIGHT_SHIFT_SIGNED + || javaInfixOp == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED; + } + private ParserRuleContext createStsInfixExpression(SingleExpressionContext stsSingleExpression, InfixExpression javaInfixExpression) { InfixExpression.Operator javaOp = javaInfixExpression.getOperator(); @@ -1024,7 +1054,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return new MultiplicativeExpressionContext(stsSingleExpression); else if (javaOp == InfixExpression.Operator.PLUS || javaOp == InfixExpression.Operator.MINUS) return new AdditiveExpressionContext(stsSingleExpression); - else if (javaOp == InfixExpression.Operator.LEFT_SHIFT || javaOp == InfixExpression.Operator.RIGHT_SHIFT_SIGNED || javaOp == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) + else if (isShiftOperator(javaOp)) return new BitShiftExpressionContext(stsSingleExpression); else if (javaOp == InfixExpression.Operator.LESS || javaOp == InfixExpression.Operator.GREATER || javaOp == InfixExpression.Operator.LESS_EQUALS || javaOp == InfixExpression.Operator.GREATER_EQUALS) return new RelationalExpressionContext(stsSingleExpression); @@ -1064,11 +1094,10 @@ public class JavaTransformer extends ASTVisitor implements Transformer { @Override public boolean visit(InfixExpression javaInfixExpression) { pushCurrent(createStsInfixExpression(pushSingleExpression(), javaInfixExpression)); - - int stsOpType = stsOperatorType(javaInfixExpression.getOperator()); + InfixExpression.Operator javaOp = javaInfixExpression.getOperator(); javaInfixExpression.getLeftOperand().accept(this); - stsCurrent.addChild(NodeBuilder.terminalNode(stsOpType)).setParent(stsCurrent); + stsCurrent.addAnyChild(createStsInfixOperator(javaOp)).setParent(stsCurrent); javaInfixExpression.getRightOperand().accept(this); popSingleExpression(); // InfixExpression @@ -1083,7 +1112,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { pushCurrent(createStsInfixExpression(pushSingleExpression(), javaInfixExpression)); stsCurrent.addChild(stsLeftOperand).setParent(stsCurrent); - stsCurrent.addChild(NodeBuilder.terminalNode(stsOpType)).setParent(stsCurrent); + stsCurrent.addAnyChild(createStsInfixOperator(javaOp)).setParent(stsCurrent); javaRightExpression.accept(this); popSingleExpression(); // InfixExpression @@ -1269,16 +1298,16 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // STS tree: // OpenParen parameterList? CloseParen // parameterList: parameter (Comma parameter)* (Comma variadicParameter)? | variadicParameter - private void createStsParameterList(List javaParameters) { - // OpenParen formalParameterList? CloseParen + private void createStsParameterList(List javaParameters) { + // OpenParen parameterList? CloseParen stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.OpenParen)).setParent(stsCurrent); assert(javaParameters != null); if (!javaParameters.isEmpty()) { pushCurrent(new ParameterListContext(stsCurrent, 0)); - for (SingleVariableDeclaration javaSingleVariableDeclaration : javaParameters) { - javaSingleVariableDeclaration.accept(this); + for (VariableDeclaration javaVariableDeclaration : javaParameters) { + javaVariableDeclaration.accept(this); } popCurrent(); // ParameterListContext @@ -1308,13 +1337,19 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // ( Block | ; ) // STS tree: // interfaceMember: methodSignature SemiColon #InterfaceMethod - // classMember: constructorDeclaration | classMethodDeclaration | other alternatives are not relevant for this visitor. - // constructorDeclaration: Constructor OpenParen formalParameterList? CloseParen OpenBrace functionBody CloseBrace + // classMember: + // accessibilityModifier? + // ( + // constructorDeclaration + // | classMethodDeclaration + // | other alternatives are not relevant for this visitor. + // ) + // constructorDeclaration: Constructor OpenParen parameterList? CloseParen constructorBody + // constructorBody: OpenBrace constructorCall? statementOrLocalDeclaration* CloseBrace // classMethodDeclaration - // : accessibilityModifier? (Static | Override | Open)? methodSignature OpenBrace functionBody CloseBrace #ClassMethodWithBody - // | Abstract accessibilityModifier? methodSignature SemiColon #AbstractClassMethod - // methodSignature: Identifier callSignature - // callSignature: typeParameters? OpenParen formalParameterList? CloseParen typeAnnotation + // : (Static | Override | Open)? Identifier signature block #ClassMethodWithBody + // | (Abstract | Static? Native | Native Static)? Identifier signature SemiColon #AbstractOrNativeClassMethod + // signature: typeParameters? OpenParen parameterList? CloseParen typeAnnotation @Override public boolean visit(MethodDeclaration javaMethodDeclaration) { boolean isInClassContext = stsCurrent instanceof ClassBodyContext; @@ -1338,7 +1373,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { pushCurrent(new ConstructorDeclarationContext(stsCurrent, 0)); stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Constructor)).setParent(stsCurrent); - // STS: OpenParen formalParameterList? CloseParen + // STS: OpenParen parameterList? CloseParen createStsParameterList(javaMethodDeclaration.parameters()); } else { // A regular method (not a constructor). @@ -1403,7 +1438,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // STS: typeParameters: LessThan typeParameterList? MoreThan createStsTypeParameters(javaMethodDeclaration.typeParameters()); - // STS: OpenParen formalParameterList? CloseParen + // STS: OpenParen parameterList? CloseParen createStsParameterList(javaMethodDeclaration.parameters()); // typeAnnotation @@ -1420,9 +1455,9 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // Java tree: // SingleVariableDeclaration: { ExtendedModifier } Type {Annotation} [ ... ] Identifier { Dimension } [ = Expression ] // STS tree for formal parameter: - // formalParameter: Identifier typeAnnotation + // parameter: Identifier typeAnnotation // or - // restFormalParameter: Ellipsis Identifier typeAnnotation + // variadicParameter: Ellipsis Identifier typeAnnotation @Override public boolean visit(SingleVariableDeclaration javaSingleVariableDeclaration) { if (javaSingleVariableDeclaration.isVarargs()) { @@ -1438,18 +1473,50 @@ public class JavaTransformer extends ASTVisitor implements Transformer { stsCurrent.addChild(NodeBuilder.terminalIdentifier(javaSingleVariableDeclaration.getName())); + // Parameter type pushCurrent(new TypeAnnotationContext(stsCurrent, 0)); javaSingleVariableDeclaration.getType().accept(this); + + int extraDims = javaSingleVariableDeclaration.getExtraDimensions(); + if (extraDims > 0) NodeBuilder.addExtraDimensions(stsCurrent, extraDims); + popCurrent(); // TypeAnnotationContext - // TODO: { Dimention } - //javaSingleVariableDeclaration. // TODO: [= Expression ] // Expression javaExpression = javaSingleVariableDeclaration.getInitializer(); // if (javaExpression != null) { // } - popCurrent(); // RestFormalParameterContext | FormalParameterContext + popCurrent(); // ParameterContext | VariadicParameterContext + + ++countDeclTransformed; + return false; + } + + // Java tree: + // VariableDeclarationFragment: + // Identifier { Dimension } [ = Expression ] + // STS tree for formal parameter: + // parameter: + // Identifier typeAnnotation + @Override + public boolean visit(VariableDeclarationFragment javaVariableDeclarationFragment) { + // Here, VariableDeclarationFragment node represents lambda's parameter + // with omitted parameter type. + + pushCurrent(new ParameterContext(stsCurrent, 0)); + + stsCurrent.addChild(NodeBuilder.terminalIdentifier(javaVariableDeclarationFragment.getName())); + + pushCurrent(new TypeAnnotationContext(stsCurrent, 0)); + IVariableBinding variableBinding = javaVariableDeclarationFragment.resolveBinding(); + translateType(variableBinding.getType()); + popCurrent(); // TypeAnnotationContext + + // Note: no need to process the "{ Dimension }" part, as the extra dimensions + // of the declaration are covered by "translateType(ITypeBinding)" call. + + popCurrent(); // ParameterContext ++countDeclTransformed; return false; @@ -3179,16 +3246,53 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // Identifier -> Body // ( [ Identifier { , Identifier } ] ) -> Body // ( [ FormalParameter { , FormalParameter } ] ) -> Body + // Body: + // Expression + // Block // STS tree: // singleExpression: - // : OpenParen parameterList? CloseParen typeAnnotation Arrow lambdaBody # LambdaExpression + // OpenParen parameterList? CloseParen typeAnnotation Arrow lambdaBody # LambdaExpression + // lambdaBody + // : singleExpression + // | block @Override public boolean visit(LambdaExpression javaLambdaExpr) { - // 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(javaLambdaExpr)).setParent(stsCurrent); + pushCurrent(new LambdaExpressionContext(pushSingleExpression())); + + createStsParameterList(javaLambdaExpr.parameters()); + + IMethodBinding lambdaMethod = javaLambdaExpr.resolveMethodBinding(); + + pushCurrent(new TypeAnnotationContext(stsCurrent, 0)); + translateType(lambdaMethod.getReturnType()); + popCurrent(); // TypeAnnotationContext + + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Arrow)); + pushCurrent(new LambdaBodyContext(stsCurrent, 0)); + ASTNode javaBody = javaLambdaExpr.getBody(); + + if (javaBody instanceof Expression) { + javaBody.accept(this); + } else { + // Better this than calling javaBody.accept(this) here + // as visit(Block) will call pushStatement() which will + // add StatementContext node which isn't needed here. + pushCurrent(new BlockContext(stsCurrent, 0)); + + Block javaBlock = (Block) javaBody; + List javaBlockStmts = javaBlock.statements(); + for (Statement javaStmt : javaBlockStmts) { + javaStmt.accept(this); + } + + popCurrent(); // BlockContext + } + popCurrent(); // LambdaBodyContext + + popSingleExpression(); // LambdaExpressionContext + + ++countExprTransformed; return false; } diff --git a/migrator/src/com/ohos/migrator/staticTS/NodeBuilder.java b/migrator/src/com/ohos/migrator/staticTS/NodeBuilder.java index 1a3ea08f0e162113268fdad24de0a56301235709..ec7daf0ac9746f82277f64f4eb073ba091dbeaa0 100644 --- a/migrator/src/com/ohos/migrator/staticTS/NodeBuilder.java +++ b/migrator/src/com/ohos/migrator/staticTS/NodeBuilder.java @@ -421,4 +421,24 @@ public class NodeBuilder { return stsStatement; } + + public static ShiftOperatorContext shiftOperator(InfixExpression.Operator javaOp) { + ShiftOperatorContext stsShiftOp = new ShiftOperatorContext(null, 0); + + if (javaOp == InfixExpression.Operator.LEFT_SHIFT) { + stsShiftOp.addChild(NodeBuilder.terminalNode(StaticTSParser.LessThan)); + stsShiftOp.addChild(NodeBuilder.terminalNode(StaticTSParser.LessThan)); + } + else if (javaOp == InfixExpression.Operator.RIGHT_SHIFT_SIGNED) { + stsShiftOp.addChild(NodeBuilder.terminalNode(StaticTSParser.MoreThan)); + stsShiftOp.addChild(NodeBuilder.terminalNode(StaticTSParser.MoreThan)); + } + else if (javaOp == InfixExpression.Operator.RIGHT_SHIFT_UNSIGNED) { + stsShiftOp.addChild(NodeBuilder.terminalNode(StaticTSParser.MoreThan)); + stsShiftOp.addChild(NodeBuilder.terminalNode(StaticTSParser.MoreThan)); + stsShiftOp.addChild(NodeBuilder.terminalNode(StaticTSParser.MoreThan)); + } + + return stsShiftOp; + } } diff --git a/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSLexer.g4 b/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSLexer.g4 index b0ba23ccce21fcc1f05f183e268848d70dc29ae6..f0b60c9fa0fc60d812dcf85e25f98c6a43b16a2c 100644 --- a/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSLexer.g4 +++ b/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSLexer.g4 @@ -46,9 +46,6 @@ Not: '!'; Multiply: '*'; Divide: '/'; Modulus: '%'; -RightShiftArithmetic: '>>'; -LeftShiftArithmetic: '<<'; -RightShiftLogical: '>>>'; LessThan: '<'; MoreThan: '>'; LessThanEquals: '<='; diff --git a/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSParser.g4 b/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSParser.g4 index 30cfd3fdcaa7c9cfb83ad8ea948dea5117716901..de2cf22eef243e5b09d0c49032cf827248f1791c 100644 --- a/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSParser.g4 +++ b/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSParser.g4 @@ -426,7 +426,7 @@ singleExpression | Not singleExpression # NotExpression | singleExpression (Multiply | Divide | Modulus) singleExpression # MultiplicativeExpression | singleExpression (Plus | Minus) singleExpression # AdditiveExpression - | singleExpression (LeftShiftArithmetic | RightShiftArithmetic | RightShiftLogical) singleExpression # BitShiftExpression + | singleExpression shiftOperator singleExpression # BitShiftExpression | singleExpression (LessThan | MoreThan | LessThanEquals | GreaterThanEquals) singleExpression # RelationalExpression | singleExpression Instanceof primaryType # InstanceofExpression | singleExpression (Equals | NotEquals) singleExpression # EqualityExpression @@ -448,6 +448,12 @@ singleExpression | singleExpression As (intersectionType | primaryType) # CastExpression ; +shiftOperator + : first=LessThan second=LessThan {$first.index + 1 == $second.index}? + | first=MoreThan second=MoreThan {$first.index + 1 == $second.index}? + | first=MoreThan second=MoreThan third=MoreThan {$first.index + 1 == $second.index && $second.index + 1 == $third.index}? + ; + lambdaBody : singleExpression | block diff --git a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java index f4c5e19a7e441db8ca4569bbd02861980bc0e9f4..dcf7ceed6ea0f6597c71a9776fdf01712c7aa1c1 100644 --- a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java +++ b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java @@ -1703,10 +1703,10 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { return null; } - // | singleExpression ('<<' | '>>' | '>>>') singleExpression # BitShiftExpression + // | singleExpression ('<' '<' | '>' '>' | '>' '>' '>') singleExpression # BitShiftExpression @Override public Void visitBitShiftExpression(BitShiftExpressionContext stsBitShiftExpression) { - String op = stsBitShiftExpression.getChild(1).getText(); + String op = stsBitShiftExpression.shiftOperator().getText(); binaryOperatorWrite(op, stsBitShiftExpression.singleExpression()); return null; } @@ -1807,7 +1807,10 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { @Override public Void visitLambdaExpression(LambdaExpressionContext stsLambdaExpression) { sb.append('('); - visitParameterList(stsLambdaExpression.parameterList()); + ParameterListContext stsParameterList = stsLambdaExpression.parameterList(); + if (stsParameterList != null) { + visitParameterList(stsParameterList); + } sb.append(')'); visitTypeAnnotation(stsLambdaExpression.typeAnnotation()); diff --git a/migrator/test/java/array_creation.java.sts b/migrator/test/java/array_creation.java.sts index 7a386f7a000b7b747853ca4a125f1eb9837aeb7d..6267de61834d43927a3ead28edaa43d6c54b93c7 100644 --- a/migrator/test/java/array_creation.java.sts +++ b/migrator/test/java/array_creation.java.sts @@ -18,7 +18,7 @@ package com.ohos.migrator.test.java; export open class array_creation { private const b1 : byte[] = new byte[8]; private const c1 : char[] = ['a', 'b', 'c']; - private foo(c2 : char): void { + private foo(c2 : char[]): void { } public open bar(): void { foo(['g', 'k', 'h']); diff --git a/migrator/test/java/inferred_types.java b/migrator/test/java/inferred_types.java new file mode 100644 index 0000000000000000000000000000000000000000..89e76bdea492aa01a59e0a55f44045476561f645 --- /dev/null +++ b/migrator/test/java/inferred_types.java @@ -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.tests.java; + +import java.util.List; + +class Main { + // Java language allows to omit return/parameter types for lambda expression. + // These types are then inferred by the compiler. + // This test covers all kinds of types that can be inferred by compiler + + class C {} + enum E {} + interface IPrimitiveTypes { void M(int i, boolean b, char c); } // Void, primitive types + interface IClassTypes { IPrimitiveTypes M(C c, String s, E e); } // Class, Interface, Enum + interface IArrays { int[] M(String[] sArray, IPrimitiveTypes[][] iArray, E[] eArray[]); } // Arrays + interface IParametrizedTypes { List M(List list, List listOfArrays, List> listOfLists[]); } // Parametrized type, Raw type + interface IWildcards { List M(List listExtends, List listSuper); } // Wildcards + interface IGeneric { T M(List list); } // Generic interface (parametrized types, type variables, wildcards, etc.) + + static class OuterClass { + class InnerClass { + void M(T t) { + IGeneric l = (list) -> list.get(0); // Generic functional interface parametrized with type variable + } + } + } + interface IInnerOuter { + int M(OuterClass>.InnerClass inner); // Nested type (with parametrized type) + } + + void Test() { + IPrimitiveTypes l1 = (i, b, c) -> { }; // Void, primitive types + IClassTypes l2 = (c, s, e) -> l1; // Class, Interface, Enum + IArrays l3 = (sArray, iArray, eArray) -> new int[5]; // Arrays + IParametrizedTypes l4 = (list, listOfArrays, ListOfLists) -> list; // Parametrized type, Raw type + IWildcards l5 = (listExtends, listSuper) -> listExtends; // Wildcards + + // Generic functional interface + IGeneric l6 = (list) -> list.get(0); // parametrized with explicit type + IGeneric l7 = (list) -> list.get(0); // parametrized with wildcard + IGeneric l8 = (list) -> list.get(0); // parametrized with wildcard with bound + IGeneric l9 = (list) -> list.get(0); // parametrized with wildcard with bound + + IInnerOuter l10 = (inner) -> 0; // Nested type (with parametrized type) + } +} \ No newline at end of file diff --git a/migrator/test/java/inferred_types.java.sts b/migrator/test/java/inferred_types.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..eea79cb1dbbfe7c09f42e395f260997c8dcb9403 --- /dev/null +++ b/migrator/test/java/inferred_types.java.sts @@ -0,0 +1,90 @@ +/* + * 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.tests.java; + +import java.util.List; +open class Main { + open class C { + } + + static class E extends Enum { + public static values(): E[] { + return []; + } + public static valueOf(name : String): E { + for (let value : E of values()){ + if (name == value.toString()) return value; + } + return null; + } + private constructor(name : String, ordinal : int) { + super(name, ordinal); + } + + } + + interface IPrimitiveTypes { + M(i : int, b : boolean, c : char): void ; + } + + interface IClassTypes { + M(c : C, s : String, e : E): IPrimitiveTypes ; + } + + interface IArrays { + M(sArray : String[], iArray : IPrimitiveTypes[][], eArray : E[][]): int[] ; + } + + interface IParametrizedTypes { + M(list : List, listOfArrays : List, listOfLists : List>[]): List ; + } + + interface IWildcards { + M(listExtends : List, listSuper : List): List ; + } + + interface IGeneric { + M(list : List): T ; + } + + static open class OuterClass { + open class InnerClass { + open M(t : T): void { + let l : IGeneric = (list : java.util.List): T =>list.get(0); + } + } + + } + + interface IInnerOuter { + M(inner : OuterClass>.InnerClass): int ; + } + + open Test(): void { + let l1 : IPrimitiveTypes = (i : int, b : boolean, c : char): void => { + } +; + let l2 : IClassTypes = (c : com.ohos.migrator.tests.java.Main.C, s : java.lang.String, e : com.ohos.migrator.tests.java.Main.E): com.ohos.migrator.tests.java.Main.IPrimitiveTypes =>l1; + let l3 : IArrays = (sArray : java.lang.String[], iArray : com.ohos.migrator.tests.java.Main.IPrimitiveTypes[][], eArray : com.ohos.migrator.tests.java.Main.E[][]): int[] =>new int[5]; + let l4 : IParametrizedTypes = (list : java.util.List, listOfArrays : java.util.List, ListOfLists : java.util.List>[]): java.util.List =>list; + let l5 : IWildcards = (listExtends : java.util.List, listSuper : java.util.List): java.util.List =>listExtends; + let l6 : IGeneric = (list : java.util.List): java.lang.Number =>list.get(0); + let l7 : IGeneric = (list : java.util.List): java.lang.Object =>list.get(0); + let l8 : IGeneric = (list : java.util.List): java.lang.Number =>list.get(0); + let l9 : IGeneric = (list : java.util.List): java.lang.Number =>list.get(0); + let l10 : IInnerOuter = (inner : com.ohos.migrator.tests.java.Main.OuterClass>.InnerClass): int =>0; + } +} + \ No newline at end of file diff --git a/migrator/test/java/lambda_expr.java b/migrator/test/java/lambda_expr.java new file mode 100644 index 0000000000000000000000000000000000000000..d24e1a349329b7c5ef396544cb8187b910b2af13 --- /dev/null +++ b/migrator/test/java/lambda_expr.java @@ -0,0 +1,49 @@ +/* + * 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.tests.java; + +class Main { + + interface I1 { void M(); } + interface I2 { int M(boolean b); } + interface I3 { int M(int i, String s); } + + void Test() { + I1 i1 = () -> { }; + i1.M(); + + I2 i2 = x -> !x ? 1 : 2; + int i = i2.M(true); + + I2 i2_2 = getLambda(); + i2_2.M(false); + + PassLambdaByParam((x1, x2) -> x2.contains("x") ? x1 : 0); + } + + I2 getLambda() { + return (boolean b) -> { + if (b) + return 10; + else + return 20; + }; + } + + int PassLambdaByParam(I3 i3) { + return i3.M(10, "Text"); + } +} \ No newline at end of file diff --git a/migrator/test/java/lambda_expr.java.sts b/migrator/test/java/lambda_expr.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..604fe6764e30be328bca51d48133c204073e8eef --- /dev/null +++ b/migrator/test/java/lambda_expr.java.sts @@ -0,0 +1,52 @@ +/* + * 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.tests.java; + +open class Main { + interface I1 { + M(): void ; + } + + interface I2 { + M(b : boolean): int ; + } + + interface I3 { + M(i : int, s : String): int ; + } + + open Test(): void { + let i1 : I1 = (): void => { + } +; + i1.M(); + let i2 : I2 = (x : boolean): int =>!x ? 1 : 2; + let i : int = i2.M(true); + let i2_2 : I2 = getLambda(); + i2_2.M(false); + PassLambdaByParam((x1 : int, x2 : java.lang.String): int =>x2.contains("x") ? x1 : 0); + } + open getLambda(): I2 { + return (b : boolean): int => { + if (b) return 10; + else return 20; + } +; + } + open PassLambdaByParam(i3 : I3): int { + return i3.M(10, "Text"); + } +} + \ No newline at end of file