diff --git a/migrator/src/com/ohos/migrator/Main.java b/migrator/src/com/ohos/migrator/Main.java index 5141f4c3dff27a4021bb4ccfff78469fd3d62c0e..cacf676685a13f98baa06f3ba87f271ff37abeee 100644 --- a/migrator/src/com/ohos/migrator/Main.java +++ b/migrator/src/com/ohos/migrator/Main.java @@ -84,7 +84,7 @@ public class Main { options.addOption(new Option("l","libs",true, "List of libraries separate with commas")); options.addOption(new Option("T","check-sts-syntax",false,"Check syntactical correctness of StaticTS sources")); options.addOption(new Option("R", "conversion-rate", false, "Report conversion rate")); - + options.addOption(new Option("noxrefs", "noxrefs", false, "Don't resolve cross-references in the input source files")); options.addOption(new Option("verbose","verbose",false,"Prints extended diagnostic messages")); options.addOption(new Option("v","version",false,"Version information")); @@ -185,11 +185,12 @@ public class Main { double convRate = 0.; int numLanguages = 0; + boolean noxrefs = cmd.hasOption("noxrefs"); if (!javaSources.isEmpty()) { System.out.println("Transpiling " + javaSources.size() + " Java files."); - JavaTranspiler javaTranspiler = new JavaTranspiler(javaSources, jarLibs, outDir); + JavaTranspiler javaTranspiler = new JavaTranspiler(javaSources, jarLibs, outDir, noxrefs); resultCode = javaTranspiler.transpile(); outFiles.addAll(javaTranspiler.getOutFiles()); errorList.addAll(javaTranspiler.getErrorList()); diff --git a/migrator/src/com/ohos/migrator/java/JavaParser.java b/migrator/src/com/ohos/migrator/java/JavaParser.java index f2cc113e4c05ccc73b7a2b6bf91267d929720c46..214f4c7cb7e1934822b5fe4f5adf5cc10c9baaf1 100644 --- a/migrator/src/com/ohos/migrator/java/JavaParser.java +++ b/migrator/src/com/ohos/migrator/java/JavaParser.java @@ -59,11 +59,12 @@ public class JavaParser { * parser's 'sourcepaths' setting. * @param classpaths List of paths to jar files or directories with '.class' files. */ - public JavaParser(File sourceFile, List sourceFiles, List classpaths) throws IOException { + public JavaParser(File sourceFile, List sourceFiles, List classpaths, boolean noxrefs) throws IOException { this(sourceFile); - // Compute reference source paths once - if (sourcepathEntries == null) + // Compute reference source paths once, unless + // explicitly prohibited by command-line option + if (!noxrefs && sourcepathEntries == null) setSourcepathEntries(sourceFiles); setClasspathEntries(classpaths); diff --git a/migrator/src/com/ohos/migrator/java/JavaTransformer.java b/migrator/src/com/ohos/migrator/java/JavaTransformer.java index ff53eaf6371712610da9dd37ccb660f90be36302..057415c05b86f058ab1e6840316dad7bee3716d0 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTransformer.java +++ b/migrator/src/com/ohos/migrator/java/JavaTransformer.java @@ -15,6 +15,8 @@ package com.ohos.migrator.java; +import com.ohos.migrator.Main; +import com.ohos.migrator.ResultCode; import com.ohos.migrator.Transformer; import com.ohos.migrator.staticTS.NodeBuilder; import com.ohos.migrator.staticTS.parser.StaticTSParser; @@ -26,8 +28,8 @@ import org.antlr.v4.runtime.tree.TerminalNode; import org.eclipse.jdt.core.dom.*; import org.jetbrains.annotations.NotNull; +import java.io.File; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Stack; @@ -38,6 +40,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { private final CompilationUnit javaCU; private CompilationUnitContext stsCU; + private final File srcFile; private ParserRuleContext stsCurrent; private final Stack stsSaved = new Stack<>(); @@ -150,8 +153,9 @@ public class JavaTransformer extends ASTVisitor implements Transformer { popStatement(); // IterationStatement, StatementContext } - public JavaTransformer(CompilationUnit javaCU) { + public JavaTransformer(CompilationUnit javaCU, File srcFile) { this.javaCU = javaCU; + this.srcFile = srcFile; } public CompilationUnitContext transform() { @@ -173,6 +177,32 @@ public class JavaTransformer extends ASTVisitor implements Transformer { else if (node instanceof Type) ++countTypeTotal; } + + // TODO: Remove as translation of remaining Java AST nodes is implemented! + @Override + public boolean visit(TryStatement node) { + return false; + } + @Override + public boolean visit(ThrowStatement node) { + return false; + } + @Override + public boolean visit(ExpressionMethodReference node) { + return false; + } + @Override + public boolean visit(SuperMethodReference node) { + return false; + } + @Override + public boolean visit(TypeMethodReference node) { + return false; + } + @Override + public boolean visit(CreationReference node) { + return false; + } }); // Visit Java AST and construct StaticTS AST. @@ -1351,8 +1381,17 @@ public class JavaTransformer extends ASTVisitor implements Transformer { public boolean visit(MethodDeclaration javaMethodDeclaration) { boolean isInClassContext = stsCurrent instanceof ClassBodyContext; assert(isInClassContext || (stsCurrent instanceof InterfaceBodyContext)); - - pushCurrent(createMemberContextWithAccessModifier(javaMethodDeclaration.getModifiers())); + + // 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. + // NOTE: This has to happen BEFORE any further pushCurrent + // or a similar call that changes the value of stsCurrent! + ParserRuleContext enclosingContext = stsCurrent.getParent(); + int javaMods = javaMethodDeclaration.getModifiers(); + + pushCurrent(createMemberContextWithAccessModifier(javaMods)); + Block javaBlock = javaMethodDeclaration.getBody(); if (javaMethodDeclaration.isConstructor()) { @@ -1392,6 +1431,42 @@ public class JavaTransformer extends ASTVisitor implements Transformer { pushCurrent(new BlockContext(stsCurrent, 0)); } + // For synchronized methods, inject MonitorEnter and deferred MonitorExit calls + // in front of all other statements in the method body. The argument of both calls + // is 'this' for non-static methods and class literal of the enclosing class otherwise. + if ((javaMods & Modifier.SYNCHRONIZED) != 0) { + // Figure out enclosing class or interface name. In case we're in anonymous class + // instance creation context, leave it null - we won't need it as this context + // doesn't allow static methods for which we need the class or interface name. + String enclosingTypeName = null; + if (enclosingContext.getRuleIndex() == StaticTSParser.RULE_classDeclaration) + enclosingTypeName = ((ClassDeclarationContext)enclosingContext).Identifier().getText(); + else if (enclosingContext.getRuleIndex() == StaticTSParser.RULE_interfaceDeclaration) + enclosingTypeName = ((InterfaceDeclarationContext)enclosingContext).Identifier().getText(); + + // Add MonitorEnter call + // NOTE: The argument has to be added manually as we don't have it in Java AST. + CallExpressionContext stsMonitorEnterCall = createIntrinsicCall("MonitorEnter"); + SingleExpressionContext stsMonitorEnterCallArg = (javaMods & Modifier.STATIC) == 0 + ? NodeBuilder.thisExpression(null) + : NodeBuilder.classLiteral(enclosingTypeName); + NodeBuilder.addArgument(stsMonitorEnterCall, stsMonitorEnterCallArg); + + // Add deferred MonitorExit call + // NOTE: The argument has to be created again to keep STS AST structure valid + // and added manually as above. + pushStatement(new DeferStatementContext(stsCurrent, 0)); + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Defer)); + + CallExpressionContext stsMonitorExitCall = createIntrinsicCall("MonitorExit"); + SingleExpressionContext stsMonitorExitCallArg = (javaMods & Modifier.STATIC) == 0 + ? NodeBuilder.thisExpression(null) + : NodeBuilder.classLiteral(enclosingTypeName); + NodeBuilder.addArgument(stsMonitorExitCall, stsMonitorExitCallArg); + + popStatement(); // DeferStatementContext + } + List javaBlockStmts = javaBlock.statements(); for (Statement javaStmt : javaBlockStmts) { javaStmt.accept(this); @@ -1496,7 +1571,20 @@ public class JavaTransformer extends ASTVisitor implements Transformer { pushCurrent(new TypeAnnotationContext(stsCurrent, 0)); IVariableBinding variableBinding = javaVariableDeclarationFragment.resolveBinding(); - translateType(variableBinding.getType()); + + if (variableBinding != null) { + translateType(variableBinding.getType()); + } + else { + // Warn and emit __UnknownType__ as variable type + String loc = srcFile.getPath() + ":" + javaCU.getLineNumber(javaVariableDeclarationFragment.getStartPosition()); + Main.addError(ResultCode.TranspileError, "Failed to resolve lambda parameter at " + loc); + + pushCurrent(new PrimaryTypeContext(stsCurrent, 0)); + stsCurrent.addChild(NodeBuilder.typeReference("__UnknownType__")).setParent(stsCurrent); + popCurrent(); // PrimaryTypeContext + } + popCurrent(); // TypeAnnotationContext // Note: no need to process the "{ Dimension }" part, as the extra dimensions @@ -3177,7 +3265,7 @@ public class JavaTransformer extends ASTVisitor implements Transformer { public boolean visit(SimpleName javaName) { IBinding binding = javaName.resolveBinding(); - if (binding.equals(javaVarBinding) && javaSwitchCase != currentSwitchCase) { + if (binding != null && binding.isEqualTo(javaVarBinding) && javaSwitchCase != currentSwitchCase) { javaVarDecl.setProperty(USED_IN_ANOTHER_CASE_CLAUSE, true); done = true; } @@ -3248,9 +3336,21 @@ public class JavaTransformer extends ASTVisitor implements Transformer { createStsParameterList(javaLambdaExpr.parameters()); IMethodBinding lambdaMethod = javaLambdaExpr.resolveMethodBinding(); - pushCurrent(new TypeAnnotationContext(stsCurrent, 0)); - translateType(lambdaMethod.getReturnType()); + + if (lambdaMethod != null) { + translateType(lambdaMethod.getReturnType()); + } + else { + // Warn and emit __UnknownType__ as return type + String loc = srcFile.getPath() + ":" + javaCU.getLineNumber(javaLambdaExpr.getStartPosition()); + Main.addError(ResultCode.TranspileError, "Failed to resolve lambda expression at " + loc); + + pushCurrent(new PrimaryTypeContext(stsCurrent, 0)); + stsCurrent.addChild(NodeBuilder.typeReference("__UnknownType__")).setParent(stsCurrent); + popCurrent(); // PrimaryTypeContext + } + popCurrent(); // TypeAnnotationContext stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Arrow)); @@ -3354,23 +3454,64 @@ public class JavaTransformer extends ASTVisitor implements Transformer { return false; } + // Java: + // synchronized(X) { statements } + // + // STS: + // { MonitorEnter(X); defer MonitorExit(X); statements } @Override public boolean visit(SynchronizedStatement javaSynchrStmt) { - // TODO: To be implemented - // Emit __untranslated_statement 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.untranslatedStatement(javaSynchrStmt)).setParent(stsCurrent); + pushStatement(new BlockContext(stsCurrent, 0)); + + // Add MonitorEnter call + Expression javaExpr = javaSynchrStmt.getExpression(); + createIntrinsicCall("MonitorEnter", javaExpr); + + // Add deferred MonitorExit call + pushStatement(new DeferStatementContext(stsCurrent, 0)); + stsCurrent.addChild(NodeBuilder.terminalNode(StaticTSParser.Defer)); + createIntrinsicCall("MonitorExit", javaExpr); + popStatement(); // DeferStatementContext + + // Translate block statements + List javaStmts = javaSynchrStmt.getBody().statements(); + for (Statement javaStmt : javaStmts) + javaStmt.accept(this); + + popStatement(); // BlockContext + ++countStmtTransformed; return false; } + private CallExpressionContext createIntrinsicCall(String name, Expression... args) { + pushStatement(new ExpressionStatementContext(stsCurrent, 0)); + + CallExpressionContext stsCallExpr = new CallExpressionContext(pushSingleExpression()); + pushCurrent(stsCallExpr); + + stsCurrent.addChild(NodeBuilder.identifierExpression(name)).setParent(stsCurrent); + + pushCurrent(new ArgumentsContext(stsCurrent, 0)); + pushCurrent(new ExpressionSequenceContext(stsCurrent, 0)); + + for (Expression arg : args) + arg.accept(this); + + popCurrent(); // ExpressionSequenceContext + popCurrent(); // ArgumentsContext + + popSingleExpression(); // CallExpressionContext + popStatement(); // ExpressionStatementContext + + return stsCallExpr; + } + // The list of not yet translated Java Expressions: // CreationReference, //?? ExpressionMethodReference, - // LambdaExpression, // SuperMethodReference, // TypeMethodReference // TryStatement - // SynchronizedStatement // ThrowStatement } \ No newline at end of file diff --git a/migrator/src/com/ohos/migrator/java/JavaTranspiler.java b/migrator/src/com/ohos/migrator/java/JavaTranspiler.java index 3d00af70e058dfe05b09052c3c1e70c96ce7c80b..0579956eefae7f5c4140b07d3c0f51b49b43c79d 100644 --- a/migrator/src/com/ohos/migrator/java/JavaTranspiler.java +++ b/migrator/src/com/ohos/migrator/java/JavaTranspiler.java @@ -37,8 +37,11 @@ import java.util.List; */ public class JavaTranspiler extends AbstractTranspiler { - public JavaTranspiler(List src, List libs, String outDir) { + + private boolean noxrefs = false; + public JavaTranspiler(List src, List libs, String outDir, boolean noxrefs) { super(src, libs, outDir); + this.noxrefs = noxrefs; } @Override @@ -46,7 +49,7 @@ public class JavaTranspiler extends AbstractTranspiler { try { CompilationUnit javaCU = parse(srcFile); - CompilationUnitContext stsCU = transform(javaCU); + CompilationUnitContext stsCU = transform(javaCU, srcFile); write(stsCU, srcFile); } catch (IOException e) { @@ -57,12 +60,12 @@ public class JavaTranspiler extends AbstractTranspiler { } private CompilationUnit parse(File srcFile) throws IOException, JavaParserException { - JavaParser parser = new JavaParser(srcFile, sourceFiles, libFiles); + JavaParser parser = new JavaParser(srcFile, sourceFiles, libFiles, noxrefs); return parser.parse(); } - private CompilationUnitContext transform(CompilationUnit javaCU) { - JavaTransformer transformer = new JavaTransformer(javaCU); + private CompilationUnitContext transform(CompilationUnit javaCU, File srcFile) { + JavaTransformer transformer = new JavaTransformer(javaCU, srcFile); return transformer.transform(); } diff --git a/migrator/src/com/ohos/migrator/staticTS/NodeBuilder.java b/migrator/src/com/ohos/migrator/staticTS/NodeBuilder.java index ec7daf0ac9746f82277f64f4eb073ba091dbeaa0..cdea22909f9aa16e8de0a564334a2fd1c45c8ca8 100644 --- a/migrator/src/com/ohos/migrator/staticTS/NodeBuilder.java +++ b/migrator/src/com/ohos/migrator/staticTS/NodeBuilder.java @@ -17,7 +17,6 @@ package com.ohos.migrator.staticTS; import com.ohos.migrator.staticTS.parser.StaticTSParser; import com.ohos.migrator.staticTS.parser.StaticTSParser.*; -import kotlin.jvm.internal.TypeReference; import org.antlr.v4.runtime.CommonToken; import org.antlr.v4.runtime.ParserRuleContext; import org.antlr.v4.runtime.Vocabulary; @@ -230,7 +229,7 @@ public class NodeBuilder { // SimpleType: { Annotation } TypeName // STS: // typeReference: typeReferencePart ('.' typeReferencePart)* - // typeReference: qualifiedName typeArguments? + // typeReferencePart: qualifiedName typeArguments? public static TypeReferenceContext typeReference(String typeName) { TypeReferenceContext stsTypeReference = new TypeReferenceContext(null, 0); TypeReferencePartContext stsTypeRefPart = typeReferencePart(typeName); @@ -441,4 +440,47 @@ public class NodeBuilder { return stsShiftOp; } + + public static void addArgument(CallExpressionContext stsCallExpr, SingleExpressionContext stsArg) { + ArgumentsContext stsArgs = stsCallExpr.arguments(); + if (stsArgs != null) { + ExpressionSequenceContext stsExprSeq = stsArgs.expressionSequence(); + if (stsExprSeq != null) { + stsExprSeq.addChild(stsArg).setParent(stsExprSeq); + } + } + } + + public static SingleExpressionContext thisExpression(TypeReferenceContext stsTypeRef) { + SingleExpressionContext stsSingleExpr = new SingleExpressionContext(null, 0); + ThisExpressionContext stsThisExpression = new ThisExpressionContext(stsSingleExpr); + stsSingleExpr.addChild(stsThisExpression).setParent(stsSingleExpr); + + if (stsTypeRef != null) { + stsThisExpression.addChild(stsTypeRef).setParent(stsThisExpression); + } + stsThisExpression.addChild(terminalNode(StaticTSParser.This)); + + return stsSingleExpr; + } + + public static SingleExpressionContext classLiteral(String className) { + // Sanity check + if (className == null) return null; + + SingleExpressionContext stsSingleExpr = new SingleExpressionContext(null, 0); + ClassLiteralExpressionContext stsClassLiteral = new ClassLiteralExpressionContext(stsSingleExpr); + stsSingleExpr.addChild(stsClassLiteral).setParent(stsSingleExpr); + + // NOTE: Class literal requires PrimaryTypeContext! + // | primaryType Dot Class # ClassLiteralExpression + PrimaryTypeContext stsPrimaryType = new PrimaryTypeContext(stsClassLiteral, 0); + stsPrimaryType.addChild(typeReference(className)).setParent(stsPrimaryType); + stsClassLiteral.addChild(stsPrimaryType).setParent(stsClassLiteral); + + stsClassLiteral.addChild(terminalNode(StaticTSParser.Dot)); + stsClassLiteral.addChild(terminalNode(StaticTSParser.Class)); + + return stsSingleExpr; + } } diff --git a/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSLexer.g4 b/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSLexer.g4 index f0b60c9fa0fc60d812dcf85e25f98c6a43b16a2c..d915ba7ca59a71b474f92584b195eb5e1132c523 100644 --- a/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSLexer.g4 +++ b/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSLexer.g4 @@ -126,6 +126,7 @@ If: 'if'; Throw: 'throw'; Of: 'of'; Try: 'try'; +Defer: 'defer'; From: 'from'; As: 'as'; diff --git a/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSParser.g4 b/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSParser.g4 index de2cf22eef243e5b09d0c49032cf827248f1791c..ca2f06803c73b85c89d63c120361c6ad318ce28b 100644 --- a/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSParser.g4 +++ b/migrator/src/com/ohos/migrator/staticTS/parser/StaticTSParser.g4 @@ -326,6 +326,7 @@ statement | switchStatement | throwStatement | tryStatement + | deferStatement | expressionStatement ; @@ -404,6 +405,10 @@ finallyClause : Finally block ; +deferStatement + : Defer statement + ; + expressionStatement : {this.notOpenBraceAndNotFunction()}? singleExpression SemiColon? ; diff --git a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java index dcf7ceed6ea0f6597c71a9776fdf01712c7aa1c1..4e652d54f2f7574cb2d1c5fc885bff01b959f54d 100644 --- a/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java +++ b/migrator/src/com/ohos/migrator/staticTS/writer/StaticTSWriter.java @@ -1856,4 +1856,14 @@ public class StaticTSWriter extends StaticTSParserBaseVisitor { return null; } + + @Override + public Void visitDeferStatement(DeferStatementContext stsDeferStmt) { + doNeededIndent(); + + sb.append(stsDeferStmt.Defer().getText()).append(' '); + stsDeferStmt.statement().accept(this); + + return null; + } } diff --git a/migrator/test/java/synchronized_blocks.java b/migrator/test/java/synchronized_blocks.java new file mode 100644 index 0000000000000000000000000000000000000000..67ea454cca28962ec4d8cf7424aff288b7ccc9e6 --- /dev/null +++ b/migrator/test/java/synchronized_blocks.java @@ -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; + +class synchronized_blocks { + public static void main(String[] args) { + synchronized_blocks t = new synchronized_blocks(); + synchronized(t) { + synchronized(System.out) { + System.out.println("made it!"); + } + } + } +} diff --git a/migrator/test/java/synchronized_blocks.java.sts b/migrator/test/java/synchronized_blocks.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..9116b4b75ce9da1ad61d0fa1f98086ba92b4a22e --- /dev/null +++ b/migrator/test/java/synchronized_blocks.java.sts @@ -0,0 +1,34 @@ +/* + * 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 synchronized_blocks { + public static main(args : String[]): void { + let t : synchronized_blocks = new synchronized_blocks(); + + { + MonitorEnter(t); + defer MonitorExit(t); + + { + MonitorEnter(System.out); + defer MonitorExit(System.out); + + System.out.println("made it!"); + } + } + } +} diff --git a/migrator/test/java/synchronized_methods.java b/migrator/test/java/synchronized_methods.java new file mode 100644 index 0000000000000000000000000000000000000000..5ade2cd3c653e847c412e08b4eef3a1e8e6feffe --- /dev/null +++ b/migrator/test/java/synchronized_methods.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.ohos.migrator.test.java; + +class synchronized_methods { + int count; + synchronized void bump() { + count++; + } + + static int classCount; + static synchronized void classBump() { + classCount++; + } + + public static void main(String[] args) { + synchronized_methods t = new synchronized_methods() { + private int anonCount; + public synchronized void bump() { anonCount++; } + }; + } +} diff --git a/migrator/test/java/synchronized_methods.java.sts b/migrator/test/java/synchronized_methods.java.sts new file mode 100644 index 0000000000000000000000000000000000000000..b0de0936a720924bb5064e6d369a90bf414f561b --- /dev/null +++ b/migrator/test/java/synchronized_methods.java.sts @@ -0,0 +1,43 @@ +/* + * 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 synchronized_methods { + count : int ; + open bump(): void { + MonitorEnter(this); + defer MonitorExit(this); + count++; + } + + static classCount : int ; + static classBump(): void { + MonitorEnter(synchronized_methods.class); + defer MonitorExit(synchronized_methods.class); + classCount++; + } + + public static main(args : String[]): void { + let t : synchronized_methods = new synchronized_methods() { + private anonCount : int ; + public override bump(): void { + MonitorEnter(this); + defer MonitorExit(this); + anonCount++; + } + }; + } +}