From 30893312b14a9560337fca5d322023e8a85b4c2b Mon Sep 17 00:00:00 2001 From: Mikhail Velikanov Date: Mon, 19 Sep 2022 14:51:47 +0300 Subject: [PATCH] Fixes for multiple exceptions observed while translating JCK benchmark. Change-Id: Idd984b449f068519358280a434062f9a235aa4c9 Signed-off-by: Mikhail Velikanov --- .../com/ohos/migrator/AbstractTranspiler.java | 2 +- .../src/com/ohos/migrator/TestRunner.java | 2 +- .../ohos/migrator/java/JavaTransformer.java | 156 ++++++++++++++++-- .../staticTS/writer/StaticTSWriter.java | 58 ++++--- .../test/java/enum_with_class_behavior.java | 8 +- .../java/enum_with_class_behavior.java.sts | 4 + migrator/test/java/for_statement.java | 6 +- .../test/java/npe_in_ctor_declaration.java | 20 +++ .../java/npe_in_ctor_declaration.java.sts | 22 +++ .../test/java/npe_in_method_declaration.java | 23 +++ .../java/npe_in_method_declaration.java.sts | 25 +++ .../test/java/npe_in_method_reference.java | 28 ++++ .../java/npe_in_method_reference.java.sts | 29 ++++ .../java/npe_in_type_parameter_bound.java | 19 +++ .../java/npe_in_type_parameter_bound.java.sts | 19 +++ migrator/test/java/npe_in_wildcard_bound.java | 20 +++ .../test/java/npe_in_wildcard_bound.java.sts | 22 +++ migrator/test/java/test_interface.java | 6 +- migrator/test/java/test_interface.java.sts | 6 + 19 files changed, 428 insertions(+), 47 deletions(-) create mode 100644 migrator/test/java/npe_in_ctor_declaration.java create mode 100644 migrator/test/java/npe_in_ctor_declaration.java.sts create mode 100644 migrator/test/java/npe_in_method_declaration.java create mode 100644 migrator/test/java/npe_in_method_declaration.java.sts create mode 100644 migrator/test/java/npe_in_method_reference.java create mode 100644 migrator/test/java/npe_in_method_reference.java.sts create mode 100644 migrator/test/java/npe_in_type_parameter_bound.java create mode 100644 migrator/test/java/npe_in_type_parameter_bound.java.sts create mode 100644 migrator/test/java/npe_in_wildcard_bound.java create mode 100644 migrator/test/java/npe_in_wildcard_bound.java.sts diff --git a/migrator/src/com/ohos/migrator/AbstractTranspiler.java b/migrator/src/com/ohos/migrator/AbstractTranspiler.java index 0dc971cd2..2d65f7b24 100644 --- a/migrator/src/com/ohos/migrator/AbstractTranspiler.java +++ b/migrator/src/com/ohos/migrator/AbstractTranspiler.java @@ -64,7 +64,7 @@ public abstract class AbstractTranspiler implements Transpiler { } catch (Exception e) { StringBuilder sb = new StringBuilder(e.getClass().getName()); - sb.append(" at:\n"); + sb.append(" while transpiling " + f.getPath() + " at:\n"); for (StackTraceElement ste : e.getStackTrace()) sb.append(ste.toString()).append("\n"); diff --git a/migrator/src/com/ohos/migrator/TestRunner.java b/migrator/src/com/ohos/migrator/TestRunner.java index 2d0af0e7d..69e59aad6 100644 --- a/migrator/src/com/ohos/migrator/TestRunner.java +++ b/migrator/src/com/ohos/migrator/TestRunner.java @@ -63,7 +63,7 @@ public class TestRunner { String testFilePath = new File(testDir, testFile).getPath(); System.out.println("Running test " + testFile); - String[] mainArgs = new String[] {"-verbose", "-s", "-o", testResultDir.getPath(), testFilePath}; + String[] mainArgs = new String[] {"-verbose", "-o", testResultDir.getPath(), testFilePath}; Main.runTests(mainArgs); File resultFile = new File(testResultDir, testFile + Main.STS_EXT); diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index d3e79c671..fe5c73d17 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTransformer.java +++ b/migrator/src/com/ohos/migrator/java/JavaTransformer.java @@ -652,7 +652,17 @@ public class JavaTransformer extends ASTVisitor implements Transformer { if (isIntersectionTypeBound) pushCurrent(new IntersectionTypeContext(stsCurrent, 0)); for (Type javaTypeBound : javaTypeBounds) { - javaTypeBound.accept(this); + if (isTypeReference(javaTypeBound)) { + javaTypeBound.accept(this); + } + else { + // Only type references are allowed as type parameter bounds + // (or as intersection type components) in STS. + // Warn and emit __UnknownType__ with original source code as comment. + reportError("Invalid type " + javaTypeBound.toString() + " in type parameter bound", javaTypeBound); + stsCurrent.addChild(NodeBuilder.typeReference("__UnknownType__")).setParent(stsCurrent); + stsCurrent.addChild(NodeBuilder.multiLineComment("/* " + javaTypeBound.toString() + " */")); + } } if (isIntersectionTypeBound) popCurrent(); // IntersectionTypeContext @@ -663,6 +673,10 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return false; } + private boolean isTypeReference(Type javaType) { + return javaType.isSimpleType() || javaType.isQualifiedType() || + javaType.isNameQualifiedType() || javaType.isParameterizedType(); + } private ParserRuleContext createDeclarationOrMemberContextWithAccessModifier(int javaMods) { boolean isInClassContext = stsCurrent instanceof ClassBodyContext; boolean isInInterfaceContext = stsCurrent instanceof InterfaceBodyContext; @@ -1024,7 +1038,17 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // Add corresponding keyword and bounding type int stsTerminalType = javaWildcardType.isUpperBound() ? StaticTSParser.Extends : StaticTSParser.Super; stsCurrent.addChild(NodeBuilder.terminalNode(stsTerminalType)); - javaBound.accept(this); + + if (isTypeReference(javaBound)) { + javaBound.accept(this); + } + else { + // Only type references are allowed as wildcard bounds in STS. + // Warn and emit __UnknownType__ with original source code as a comment. + reportError("Invalid type " + javaBound.toString() + " in wildcard type bound", javaBound); + stsCurrent.addChild(NodeBuilder.typeReference("__UnknownType__")).setParent(stsCurrent); + stsCurrent.addChild(NodeBuilder.multiLineComment("/* " + javaBound.toString() + " */")); + } popCurrent(); // WildcardBoundContext } @@ -1465,7 +1489,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // STS Tree: // typeArguments: LessThan typeArgumentList? MoreThan // typeArgumentList: typeArgument (Comma typeArgument)* - // typeArgument: typeReference | arrayType + // typeArgument: typeReference | arrayType | wildcardType private void translateTypeArguments(List javaTypeArgs) { if (javaTypeArgs != null && !javaTypeArgs.isEmpty()) { pushCurrent(new TypeArgumentsContext(stsCurrent, 0)); @@ -1544,6 +1568,22 @@ public class JavaTransformer extends ASTVisitor implements Transformer { boolean isInClassContext = stsCurrent instanceof ClassBodyContext; assert(isInClassContext || (stsCurrent instanceof InterfaceBodyContext)); + // Sanity check: Constructors cannot be body-less and methods cannot be without return type + Block javaBlock = javaMethodDeclaration.getBody(); + if (javaMethodDeclaration.isConstructor() && javaBlock == null) { + // Warn and emit a comment with original source code of the constructor. + reportError("Invalid constructor declaration without a body", javaMethodDeclaration); + stsCurrent.addChild(NodeBuilder.multiLineComment("/* Untranslated constructor declaration:\n" + + javaMethodDeclaration.toString() + "\n*/\n")); + return false; + } + else if (!javaMethodDeclaration.isConstructor() && javaMethodDeclaration.getReturnType2() == null) { + reportError("Invalid method declaration without return type", javaMethodDeclaration); + stsCurrent.addChild(NodeBuilder.multiLineComment("/* Untranslated method declaration:\n" + + javaMethodDeclaration.toString() + "\n*/\n")); + return false; + } + // Get current enclosing context - we'll need it later if // current method is synchronized (see below). Also store // modifiers of the current method for the same reason. @@ -1554,8 +1594,6 @@ public class JavaTransformer extends ASTVisitor implements Transformer { pushCurrent(createDeclarationOrMemberContextWithAccessModifier(javaMods)); - Block javaBlock = javaMethodDeclaration.getBody(); - if (javaMethodDeclaration.isConstructor()) { pushCurrent(new ConstructorDeclarationContext(stsCurrent, 0)); stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Constructor)).setParent(stsCurrent); @@ -1581,7 +1619,6 @@ public class JavaTransformer extends ASTVisitor implements Transformer { } if (javaBlock == null) { // Abstract method. - assert (!javaMethodDeclaration.isConstructor()); stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.SemiColon)); } else { if (javaMethodDeclaration.isConstructor()) { @@ -1930,7 +1967,11 @@ public class JavaTransformer extends ASTVisitor implements Transformer { ExpressionSequenceContext stsExprSeq = stsEnumCtorCallArgs.expressionSequence(); if (stsExprSeq == null) { // Create expression sequence node, if necessary + // ExpressionSequenceContext ctor doesn't initialize children field + // which we use below, so initialize it explicitly. stsExprSeq = new ExpressionSequenceContext(stsEnumCtorCallArgs, 0); + stsExprSeq.children = new ArrayList<>(); + stsEnumCtorCallArgs.addChild(stsExprSeq).setParent(stsEnumCtorCallArgs); } @@ -2288,7 +2329,16 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // variableDeclaration (Comma variableDeclaration)* @Override public boolean visit(VariableDeclarationExpression javaVarDeclExpr) { - createAndFillVarOrConstDeclarationList(javaVarDeclExpr.getModifiers(), javaVarDeclExpr.fragments(), javaVarDeclExpr.getType()); + // Drop 'final' modifier if we're inside for statement, as + // STS doesn't allow constant declarations in that context. + int javaMods = javaVarDeclExpr.getModifiers(); + if (javaVarDeclExpr.getParent().getNodeType() == ASTNode.FOR_STATEMENT) { + if (Modifier.isFinal(javaMods)) { + javaMods = javaMods & ~Modifier.FINAL; + } + } + + createAndFillVarOrConstDeclarationList(javaMods, javaVarDeclExpr.fragments(), javaVarDeclExpr.getType()); exprTransformed.add(javaVarDeclExpr); return false; @@ -2382,14 +2432,21 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // static block @Override public boolean visit(Initializer javaInitializer) { - assert(stsCurrent instanceof ClassBodyContext); - boolean isStatic = Modifier.isStatic(javaInitializer.getModifiers()); + + // Sanity check: Initializers are allowed only in class body context. + if (stsCurrent.getRuleIndex() != StaticTSParser.RULE_classBody) { + // Warn and emit a comment with original source code of initializer + reportError("Invalid context for initializer", javaInitializer); + stsCurrent.addChild(NodeBuilder.multiLineComment("/* Untranslated initializer:\n" + + javaInitializer.toString() + "\n*/\n")); + return false; + } + if (isStatic) { // StaticTS permits only one static initializer per class declaration. // Thus, we gather statements from all Java's static initializers // into one single static initializer block in StaticTS code. - ClassBodyContext stsClassBody = (ClassBodyContext) stsCurrent; if (stsClassBody.clinit != null) { // Note: Class initializer is already added to parent context's children, @@ -3131,7 +3188,16 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // | Try singleExpression #TryExpression @Override public boolean visit(MethodInvocation javaMethodInvocation) { - IMethodBinding javaMethodBinding = javaMethodInvocation.resolveMethodBinding(); + // resolveMethodBinding() can throw exceptions, + // so let's catch them to make sure we proceed. + IMethodBinding javaMethodBinding; + try { + javaMethodBinding = javaMethodInvocation.resolveMethodBinding(); + } + catch (Exception e) { + javaMethodBinding = null; + } + boolean isThrowingCall = false; if (javaMethodBinding != null) { isThrowingCall = javaMethodBinding.getExceptionTypes().length > 0; @@ -3575,7 +3641,15 @@ public class JavaTransformer extends ASTVisitor implements Transformer { createStsParameterList(javaLambdaExpr.parameters()); - IMethodBinding lambdaMethod = javaLambdaExpr.resolveMethodBinding(); + // resolveMethodBinding() can throw exceptions, + // so let's catch them to make sure we proceed. + IMethodBinding lambdaMethod; + try { + lambdaMethod = javaLambdaExpr.resolveMethodBinding(); + } + catch (Exception e) { + lambdaMethod = null; + } if (lambdaMethod != null) { translateType(lambdaMethod.getReturnType(), javaLambdaExpr); @@ -3723,7 +3797,15 @@ public class JavaTransformer extends ASTVisitor implements Transformer { } private void translateClassCreationReference(CreationReference javaCreationRef) { - IMethodBinding javaCtorBinding = javaCreationRef.resolveMethodBinding(); + // resolveMethodBinding() can throw exceptions, + // so let's catch them to make sure we proceed. + IMethodBinding javaCtorBinding; + try { + javaCtorBinding = javaCreationRef.resolveMethodBinding(); + } + catch (Exception e) { + javaCtorBinding = null; + } // Exclude recovered bindings as well here, as information we need // to extract from it below might not be available after recovery. @@ -3792,7 +3874,15 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // singleExpression: singleExpression typeArguments? arguments # CallExpression @Override public boolean visit(SuperMethodReference javaSuperMethodRef) { - IMethodBinding javaMethodBinding = javaSuperMethodRef.resolveMethodBinding(); + // resolveMethodBinding() can throw exceptions, + // so let's catch them to make sure we proceed. + IMethodBinding javaMethodBinding; + try { + javaMethodBinding = javaSuperMethodRef.resolveMethodBinding(); + } + catch (Exception e) { + javaMethodBinding = null; + } // Exclude recovered bindings as well here, as information we need // to extract from it below might not be available after recovery. @@ -3866,7 +3956,15 @@ public class JavaTransformer extends ASTVisitor implements Transformer { Type javaType = javaTypeMethodRef.getType(); boolean usingFunctionalInterfaceMethod = false; - IMethodBinding javaMethodBinding = javaTypeMethodRef.resolveMethodBinding(); + // resolveMethodBinding() can throw exceptions, + // so let's catch them to make sure we proceed. + IMethodBinding javaMethodBinding; + try { + javaMethodBinding = javaTypeMethodRef.resolveMethodBinding(); + } + catch (Exception e) { + javaMethodBinding = null; + } // Exclude recovered bindings as well here, as information we need // to extract from it below might not be available after recovery. @@ -3914,7 +4012,15 @@ public class JavaTransformer extends ASTVisitor implements Transformer { // singleExpression: singleExpression typeArguments? arguments # CallExpression @Override public boolean visit(ExpressionMethodReference javaExprMethodRef) { - IMethodBinding javaMethodBinding = javaExprMethodRef.resolveMethodBinding(); + // resolveMethodBinding() can throw exceptions, + // so let's catch them to make sure we proceed. + IMethodBinding javaMethodBinding; + try { + javaMethodBinding = javaExprMethodRef.resolveMethodBinding(); + } + catch (Exception e) { + javaMethodBinding = null; + } Expression javaExpr = javaExprMethodRef.getExpression(); ITypeBinding exprType = javaExpr.resolveTypeBinding(); @@ -3969,9 +4075,17 @@ public class JavaTransformer extends ASTVisitor implements Transformer { private void translateClassMethodReference(MethodReference javaMethodRef, IMethodBinding javaMethodBinding, SimpleName javaName, ASTNode javaTypeOrExpression, boolean isTypeMethodRef, boolean usingFunctionalInterfaceMethod) { - boolean isThrowingCall = javaMethodBinding.getExceptionTypes().length > 0; + // Sanity check: References to static methods via parameterized types are not allowed in Java. + boolean isStatic = (javaMethodBinding.getModifiers() & Modifier.STATIC) != 0; + if (javaTypeOrExpression.getNodeType() == ASTNode.PARAMETERIZED_TYPE && isStatic) { + // Warn and emit __untranslatedExpression call + reportError("Invalid reference to a static method of parameterized type", javaMethodRef); + stsCurrent.addChild(NodeBuilder.untranslatedExpression(javaMethodRef)); + return; + } // Add TryExpressionContext if this is a throwing call. + boolean isThrowingCall = javaMethodBinding.getExceptionTypes().length > 0; if (isThrowingCall) { pushCurrent(new TryExpressionContext(pushSingleExpression())); stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Try)); @@ -3979,7 +4093,6 @@ public class JavaTransformer extends ASTVisitor implements Transformer { pushCurrent(new CallExpressionContext(pushSingleExpression())); - boolean isStatic = (javaMethodBinding.getModifiers() & Modifier.STATIC) != 0; boolean needInstanceParam = false; int lambdaParamIdx = 1; @@ -4502,6 +4615,13 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return stsCallExpr; } + // NOTE: We don't translate annotations yet, so + // no need to bother with annotation types. + @Override + public boolean visit(AnnotationTypeDeclaration node) { + return false; + } + // NOTE: The following AST nodes should not appear in Java 9 sources // but since they are supported by the version of Eclipse JDT we use, // let's report in case we see them. diff --git a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java index 87cab247b..3fba2b340 100644 --- a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java +++ b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java @@ -22,6 +22,7 @@ import com.ohos.migrator.staticTS.parser.StaticTSParserBaseVisitor; import org.antlr.v4.runtime.CharStream; import org.antlr.v4.runtime.CharStreams; import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.TerminalNode; @@ -160,6 +161,10 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { TypeReferenceContext stsType = stsConstraint.typeReference(); if (stsType != null) { visitTypeReference(stsType); + + // We may have added a comment here if type reference was invalid. + // See JavaTransformer.visit(TypeParameter) for details. + visitComment(stsConstraint, 2); } else { IntersectionTypeContext stsIntersectionType = stsConstraint.intersectionType(); @@ -247,18 +252,29 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { assert(stsTypeRef != null); visitTypeReference(stsTypeRef); + // We may have added a comment here if type reference was invalid. + // See JavaTransformer.visit(WildcardType) for details. + visitComment(stsWildcardBound, 2); + return null; } @Override public Void visitIntersectionType(IntersectionTypeContext stsIntersectionType) { - List stsTypeRefs = stsIntersectionType.typeReference(); - sb.append('('); - for (int i = 0; i < stsTypeRefs.size(); ++i) { - if (i > 0) sb.append(" & "); - visitTypeReference(stsTypeRefs.get(i)); + for (int i = 0; i < stsIntersectionType.getChildCount(); ++i) { + // There may be comments among type references here, + // each following a type reference that was invalid. + // See JavaTransformer.visit(TypeParameter) for details. + ParseTree stsChildNode = stsIntersectionType.getChild(i); + if (stsChildNode instanceof TypeReferenceContext) { + if (i > 0) sb.append(" & "); + visitTypeReference((TypeReferenceContext) stsChildNode); + } + else if (stsChildNode instanceof TerminalNode) { + visitTerminal((TerminalNode)stsChildNode); + } } sb.append(')'); @@ -357,17 +373,12 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { stsTypeAnnotation.parent.getRuleIndex() != StaticTSParser.RULE_exceptionParameter) sb.append(' '); - if (stsTypeAnnotation.getChildCount() > 1) { - // In case of unresolved or invalid type, we add a comment to TypeAnnotationContext - // node indicating the original type name where we can (the type itself is emitted as - // __UnknownType__, see, e.g., JavaTransformer.createCatchOrRecoverClause method). - // Here we check if that additional child of TypeAnnotationContext node exists, - // and if so, emit it in STS source code. - ParseTree lastChild = stsTypeAnnotation.getChild(stsTypeAnnotation.getChildCount()-1); - if (lastChild instanceof TerminalNode) { - stsTypeAnnotation.getChild(1).accept(this); - } - } + // In case of unresolved or invalid type, we add a comment to TypeAnnotationContext + // node indicating the original type name where we can (the type itself is emitted as + // __UnknownType__, see, e.g., JavaTransformer.createCatchOrRecoverClause method). + // Here we check if that additional child of TypeAnnotationContext node exists, + // and if so, emit it in STS source code. + visitComment(stsTypeAnnotation, 1); return null; } @@ -509,11 +520,7 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { // interfaceBody: interfaceMember+ @Override public Void visitInterfaceBody(InterfaceBodyContext stsInterfaceBody) { - for (InterfaceMemberContext stsInterfaceMember : stsInterfaceBody.interfaceMember()) { - doNeededIndent(); - stsInterfaceMember.accept(this); - } - + visitChildren(stsInterfaceBody); return null; } @@ -1905,4 +1912,13 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { return null; } + + private void visitComment(ParserRuleContext parentNode, int childIndex) { + if (parentNode.getChildCount() > childIndex) { + ParseTree childNode = parentNode.getChild(childIndex); + if (childNode instanceof TerminalNode) { + visitTerminal((TerminalNode)childNode); + } + } + } } diff --git a/migrator/test/java/enum_with_class_behavior.java b/migrator/test/java/enum_with_class_behavior.java index ff6d2406b..b8d4ee743 100644 --- a/migrator/test/java/enum_with_class_behavior.java +++ b/migrator/test/java/enum_with_class_behavior.java @@ -79,13 +79,19 @@ enum Planet { } // Checks addition of name and ordinal parameters - // to explciitly-defined parameter-less enum ctor. + // to explcitly-defined parameter-less enum ctor. private Planet() { mass = 0.; radius = 0.; type = PlanetType.ROCK; } + // Checks addition of name and ordinal arguments + // to explicit parameter-less ctor call + private Planet(double mass) { + this(); + } + // universal gravitational constant (m3 kg-1 s-2) public static final double G = 6.67300E-11; diff --git a/migrator/test/java/enum_with_class_behavior.java.sts b/migrator/test/java/enum_with_class_behavior.java.sts index 2fb6e6d4c..010862bf2 100644 --- a/migrator/test/java/enum_with_class_behavior.java.sts +++ b/migrator/test/java/enum_with_class_behavior.java.sts @@ -108,6 +108,10 @@ class Planet extends Enum { type = PlanetType.ROCK; } + private constructor(name : String, ordinal : int, mass : double) { + this(name, ordinal); + } + public static const G : double = 6.67300E-11; open surfaceGravity(): double { diff --git a/migrator/test/java/for_statement.java b/migrator/test/java/for_statement.java index cab5fb405..29e76949e 100644 --- a/migrator/test/java/for_statement.java +++ b/migrator/test/java/for_statement.java @@ -46,8 +46,8 @@ class ForStatements { } outerLoop: - for (int i = 0; i < 5; i++) { - + for (final int i = 0; i < 5; i++) { + innerLoop: for (int j = 0; j < 5; j++) { if (j == 2) continue innerLoop; @@ -55,4 +55,4 @@ class ForStatements { } } } -} \ No newline at end of file +} diff --git a/migrator/test/java/npe_in_ctor_declaration.java b/migrator/test/java/npe_in_ctor_declaration.java new file mode 100644 index 000000000..8143bf8f1 --- /dev/null +++ b/migrator/test/java/npe_in_ctor_declaration.java @@ -0,0 +1,20 @@ +/* + * 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 npe_in_ctor_declaration { + native npe_in_ctor_declaration(); +} diff --git a/migrator/test/java/npe_in_ctor_declaration.java.sts b/migrator/test/java/npe_in_ctor_declaration.java.sts new file mode 100644 index 000000000..a1e8b232b --- /dev/null +++ b/migrator/test/java/npe_in_ctor_declaration.java.sts @@ -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; + +open class npe_in_ctor_declaration { +/* Untranslated constructor declaration: + native npe_in_ctor_declaration(); +*/ +} diff --git a/migrator/test/java/npe_in_method_declaration.java b/migrator/test/java/npe_in_method_declaration.java new file mode 100644 index 000000000..c9e742f85 --- /dev/null +++ b/migrator/test/java/npe_in_method_declaration.java @@ -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; + +@interface TestID { +} + +class npe_in_method_header { + #TestID() void foo() { } +} diff --git a/migrator/test/java/npe_in_method_declaration.java.sts b/migrator/test/java/npe_in_method_declaration.java.sts new file mode 100644 index 000000000..f317fe41b --- /dev/null +++ b/migrator/test/java/npe_in_method_declaration.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; + +open class npe_in_method_header { +/* Untranslated method declaration: + void TestID(){ + } +*/ + open foo(): void { + } +} diff --git a/migrator/test/java/npe_in_method_reference.java b/migrator/test/java/npe_in_method_reference.java new file mode 100644 index 000000000..4d372c6fb --- /dev/null +++ b/migrator/test/java/npe_in_method_reference.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.tests.java; + +interface Action { + void run(); +} + +class npe_in_method_reference { + static void foo() { } + + void test() { + Action a = npe_in_method_reference::foo; + } +} diff --git a/migrator/test/java/npe_in_method_reference.java.sts b/migrator/test/java/npe_in_method_reference.java.sts new file mode 100644 index 000000000..9f2b9afed --- /dev/null +++ b/migrator/test/java/npe_in_method_reference.java.sts @@ -0,0 +1,29 @@ +/* +* 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; + +interface Action { + run(): void ; +} + +open class npe_in_method_reference { + static foo(): void { + } + + open test(): void { + let a : Action = __untranslated_expression(/* npe_in_method_reference::foo */); + } +} diff --git a/migrator/test/java/npe_in_type_parameter_bound.java b/migrator/test/java/npe_in_type_parameter_bound.java new file mode 100644 index 000000000..3df21b180 --- /dev/null +++ b/migrator/test/java/npe_in_type_parameter_bound.java @@ -0,0 +1,19 @@ +/* + * 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 npe_in_type_parameter_bound { +} diff --git a/migrator/test/java/npe_in_type_parameter_bound.java.sts b/migrator/test/java/npe_in_type_parameter_bound.java.sts new file mode 100644 index 000000000..d69eaf574 --- /dev/null +++ b/migrator/test/java/npe_in_type_parameter_bound.java.sts @@ -0,0 +1,19 @@ +/* + * 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 npe_in_type_parameter_bound { +} diff --git a/migrator/test/java/npe_in_wildcard_bound.java b/migrator/test/java/npe_in_wildcard_bound.java new file mode 100644 index 000000000..cb5e17247 --- /dev/null +++ b/migrator/test/java/npe_in_wildcard_bound.java @@ -0,0 +1,20 @@ +/* + * 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 npe_in_wildcard_bound { + Class test() { return null; } +} diff --git a/migrator/test/java/npe_in_wildcard_bound.java.sts b/migrator/test/java/npe_in_wildcard_bound.java.sts new file mode 100644 index 000000000..bd4353f41 --- /dev/null +++ b/migrator/test/java/npe_in_wildcard_bound.java.sts @@ -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; + +open class npe_in_wildcard_bound { + open test(): Class { + return null; + } +} diff --git a/migrator/test/java/test_interface.java b/migrator/test/java/test_interface.java index 877048937..7cd26d064 100644 --- a/migrator/test/java/test_interface.java +++ b/migrator/test/java/test_interface.java @@ -32,10 +32,12 @@ interface test_interface { } strictfp interface iface_FP { - void foo(float f); + void foo(float f); } interface iface_C extends test_interface, iface_FP { - public void foo(double d); + public void foo(double d); + + { foo(2.5); } } diff --git a/migrator/test/java/test_interface.java.sts b/migrator/test/java/test_interface.java.sts index d90b30942..4027c2fdb 100644 --- a/migrator/test/java/test_interface.java.sts +++ b/migrator/test/java/test_interface.java.sts @@ -33,5 +33,11 @@ interface iface_FP { interface iface_C extends test_interface, iface_FP { foo(d : double): void ; + +/* Untranslated initializer: + { + foo(2.5); + } +*/ } -- Gitee