Skip to content

Commit

Permalink
Hides private static variables to the implementation file. Take 2 wit…
Browse files Browse the repository at this point in the history
…h C++ compilation fix.

	Change on 2015/03/11 by kstanger <kstanger@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=88357190
  • Loading branch information
kstanger committed Mar 18, 2015
1 parent 5f66e8b commit 9838c13
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 113 deletions.
Expand Up @@ -36,14 +36,14 @@
import com.google.devtools.j2objc.ast.TreeUtil;
import com.google.devtools.j2objc.ast.Type;
import com.google.devtools.j2objc.ast.TypeDeclaration;
import com.google.devtools.j2objc.ast.VariableDeclarationFragment;
import com.google.devtools.j2objc.types.HeaderImportCollector;
import com.google.devtools.j2objc.types.Import;
import com.google.devtools.j2objc.types.Types;
import com.google.devtools.j2objc.util.BindingUtil;
import com.google.devtools.j2objc.util.NameTable;

import org.eclipse.jdt.core.dom.ITypeBinding;
import org.eclipse.jdt.core.dom.IVariableBinding;
import org.eclipse.jdt.core.dom.Modifier;

import java.util.Iterator;
Expand Down Expand Up @@ -357,10 +357,7 @@ static enum SortState { BEFORE_DECLS, METHODS, AFTER_DECLS };
*/
@Override
protected void printInnerDeclarations(AbstractTypeDeclaration node) {
Iterable<BodyDeclaration> iter = Iterables.filter(Iterables.filter(
node.getBodyDeclarations(), isInnerFilter()), printDeclFilter());

List<BodyDeclaration> allDeclarations = Lists.newArrayList(iter);
List<BodyDeclaration> allDeclarations = Lists.newArrayList(getInnerDeclarations(node));
List<BodyDeclaration> beforeDeclarations = Lists.newArrayList();
List<MethodDeclaration> allMethods = Lists.newArrayList();
List<BodyDeclaration> afterDeclarations = Lists.newArrayList();
Expand Down Expand Up @@ -418,13 +415,6 @@ private void printSortedMethods(List<MethodDeclaration> allMethods, String title
}
}

protected boolean shouldPrintDeclaration(BodyDeclaration decl) {
if (decl instanceof FunctionDeclaration) {
return !Modifier.isPrivate(decl.getModifiers());
}
return !Options.hidePrivateMembers() || !isPrivateOrSynthetic(decl.getModifiers());
}

protected void printForwardDeclarations(Set<Import> forwardDecls) {
Set<String> forwardStmts = Sets.newTreeSet();
for (Import imp : forwardDecls) {
Expand Down Expand Up @@ -526,20 +516,9 @@ private void printAnnotationAccessors(List<AnnotationTypeMemberDeclaration> memb
}
}

private void printConstantDefines(AbstractTypeDeclaration node) {
ITypeBinding type = node.getTypeBinding();
boolean needsNewline = true;
for (IVariableBinding field : type.getDeclaredFields()) {
if (BindingUtil.isPrimitiveConstant(field)) {
if (needsNewline) {
needsNewline = false;
newline();
}
printf("#define %s ", NameTable.getPrimitiveConstantName(field));
Object value = field.getConstantValue();
assert value != null;
println(LiteralGenerator.generate(value));
}
}
@Override
protected void printStaticFieldDeclaration(
VariableDeclarationFragment fragment, String baseDeclaration) {
println("FOUNDATION_EXPORT " + baseDeclaration + ";");
}
}
Expand Up @@ -92,7 +92,7 @@ public void generate() {
printImports(unit);
printIgnoreIncompletePragmas(unit);
pushIgnoreDeprecatedDeclarationsPragma();
printFinalFunctionDecls(types);
printPrivateDeclarations(types);
printClassExtensions(types);
for (AbstractTypeDeclaration type : types) {
generate(type);
Expand Down Expand Up @@ -328,11 +328,11 @@ private void printInitFlagDefinition(AbstractTypeDeclaration node) {
}

private void printInnerDefinitions(AbstractTypeDeclaration node) {
printDefinitions(Iterables.filter(node.getBodyDeclarations(), isInnerFilter()));
printDefinitions(getInnerDefinitions(node));
}

private void printOuterDefinitions(AbstractTypeDeclaration node) {
printDefinitions(Iterables.filter(node.getBodyDeclarations(), isOuterFilter()));
printDefinitions(getOuterDefinitions(node));
}

private void printDefinitions(Iterable<BodyDeclaration> declarations) {
Expand Down Expand Up @@ -410,26 +410,49 @@ private void printImports(CompilationUnit node) {
}
}

private void printStaticVars(AbstractTypeDeclaration node) {
Iterable<VariableDeclarationFragment> fragments = getStaticFieldsNeedingInitialization(node);
if (!Iterables.isEmpty(fragments)) {
newline();
@Override
protected void printStaticFieldDeclaration(
VariableDeclarationFragment fragment, String baseDeclaration) {
Expression initializer = fragment.getInitializer();
print("static " + baseDeclaration);
if (initializer != null) {
print(" = " + generateExpression(initializer));
}
for (VariableDeclarationFragment var : fragments) {
IVariableBinding binding = var.getVariableBinding();
String name = NameTable.getStaticVarQualifiedName(binding);
String objcType = NameTable.getObjCType(binding.getType());
Expression initializer = var.getInitializer();
if (initializer != null) {
printf("%s %s = %s;\n", objcType, name, generateExpression(initializer));
} else {
printf("%s %s;\n", objcType, name);
println(";");
}

private void printStaticVars(AbstractTypeDeclaration node) {
boolean needsNewline = true;
for (FieldDeclaration field : getStaticFields(node)) {
if (shouldPrintDeclaration(field)) {
// Static var is defined in declaration.
continue;
}
for (VariableDeclarationFragment var : field.getFragments()) {
IVariableBinding binding = var.getVariableBinding();
Expression initializer = var.getInitializer();
if (BindingUtil.isPrimitiveConstant(binding)) {
continue;
} else if (needsNewline) {
needsNewline = false;
newline();
}
String name = NameTable.getStaticVarQualifiedName(binding);
String objcType = NameTable.getObjCType(binding.getType());
objcType += objcType.endsWith("*") ? "" : " ";
if (initializer != null) {
printf("%s%s = %s;\n", objcType, name, generateExpression(initializer));
} else {
printf("%s%s;\n", objcType, name);
}
}
}
}

private void printFinalFunctionDecls(List<AbstractTypeDeclaration> types) {
private void printPrivateDeclarations(List<AbstractTypeDeclaration> types) {
for (AbstractTypeDeclaration type : types) {
printConstantDefines(type);
printStaticFieldDeclarations(type);
printOuterDeclarations(type);
}
}
Expand Down Expand Up @@ -624,20 +647,11 @@ private void printClassExtensions(List<AbstractTypeDeclaration> types) {
}
}

protected boolean shouldPrintDeclaration(BodyDeclaration decl) {
if (decl instanceof FunctionDeclaration) {
return Modifier.isPrivate(decl.getModifiers());
}
// TODO(kstanger): exclude synthetic.
return Options.hidePrivateMembers() && isPrivateOrSynthetic(decl.getModifiers());
}

private void printClassExtension(AbstractTypeDeclaration node) {
if (Options.hidePrivateMembers()) {
Iterable<FieldDeclaration> privateFields = getFieldsToDeclare(node);
boolean hasPrivateFields = !Iterables.isEmpty(privateFields);
Iterable<BodyDeclaration> privateDecls = Iterables.filter(Iterables.filter(
node.getBodyDeclarations(), isInnerFilter()), printDeclFilter());
Iterable<BodyDeclaration> privateDecls = getInnerDeclarations(node);
if (!Iterables.isEmpty(privateDecls) || hasPrivateFields) {
String typeName = NameTable.getFullName(node.getTypeBinding());
newline();
Expand All @@ -655,4 +669,9 @@ private void printClassExtension(AbstractTypeDeclaration node) {
}
}
}

@Override
protected boolean printPrivateDeclarations() {
return true;
}
}
Expand Up @@ -96,14 +96,6 @@ public boolean apply(BodyDeclaration decl) {

private static final Predicate<BodyDeclaration> NOT_STATIC = Predicates.not(IS_STATIC);

private static final Predicate<VariableDeclarationFragment> NEEDS_INITIALIZATION_PRED =
new Predicate<VariableDeclarationFragment>() {
public boolean apply(VariableDeclarationFragment frag) {
IVariableBinding binding = frag.getVariableBinding();
return BindingUtil.isStatic(binding) && !BindingUtil.isPrimitiveConstant(binding);
}
};

protected static final String DEPRECATED_ATTRIBUTE = "__attribute__((deprecated))";

protected boolean isInterfaceType(AbstractTypeDeclaration node) {
Expand All @@ -114,21 +106,16 @@ protected boolean isInterfaceType(AbstractTypeDeclaration node) {

protected Iterable<VariableDeclarationFragment> getStaticFieldsNeedingAccessors(
AbstractTypeDeclaration node) {
// TODO(kstanger): Move private static fields to implementation file.
return TreeUtil.asFragments(Iterables.filter(getStaticFields(node), printDeclFilter));
}

protected Iterable<FieldDeclaration> getStaticFields(AbstractTypeDeclaration node) {
Iterable<FieldDeclaration> fieldDecls = TreeUtil.getFieldDeclarations(node);
// All variables declared in interface types are static.
if (!isInterfaceType(node)) {
fieldDecls = Iterables.filter(fieldDecls, IS_STATIC);
}
return TreeUtil.asFragments(fieldDecls);
}

/**
* Excludes primitive constants which will not have variables declared for them.
*/
protected Iterable<VariableDeclarationFragment> getStaticFieldsNeedingInitialization(
AbstractTypeDeclaration node) {
return Iterables.filter(TreeUtil.getAllFields(node), NEEDS_INITIALIZATION_PRED);
return fieldDecls;
}

protected boolean hasInitializeMethod(AbstractTypeDeclaration node) {
Expand All @@ -137,7 +124,22 @@ protected boolean hasInitializeMethod(AbstractTypeDeclaration node) {

protected abstract void printFunctionDeclaration(FunctionDeclaration declaration);

protected abstract boolean shouldPrintDeclaration(BodyDeclaration declaration);
protected boolean printPrivateDeclarations() {
return false;
}

protected boolean shouldPrintDeclaration(BodyDeclaration decl) {
int modifiers = decl.getModifiers();
// Don't print declarations for any synthetic members.
if (BindingUtil.isSynthetic(modifiers)) {
return false;
}
boolean isPrivate = false;
if (Options.hidePrivateMembers() || decl instanceof FunctionDeclaration) {
isPrivate = Modifier.isPrivate(modifiers);
}
return isPrivate == printPrivateDeclarations();
}

private void printDeclaration(BodyDeclaration declaration) {
switch (declaration.getKind()) {
Expand All @@ -162,13 +164,11 @@ protected void printDeclarations(Iterable<BodyDeclaration> declarations) {
}

protected void printInnerDeclarations(AbstractTypeDeclaration node) {
printDeclarations(Iterables.filter(Iterables.filter(
node.getBodyDeclarations(), isInnerFilter()), printDeclFilter()));
printDeclarations(getInnerDeclarations(node));
}

protected void printOuterDeclarations(AbstractTypeDeclaration node) {
printDeclarations(Iterables.filter(Iterables.filter(
node.getBodyDeclarations(), isOuterFilter()), printDeclFilter()));
printDeclarations(getOuterDeclarations(node));
}

private static final Predicate<BodyDeclaration> IS_OUTER_DECL = new Predicate<BodyDeclaration>() {
Expand All @@ -177,29 +177,44 @@ public boolean apply(BodyDeclaration decl) {
}
};

private static final Predicate<BodyDeclaration> IS_INNER_DECL = Predicates.not(IS_OUTER_DECL);

protected Predicate<BodyDeclaration> isInnerFilter() {
return IS_INNER_DECL;
}

protected Predicate<BodyDeclaration> isOuterFilter() {
return IS_OUTER_DECL;
}
private static final Predicate<BodyDeclaration> IS_INNER_DECL = new Predicate<BodyDeclaration>() {
public boolean apply(BodyDeclaration decl) {
switch (decl.getKind()) {
case METHOD_DECLARATION:
case NATIVE_DECLARATION:
return true;
}
return false;
}
};

private final Predicate<BodyDeclaration> printDeclFilterImpl = new Predicate<BodyDeclaration>() {
private final Predicate<BodyDeclaration> printDeclFilter = new Predicate<BodyDeclaration>() {
public boolean apply(BodyDeclaration decl) {
return shouldPrintDeclaration(decl);
}
};

protected Predicate<BodyDeclaration> printDeclFilter() {
return printDeclFilterImpl;
protected Iterable<BodyDeclaration> getInnerDeclarations(AbstractTypeDeclaration node) {
return Iterables.filter(Iterables.filter(
node.getBodyDeclarations(), IS_INNER_DECL), printDeclFilter);
}

protected Iterable<BodyDeclaration> getOuterDeclarations(AbstractTypeDeclaration node) {
return Iterables.filter(Iterables.filter(
node.getBodyDeclarations(), IS_OUTER_DECL), printDeclFilter);
}

protected Iterable<BodyDeclaration> getInnerDefinitions(AbstractTypeDeclaration node) {
return Iterables.filter(node.getBodyDeclarations(), IS_INNER_DECL);
}

protected Iterable<BodyDeclaration> getOuterDefinitions(AbstractTypeDeclaration node) {
return Iterables.filter(node.getBodyDeclarations(), IS_OUTER_DECL);
}

protected Iterable<FieldDeclaration> getFieldsToDeclare(AbstractTypeDeclaration node) {
return Iterables.filter(Iterables.filter(
TreeUtil.getFieldDeclarations(node), NOT_STATIC), printDeclFilter());
TreeUtil.getFieldDeclarations(node), NOT_STATIC), printDeclFilter);
}

/**
Expand Down Expand Up @@ -538,11 +553,15 @@ protected String getFunctionSignature(FunctionDeclaration function) {

protected void printStaticFieldDeclarations(AbstractTypeDeclaration node) {
for (VariableDeclarationFragment fragment : getStaticFieldsNeedingAccessors(node)) {
printStaticFieldDeclaration(fragment.getVariableBinding());
printStaticFieldFullDeclaration(fragment);
}
}

private void printStaticFieldDeclaration(IVariableBinding var) {
protected abstract void printStaticFieldDeclaration(
VariableDeclarationFragment fragment, String baseDeclaration);

private void printStaticFieldFullDeclaration(VariableDeclarationFragment fragment) {
IVariableBinding var = fragment.getVariableBinding();
String objcType = NameTable.getObjCType(var.getType());
String typeWithSpace = objcType + (objcType.endsWith("*") ? "" : " ");
String name = NameTable.getStaticVarName(var);
Expand All @@ -553,7 +572,8 @@ private void printStaticFieldDeclaration(IVariableBinding var) {
if (BindingUtil.isPrimitiveConstant(var)) {
name = var.getName();
} else {
printf("FOUNDATION_EXPORT %s%s_%s;\n", typeWithSpace, className, name);
printStaticFieldDeclaration(
fragment, String.format("%s%s_%s", typeWithSpace, className, name));
}
printf("J2OBJC_STATIC_FIELD_GETTER(%s, %s, %s)\n", className, name, objcType);
if (!isFinal) {
Expand All @@ -564,4 +584,26 @@ private void printStaticFieldDeclaration(IVariableBinding var) {
}
}
}

protected void printConstantDefines(AbstractTypeDeclaration node) {
boolean needsNewline = true;
for (FieldDeclaration fieldDecl : getStaticFields(node)) {
if (!shouldPrintDeclaration(fieldDecl)) {
continue;
}
for (VariableDeclarationFragment fragment : fieldDecl.getFragments()) {
IVariableBinding field = fragment.getVariableBinding();
if (BindingUtil.isPrimitiveConstant(field)) {
if (needsNewline) {
needsNewline = false;
newline();
}
printf("#define %s ", NameTable.getPrimitiveConstantName(field));
Object value = field.getConstantValue();
assert value != null;
println(LiteralGenerator.generate(value));
}
}
}
}
}

0 comments on commit 9838c13

Please sign in to comment.