Skip to content

Commit

Permalink
Convert LambdaExpression into TypeDeclaration nodes instead of creati…
Browse files Browse the repository at this point in the history
…ng the class dynamically.

	Change on 2016/09/13 by kstanger <kstanger@google.com>

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=133037217
  • Loading branch information
kstanger authored and tomball committed Sep 16, 2016
1 parent 6d05ccc commit 7739393
Show file tree
Hide file tree
Showing 14 changed files with 335 additions and 531 deletions.
Expand Up @@ -464,8 +464,10 @@ private void printStaticFieldFullDeclaration(VariableDeclarationFragment fragmen
}

private void printTypeLiteralDeclaration() {
newline();
printf("J2OBJC_TYPE_LITERAL_HEADER(%s)\n", typeName);
if (needsTypeLiteral()) {
newline();
printf("J2OBJC_TYPE_LITERAL_HEADER(%s)\n", typeName);
}
}

private void printBoxedOperators() {
Expand Down
Expand Up @@ -262,6 +262,11 @@ protected boolean hasInitializeMethod() {
return !typeNode.getClassInitStatements().isEmpty();
}

protected boolean needsTypeLiteral() {
return !(BindingUtil.isPackageInfo(typeBinding) || typeBinding.isAnonymous()
|| BindingUtil.isLambda(typeBinding));
}

protected String getDeclarationType(IVariableBinding var) {
ITypeBinding type = var.getType();
if (BindingUtil.isVolatile(var)) {
Expand Down
Expand Up @@ -93,9 +93,7 @@ protected void generate() {
}

printOuterDeclarations();
if (!BindingUtil.isPackageInfo(typeBinding)) {
printTypeLiteralImplementation();
}
printTypeLiteralImplementation();
}

private void printInitFlagDefinition() {
Expand Down Expand Up @@ -219,9 +217,11 @@ private void printEnumValuesArray() {
}

private void printTypeLiteralImplementation() {
newline();
printf("J2OBJC_%s_TYPE_LITERAL_SOURCE(%s)\n",
isInterfaceType() ? "INTERFACE" : "CLASS", typeName);
if (needsTypeLiteral()) {
newline();
printf("J2OBJC_%s_TYPE_LITERAL_SOURCE(%s)\n",
isInterfaceType() ? "INTERFACE" : "CLASS", typeName);
}
}

private boolean extendsNumber(ITypeBinding type) {
Expand Down
Expand Up @@ -347,7 +347,8 @@ public static JdtPackageElement getPackageElement(
public static IBinding unwrapElement(Element element) {
if (element instanceof GeneratedVariableElement) {
Element possibleEnclosing = element.getEnclosingElement();
GeneratedVariableBinding newBinding = new GeneratedVariableBinding(element.toString(), 0,
GeneratedVariableBinding newBinding = new GeneratedVariableBinding(
element.toString(), ElementUtil.fromModifierSet(element.getModifiers()),
element.asType(), element.getKind() == ElementKind.FIELD,
element.getKind() == ElementKind.PARAMETER,
possibleEnclosing != null ? possibleEnclosing.asType() : null, null);
Expand Down Expand Up @@ -382,6 +383,10 @@ public static IVariableBinding unwrapVariableElement(VariableElement v) {
return (IVariableBinding) unwrapElement(v);
}

public static IMethodBinding unwrapExecutableElement(ExecutableElement e) {
return (IMethodBinding) unwrapElement(e);
}

public static IBinding unwrapTypeMirrorIntoBinding(TypeMirror t) {
if (t == null) {
return null;
Expand Down
Expand Up @@ -181,6 +181,9 @@ public static void applyMutations(
new AnonymousClassConverter().run(unit);
ticker.tick("AnonymousClassConverter");

new LambdaRewriter().run(unit);
ticker.tick("LambdaRewriter");

new InnerClassExtractor(outerResolver, unit).run(unit);
ticker.tick("InnerClassExtractor");

Expand All @@ -203,6 +206,8 @@ public static void applyMutations(
}

// Adds nil_chk calls wherever an expression is dereferenced.
// After: InnerClassExtractor - Cannot handle local classes.
// After: InitializationNormalizer
// Before: LabelRewriter - Control flow analysis requires original Java
// labels.
new NilCheckResolver().run(unit);
Expand All @@ -223,11 +228,6 @@ public static void applyMutations(
new OcniExtractor(unit, deadCodeMap).run(unit);
ticker.tick("OcniExtractor");

// After: NilCheckResolver - Don't add nil checks to our generated code,
// NilCheckResolver doesn't handle Functions correctly.
new LambdaRewriter(outerResolver).run(unit);
ticker.tick("LambdaRewriter");

// Before: AnnotationRewriter - Needs AnnotationRewriter to add the
// annotation metadata to the generated package-info type.
PackageInfoRewriter.run(unit);
Expand Down

0 comments on commit 7739393

Please sign in to comment.