Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(39)

Unified Diff: editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/MainEngine.java

Issue 475543002: New analyzer snapshot, add format/formatList. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/MainEngine.java
diff --git a/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/MainEngine.java b/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/MainEngine.java
index 8f6f5d2ef97281ee133f8807ae33dae44674eadc..2f93b8ec19c62c392d700c8ff90e4b0696534bd3 100644
--- a/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/MainEngine.java
+++ b/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/MainEngine.java
@@ -28,10 +28,14 @@ import com.google.dart.engine.ast.CompilationUnit;
import com.google.dart.engine.ast.CompilationUnitMember;
import com.google.dart.engine.ast.Expression;
import com.google.dart.engine.ast.FunctionDeclaration;
+import com.google.dart.engine.ast.ImplementsClause;
import com.google.dart.engine.ast.NodeList;
import com.google.dart.engine.ast.ParenthesizedExpression;
+import com.google.dart.engine.ast.SimpleStringLiteral;
import com.google.dart.engine.ast.Statement;
+import com.google.dart.engine.ast.TypeName;
import com.google.dart.engine.ast.visitor.NodeLocator;
+import com.google.dart.engine.ast.visitor.RecursiveAstVisitor;
import com.google.dart.engine.context.AnalysisContext;
import com.google.dart.engine.context.AnalysisErrorInfo;
import com.google.dart.engine.context.AnalysisResult;
@@ -47,6 +51,7 @@ import com.google.dart.engine.source.Source;
import com.google.dart.engine.source.SourceFactory;
import com.google.dart.engine.utilities.io.PrintStringWriter;
import com.google.dart.java2dart.Context;
+import com.google.dart.java2dart.SyntaxTranslator;
import com.google.dart.java2dart.processor.BeautifySemanticProcessor;
import com.google.dart.java2dart.processor.CollectionSemanticProcessor;
import com.google.dart.java2dart.processor.GuavaSemanticProcessor;
@@ -64,6 +69,7 @@ import static com.google.dart.java2dart.util.AstFactory.exportDirective;
import static com.google.dart.java2dart.util.AstFactory.importDirective;
import static com.google.dart.java2dart.util.AstFactory.importShowCombinator;
import static com.google.dart.java2dart.util.AstFactory.libraryDirective;
+import static com.google.dart.java2dart.util.TokenFactory.token;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
@@ -298,6 +304,7 @@ public class MainEngine {
context.ensureUniqueClassMemberNames();
context.applyLocalVariableSemanticChanges(dartUnit);
EngineSemanticProcessor.rewriteReflectionFieldsWithDirect(context, dartUnit);
+ rewriteErrorCodePatterns(dartUnit);
// dump as several libraries
Files.copy(new File("resources/interner.dart"), new File(targetFolder + "/interner.dart"));
Files.copy(new File("resources/java_core.dart"), new File(targetFolder + "/java_core.dart"));
@@ -410,10 +417,9 @@ public class MainEngine {
}
{
CompilationUnit library = buildErrorLibrary();
- Files.write(
- getFormattedSource(library),
- new File(targetFolder + "/error.dart"),
- Charsets.UTF_8);
+ String source = getFormattedSource(library);
+ source = replaceSourceFragment(source, "JavaString.format(", "formatList(");
+ Files.write(source, new File(targetFolder + "/error.dart"), Charsets.UTF_8);
}
{
CompilationUnit library = buildScannerLibrary();
@@ -1685,6 +1691,48 @@ public class MainEngine {
return matcher.replaceFirst(replacement);
}
+ /**
+ * Rewrites errors from the Java.format() style to the MessageFormat style.
+ */
+ private static void rewriteErrorCodePatterns(CompilationUnit unit) {
+ unit.accept(new RecursiveAstVisitor<Void>() {
+ @Override
+ public Void visitClassDeclaration(ClassDeclaration node) {
+ ImplementsClause implementsClause = node.getImplementsClause();
+ if (implementsClause != null) {
+ NodeList<TypeName> interfaces = implementsClause.getInterfaces();
+ if (interfaces.size() == 1 && interfaces.get(0).getName().getName().equals("ErrorCode")) {
+ return super.visitClassDeclaration(node);
+ }
+ }
+ return null;
+ }
+
+ @Override
+ public Void visitSimpleStringLiteral(SimpleStringLiteral node) {
+ int index = 0;
+ int lastPatternEnd = 0;
+ String content = node.getValue();
+ String newContent = "";
+ Pattern pattern = Pattern.compile("(%.)");
+ Matcher matcher = pattern.matcher(content);
+ while (matcher.find()) {
+ newContent += content.substring(lastPatternEnd, matcher.start());
+ newContent += "{" + index + "}";
+ index++;
+ lastPatternEnd = matcher.end();
+ }
+ newContent += content.substring(lastPatternEnd);
+ String lexeme = "\""
+ + newContent.replace("\\", "\\\\").replace("\"", "\\\"").replace("$", "\\$") + "\"";
+ SyntaxTranslator.replaceNode(node.getParent(), node, new SimpleStringLiteral(
+ token(lexeme),
+ newContent));
+ return super.visitSimpleStringLiteral(node);
+ }
+ });
+ }
+
private static void sortUnitMembersByName(CompilationUnit unit) {
Collections.sort(unit.getDeclarations(), new Comparator<CompilationUnitMember>() {
@Override

Powered by Google App Engine
This is Rietveld 408576698