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

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

Issue 192303002: Support for @DartName and @DartOptional annotations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 9 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/EngineAnnotationProcessor.java
diff --git a/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/EngineAnnotationProcessor.java b/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/EngineAnnotationProcessor.java
index e0238b8ad75f959484602b1c50dca6b5b11799eb..22b2915bbb37ac9bec09a18bfeff1af2b59c5f5c 100644
--- a/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/EngineAnnotationProcessor.java
+++ b/editor/util/plugins/com.google.dart.java2dart/src/com/google/dart/java2dart/engine/EngineAnnotationProcessor.java
@@ -17,12 +17,26 @@ package com.google.dart.java2dart.engine;
import com.google.dart.engine.ast.AstNode;
import com.google.dart.engine.ast.ClassDeclaration;
import com.google.dart.engine.ast.CompilationUnit;
+import com.google.dart.engine.ast.ConstructorDeclaration;
+import com.google.dart.engine.ast.DefaultFormalParameter;
+import com.google.dart.engine.ast.Expression;
+import com.google.dart.engine.ast.FieldDeclaration;
+import com.google.dart.engine.ast.FormalParameterList;
+import com.google.dart.engine.ast.MethodDeclaration;
+import com.google.dart.engine.ast.MethodInvocation;
+import com.google.dart.engine.ast.SimpleFormalParameter;
+import com.google.dart.engine.ast.SimpleIdentifier;
+import com.google.dart.engine.ast.VariableDeclaration;
import com.google.dart.engine.utilities.translation.DartBlockBody;
import com.google.dart.java2dart.Context;
import com.google.dart.java2dart.ParsedAnnotation;
import com.google.dart.java2dart.processor.SemanticProcessor;
import com.google.dart.java2dart.util.ToFormattedSourceVisitor;
+import static com.google.dart.java2dart.util.AstFactory.namedExpression;
+import static com.google.dart.java2dart.util.AstFactory.namedFormalParameter;
+import static com.google.dart.java2dart.util.AstFactory.positionalFormalParameter;
+
import org.apache.commons.lang3.StringUtils;
import java.util.List;
@@ -44,7 +58,8 @@ public class EngineAnnotationProcessor extends SemanticProcessor {
for (Entry<AstNode, List<ParsedAnnotation>> entry : nodeAnnotations.entrySet()) {
AstNode node = entry.getKey();
for (ParsedAnnotation annotation : entry.getValue()) {
- if (annotation.getName().equals("DartBlockBody")) {
+ String annotationName = annotation.getName();
+ if (annotationName.equals("DartBlockBody")) {
List<String> bodyLines = (List<String>) annotation.get("value");
String bodySource = "";
if (!bodyLines.isEmpty()) {
@@ -52,18 +67,80 @@ public class EngineAnnotationProcessor extends SemanticProcessor {
bodySource = " " + bodySource.trim();
}
node.setProperty(ToFormattedSourceVisitor.BLOCK_BODY_KEY, bodySource);
- } else if (annotation.getName().equals("DartExpressionBody")) {
+ } else if (annotationName.equals("DartExpressionBody")) {
String bodySource = (String) annotation.get("value");
node.setProperty(ToFormattedSourceVisitor.EXPRESSION_BODY_KEY, bodySource);
- } else if (annotation.getName().equals("DartOmit")) {
+ } else if (annotationName.equals("DartName")) {
+ String newName = (String) annotation.get("value");
+ if (node instanceof ClassDeclaration) {
+ ClassDeclaration classDeclaration = (ClassDeclaration) node;
+ SimpleIdentifier nameNode = classDeclaration.getName();
+ context.renameIdentifier(nameNode, newName);
+ } else if (node instanceof ConstructorDeclaration) {
+ ConstructorDeclaration constructorDeclaration = (ConstructorDeclaration) node;
+ context.renameConstructor(constructorDeclaration, newName);
+ } else if (node instanceof FieldDeclaration) {
+ FieldDeclaration fieldDeclaration = (FieldDeclaration) node;
+ List<VariableDeclaration> fields = fieldDeclaration.getFields().getVariables();
+ if (fields.size() != 1) {
+ throw new IllegalArgumentException(
+ "@DartNode is supported only field declarations with a single field.");
Brian Wilkerson 2014/03/09 23:43:15 nit: "DartNode" --> "DartName"
scheglov 2014/03/10 01:13:42 Done.
+ }
+ SimpleIdentifier nameNode = fields.get(0).getName();
+ context.renameIdentifier(nameNode, newName);
+ } else if (node instanceof MethodDeclaration) {
+ MethodDeclaration methodDeclaration = (MethodDeclaration) node;
+ SimpleIdentifier nameNode = methodDeclaration.getName();
+ context.renameIdentifier(nameNode, newName);
+ } else {
+ throw new IllegalArgumentException("@DartNode is not supported for: " + node.getClass());
Brian Wilkerson 2014/03/09 23:43:15 nit: "DartNode" --> "DartName"
scheglov 2014/03/10 01:13:42 Done.
+ }
+ } else if (annotationName.equals("DartOmit")) {
AstNode parent = node.getParent();
if (parent instanceof CompilationUnit) {
unit.getDeclarations().remove(node);
} else if (parent instanceof ClassDeclaration) {
((ClassDeclaration) parent).getMembers().remove(node);
}
+ } else if (annotationName.equals("DartOptional")) {
+ SimpleFormalParameter parameter = (SimpleFormalParameter) node;
+ AstNode parameterList = parameter.getParent();
+ // prepare annotation arguments
+ String kindName = (String) annotation.get("kind");
+ String defaultValueSource = (String) annotation.get("defaultValue");
+ // replace normal parameter with default
+ DefaultFormalParameter defaultParameter;
+ if (kindName == null || kindName.endsWith(".POSITIONAL")) {
+ defaultParameter = positionalFormalParameter(parameter, null);
+ } else {
+ replaceInvocationArgumentWithNamed(parameter);
+ defaultParameter = namedFormalParameter(parameter, null);
+ }
+ replaceNode(parameterList, parameter, defaultParameter);
+ // set default value
+ defaultParameter.setProperty(
+ ToFormattedSourceVisitor.DEFAULT_VALUE_KEY,
+ defaultValueSource);
+ } else {
+// throw new IllegalArgumentException("Unknown annotation: " + annotationName);
}
}
}
}
+
+ private void replaceInvocationArgumentWithNamed(SimpleFormalParameter parameter) {
+ String parameterName = parameter.getIdentifier().getName();
+ FormalParameterList parameterList = (FormalParameterList) parameter.getParent();
+ AstNode member = parameterList.getParent();
+ int index = parameterList.getParameters().indexOf(parameter);
+ if (member instanceof MethodDeclaration) {
+ MethodDeclaration method = (MethodDeclaration) member;
+ List<MethodInvocation> invocations = context.getInvocations(method);
+ for (MethodInvocation invocation : invocations) {
+ List<Expression> arguments = invocation.getArgumentList().getArguments();
+ Expression argument = arguments.get(index);
+ arguments.set(index, namedExpression(parameterName, argument));
+ }
+ }
+ }
}

Powered by Google App Engine
This is Rietveld 408576698