diff --git a/migrator/src/com/ohos/migrator/AbstractTranspiler.java b/migrator/src/com/ohos/migrator/AbstractTranspiler.java index 196df0c8aad94c6b10e877c6b0ddb4b767c0c075..0dc971cd2e14c4b2cb6439c3dcece226353b9e08 100644 --- a/migrator/src/com/ohos/migrator/AbstractTranspiler.java +++ b/migrator/src/com/ohos/migrator/AbstractTranspiler.java @@ -70,7 +70,7 @@ public abstract class AbstractTranspiler implements Transpiler { sb.append(ste.toString()).append("\n"); errorList.add(new TranspileException(ResultCode.InputError, sb.toString())); - transpileResult = ResultCode.majorValue(ResultCode.InputError, transpileResult); + transpileResult = ResultCode.majorValue(ResultCode.TranspileError, transpileResult); if (Main.isStrictMode()) return transpileResult; } } diff --git a/migrator/src/com/ohos/migrator/Main.java b/migrator/src/com/ohos/migrator/Main.java index 4f8e169543d6e5570cde405c818c7d650c99d220..5141f4c3dff27a4021bb4ccfff78469fd3d62c0e 100644 --- a/migrator/src/com/ohos/migrator/Main.java +++ b/migrator/src/com/ohos/migrator/Main.java @@ -51,7 +51,10 @@ public class Main { } } - if (!runningTests) System.exit(exitCode.value); + if (!runningTests) + System.exit(exitCode.value); + + errorList = new ArrayList<>(); } private static boolean runningTests = false; @@ -245,7 +248,10 @@ public class Main { ResultCode code = stsChecker.transpile(); errorList.addAll(stsChecker.getErrorList()); - if (code == ResultCode.OK) System.out.println("Syntax OK."); + if (code == ResultCode.OK) + System.out.println("Syntax OK."); + else + System.out.println("Bad syntax!"); return code; } } diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index 25cd45f53897cec1b861f439a94220a72b5cdf60..ff53eaf6371712610da9dd37ccb660f90be36302 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTransformer.java +++ b/migrator/src/com/ohos/migrator/java/JavaTransformer.java @@ -410,9 +410,14 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // TypeReferenceContext // QualifiedNameContext // TerminalNode - pushCurrent(new ImplementsClauseContext(stsCurrent, 0)); - stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Implements)); - + if( stsCurrent instanceof InterfaceDeclarationContext) { + pushCurrent(new InterfaceExtendsClauseContext(stsCurrent, 0)); + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Extends)); + } + else { + pushCurrent(new ImplementsClauseContext(stsCurrent, 0)); + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Implements)); + } pushCurrent(new InterfaceTypeListContext(stsCurrent, 0)); for (Type javaSuperInterfaceType : javaSuperInterfaceTypes) { @@ -480,9 +485,11 @@ 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 || isInInterfaceContext) { + if (isInClassContext) { AccessibilityModifierContext stsAccessMod = NodeBuilder.accessibilityModifier(javaMods); - if (stsAccessMod != null) stsMemberContext.addChild(stsAccessMod).setParent(stsMemberContext); + if (stsAccessMod != null) {//stsMemberContext = null; + stsMemberContext.addChild(stsAccessMod).setParent(stsMemberContext); + } } else if ((javaMods & Modifier.PUBLIC) != 0) stsMemberContext.addChild(NodeBuilder.terminalNode(StaticTSParser.Export)); @@ -568,17 +575,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { List javaVarDeclFragments = javaFieldDecl.fragments(); for (VariableDeclarationFragment javaVarDeclFragment : javaVarDeclFragments) { - // Create appropriate member context to put declaration into. - ParserRuleContext stsClassOrInterMember = isInClassContext ? - new ClassMemberContext(stsCurrent, 0) - : new InterfaceMemberContext(stsCurrent, 0); - - pushCurrent(stsClassOrInterMember); - - // Access modifier in STS is a child of ClassMemberContext or InterfaceMemberContext, not declaration itself! - AccessibilityModifierContext stsAccessMod = NodeBuilder.accessibilityModifier(javaMods); - if (stsAccessMod != null) stsCurrent.addChild(stsAccessMod).setParent(stsCurrent); - + pushCurrent(createMemberContextWithAccessModifier(javaMods)); ParserRuleContext stsClassOrInterField = isInClassContext ? new ClassFieldDeclarationContext(stsCurrent, 0) : new InterfaceFieldContext((InterfaceMemberContext)stsCurrent); @@ -1354,19 +1351,8 @@ public class JavaTransformer extends ASTVisitor implements Transformer { public boolean visit(MethodDeclaration javaMethodDeclaration) { boolean isInClassContext = stsCurrent instanceof ClassBodyContext; assert(isInClassContext || (stsCurrent instanceof InterfaceBodyContext)); - - // Create appropriate member context to put declaration into. - ParserRuleContext stsClassOrInterMember = isInClassContext ? - new ClassMemberContext(stsCurrent, 0) - : new InterfaceMemberContext(stsCurrent, 0); - - pushCurrent(stsClassOrInterMember); - - // Access modifier in STS is a child of ClassMemberContext or InterfaceMemberContext, not declaration itself! - int javaMods = javaMethodDeclaration.getModifiers(); - AccessibilityModifierContext stsAccessMod = NodeBuilder.accessibilityModifier(javaMods); - if (stsAccessMod != null) stsCurrent.addChild(stsAccessMod).setParent(stsCurrent); - + + pushCurrent(createMemberContextWithAccessModifier(javaMethodDeclaration.getModifiers())); Block javaBlock = javaMethodDeclaration.getBody(); if (javaMethodDeclaration.isConstructor()) { diff --git a/migrator/src/com/ohos/migrator/staticTS/StaticTSSyntaxChecker.java b/migrator/src/com/ohos/migrator/staticTS/StaticTSSyntaxChecker.java index b3f2949be69494bc072110f43284e29385916de7..997069ef65653247ec3b892ca8319c192cbe1a07 100644 --- a/migrator/src/com/ohos/migrator/staticTS/StaticTSSyntaxChecker.java +++ b/migrator/src/com/ohos/migrator/staticTS/StaticTSSyntaxChecker.java @@ -54,6 +54,9 @@ public class StaticTSSyntaxChecker extends AbstractTranspiler { throw new TranspileException(ResultCode.ParseError, "File " + srcFile.getPath() + " failed syntax check!"); } catch (ParseCancellationException e) { throw new TranspileException(ResultCode.ParseError, "File " + srcFile.getPath() + " failed syntax check!"); - } + } finally { + SyntaxErrorListener.INSTANCE.clearMsgList(); + } + } } diff --git a/migrator/src/com/ohos/migrator/staticTS/SyntaxErrorListener.java b/migrator/src/com/ohos/migrator/staticTS/SyntaxErrorListener.java index 25586362fefb9e1627bf1977eb78db04a5526b3b..7960a0038db45dc6ca7abcf81dd63402a21f431c 100644 --- a/migrator/src/com/ohos/migrator/staticTS/SyntaxErrorListener.java +++ b/migrator/src/com/ohos/migrator/staticTS/SyntaxErrorListener.java @@ -28,9 +28,11 @@ public class SyntaxErrorListener extends BaseErrorListener { List msgList; public List getMsgList() { return msgList; } + public void clearMsgList() { msgList = new ArrayList<>();} + public static final SyntaxErrorListener INSTANCE = new SyntaxErrorListener(); - public SyntaxErrorListener() { msgList = new ArrayList<>();} + public SyntaxErrorListener() { clearMsgList(); } @Override public void syntaxError(Recognizer recognizer, Object offendingsymbol, int line, int charPositionInLine, String msg, RecognitionException e) { diff --git a/migrator/test/java/interface_nested.java b/migrator/test/java/interface_nested.java new file mode 100644 index 0000000000000000000000000000000000000000..ced33cff34dc3bae6d7600890ebbc62feb39951a --- /dev/null +++ b/migrator/test/java/interface_nested.java @@ -0,0 +1,62 @@ +/* + * 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; +import java.io.*; + + +interface Test { + void Outtermethod(); + + public class A { + int i; + } + + abstract interface dummy { + void getI(); + } + + + interface innerInterface { + public int Innermethod(); + } + + public interface inner_A { + double coeff(); + } +} + + +class TestClass implements Test.innerInterface, Test.inner_A { + @Override + public int Innermethod() { + return 37037; + } + + @Override + public double coeff() { + return 3.14; + } + + public interface constants { + public double Pi(); + static double E() { return 2.7828; }; + } + +} + + diff --git a/migrator/test/java/interface_nested.java.sts b/migrator/test/java/interface_nested.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..088e675bede785cf290e528f02588a2e7bf59d95 --- /dev/null +++ b/migrator/test/java/interface_nested.java.sts @@ -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.tests.java; + +import java.util.List; +import java.io.*; +interface Test { + Outtermethod(): void ; + open class A { + i : int ; + } + + interface dummy { + getI(): void ; + } + + interface innerInterface { + Innermethod(): int ; + } + + interface inner_A { + coeff(): double ; + } + +} + +open class TestClass implements Test.innerInterface, Test.inner_A { + public override Innermethod(): int { + return 37037; + } + public override coeff(): double { + return 3.14; + } + public interface constants { + Pi(): double ; + static E(): double { + return 2.7828; + } + } + +} + diff --git a/migrator/test/java/interface_public.java b/migrator/test/java/interface_public.java new file mode 100644 index 0000000000000000000000000000000000000000..57623fccc78bbf6e9133a638cb0093f65cdf4d82 --- /dev/null +++ b/migrator/test/java/interface_public.java @@ -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.tests.java; + +import java.util.List; +import java.io.*; + +public strictfp interface interface_public { + int i = 10; + public static final double pi = 3.1416; + + void foo(); + + static strictfp void foo(int i) {} + + private strictfp void foo(boolean b) {} + + public void foo(String s); + + public static strictfp double Pi () { return pi; } + + default double E() { return 2.71828; } +} + diff --git a/migrator/test/java/interface_public.java.sts b/migrator/test/java/interface_public.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..8e06462c995b1befb9326b40f3b71c3622b9eb21 --- /dev/null +++ b/migrator/test/java/interface_public.java.sts @@ -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.tests.java; + +import java.util.List; +import java.io.*; +export interface interface_public { + i : int = 10; + pi : double = 3.1416; + foo(): void ; + static foo(i : int): void { + } + foo(b : boolean): void { + } + foo(s : String): void ; + static Pi(): double { + return pi; + } + E(): double { + return 2.71828; + } +} + diff --git a/migrator/test/java/test_interface.java b/migrator/test/java/test_interface.java index 279e096c69ceb6c9103df6c782b7fbdf7e31500a..877048937b05cde932c9b3a92f216c946c70aec1 100644 --- a/migrator/test/java/test_interface.java +++ b/migrator/test/java/test_interface.java @@ -21,10 +21,21 @@ import java.io.*; interface test_interface { int i = 10; double pi = 3.1416; + void foo(); static void foo(int i) {} private void foo(boolean b) {} + + public void foo(String s); +} + +strictfp interface iface_FP { + void foo(float f); } + +interface iface_C extends test_interface, iface_FP { + public void foo(double d); +} diff --git a/migrator/test/java/test_interface.java.sts b/migrator/test/java/test_interface.java.sts index 9c0696455c3bea87454a03bb2194aa229fc7b76f..1ad4abae3647c4e8347bc8f057c95354938f73d9 100644 --- a/migrator/test/java/test_interface.java.sts +++ b/migrator/test/java/test_interface.java.sts @@ -22,9 +22,16 @@ interface test_interface { foo(): void ; static foo(i : int): void { } - - private foo(b : boolean): void { + foo(b : boolean): void { } + foo(s : String): void ; +} + +interface iface_FP { + foo(f : float): void ; +} +interface iface_C extends test_interface, iface_FP { + foo(d : double): void ; } diff --git a/migrator/test/java/var_declare.java b/migrator/test/java/var_declare.java index 0dd30ecc692d6cbff6b9be1febac1cf9dd4f1b45..5e0e8cea1dded3a423f167990a0e3b13ed0f77ab 100644 --- a/migrator/test/java/var_declare.java +++ b/migrator/test/java/var_declare.java @@ -25,5 +25,8 @@ class VarDeclareTest { final double e = 2.781828; final float Pi = 3.14f; + //final double phi = 0.14e+01; + //final double g = 9.81e+00; + //final double dd = 4.343234234234234e+00; } }