Skip to content

Commit

Permalink
Cleanup: close jar files when finished reading them, remove unused
Browse files Browse the repository at this point in the history
code and import, always use @autoreleasepool (iOS 4 didn't support
it, but is now two releases back), fixed test script.
	Change on 2013/04/01 by tball <tball@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=44690933
  • Loading branch information
tomball committed Apr 10, 2013
1 parent 1d00f55 commit 5bcaadc
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 96 deletions.
4 changes: 2 additions & 2 deletions scripts/runtests.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/bash
#!/bin/bash
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
Expand All @@ -20,7 +20,7 @@
# directory.

if [ $# -eq 0 ]; then
TESTS=$(/usr/bin/find build/tests -perm 755 -a -type f)
TESTS=$(/usr/bin/find build_result/tests -perm 755 -a -type f)
else
TESTS=$*
fi
Expand Down
47 changes: 27 additions & 20 deletions translator/src/main/java/com/google/devtools/j2objc/J2ObjC.java
Original file line number Diff line number Diff line change
Expand Up @@ -708,29 +708,36 @@ private static void initPlugins(String[] pluginPaths, String pluginOptionString)
JarFileLoader classLoader = new JarFileLoader();
for (String path : pluginPaths) {
if (path.endsWith(".jar")) {
JarInputStream jarStream = new JarInputStream(new FileInputStream(path));
classLoader.addJarFile(new File(path).getAbsolutePath());

JarEntry entry;
while ((entry = jarStream.getNextJarEntry()) != null) {
String entryName = entry.getName();
if (!entryName.endsWith(".class")) {
continue;
}
JarInputStream jarStream = null;
try {
jarStream = new JarInputStream(new FileInputStream(path));
classLoader.addJarFile(new File(path).getAbsolutePath());

JarEntry entry;
while ((entry = jarStream.getNextJarEntry()) != null) {
String entryName = entry.getName();
if (!entryName.endsWith(".class")) {
continue;
}

String className =
entryName.replaceAll("/", "\\.").substring(0, entryName.length() - ".class".length());
String className =
entryName.replaceAll("/", "\\.").substring(0, entryName.length() - ".class".length());

try {
Class<?> clazz = classLoader.loadClass(className);
if (Plugin.class.isAssignableFrom(clazz)) {
Constructor<?> cons = clazz.getDeclaredConstructor();
Plugin plugin = (Plugin) cons.newInstance();
plugin.initPlugin(pluginOptionString);
Options.getPlugins().add(plugin);
try {
Class<?> clazz = classLoader.loadClass(className);
if (Plugin.class.isAssignableFrom(clazz)) {
Constructor<?> cons = clazz.getDeclaredConstructor();
Plugin plugin = (Plugin) cons.newInstance();
plugin.initPlugin(pluginOptionString);
Options.getPlugins().add(plugin);
}
} catch (Exception e) {
throw new IOException("plugin exception: ", e);
}
} catch (Exception e) {
throw new IOException("plugin exception: ", e);
}
} finally {
if (jarStream != null) {
jarStream.close();
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -833,12 +833,7 @@ private String getRightShiftType(Expression node) {
@Override
public boolean visit(Block node) {
if (Types.hasAutoreleasePool(node)) {
if (Options.useReferenceCounting()) {
// TODO(user): use @autoreleasepool like ARC when iOS 5 is minimum.
buffer.append("{\nNSAutoreleasePool *pool__ = [[NSAutoreleasePool alloc] init];\n");
} else if (Options.useARC()) {
buffer.append("{\n@autoreleasepool ");
}
buffer.append("{\n@autoreleasepool ");
}
buffer.append("{\n");
List<?> stmts = node.statements();
Expand All @@ -863,11 +858,7 @@ public boolean visit(Block node) {
printStatements(stmts);
buffer.append("}\n");
if (Types.hasAutoreleasePool(node)) {
if (Options.useReferenceCounting()) {
buffer.append("[pool__ release];\n}\n");
} else if (Options.useARC()) {
buffer.append("}\n");
}
buffer.append("}\n");
}
return false;
}
Expand Down Expand Up @@ -1078,17 +1069,9 @@ public boolean visit(FieldAccess node) {
@SuppressWarnings("unchecked")
@Override
public boolean visit(ForStatement node) {
boolean emitAutoreleasePool = false;
buffer.append("for (");
for (Iterator<Expression> it = node.initializers().iterator(); it.hasNext(); ) {
Expression next = it.next();
if (next instanceof VariableDeclarationExpression) {
List<VariableDeclarationFragment> vars =
((VariableDeclarationExpression) next).fragments();
for (VariableDeclarationFragment fragment : vars) {
emitAutoreleasePool |= Types.hasAutoreleasePoolAnnotation(Types.getBinding(fragment));
}
}
next.accept(this);
if (it.hasNext()) {
buffer.append(", ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -504,19 +504,6 @@ private GeneratedMethodBinding addDefaultConstructor(
return binding;
}

private boolean argsMatch(List<Expression> invocationArguments, ITypeBinding[] parameterTypes) {
if (invocationArguments.size() != parameterTypes.length) {
return false;
}
for (int i = 0; i < parameterTypes.length; i++) {
ITypeBinding argType = Types.getTypeBinding(invocationArguments.get(i));
if (!parameterTypes[i].isEqualTo(argType)) {
return false;
}
}
return true;
}

private IMethodBinding findSuperConstructorBinding(ITypeBinding clazz,
List<Expression> superArgs) {
if (clazz == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.google.common.annotations.GwtIncompatible;
import com.google.common.collect.Sets;
import com.google.devtools.j2objc.J2ObjC;
import com.google.devtools.j2objc.types.GeneratedMethodBinding;
import com.google.devtools.j2objc.types.NodeCopier;
import com.google.devtools.j2objc.types.Types;
import com.google.devtools.j2objc.util.ErrorReportingASTVisitor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ private void addOuterFields(AbstractTypeDeclaration node) {

// Ensure that the new outer field does not conflict with a field in a superclass.
ITypeBinding superClazz = clazz.getSuperclass();
ITypeBinding superDeclaringClazz = superClazz.getDeclaringClass();
int suffix = 0;
while (superClazz.getDeclaringClass() != null) {
if (!Modifier.isStatic(superClazz.getModifiers())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -457,9 +457,8 @@ public void testAutoreleasePoolMethod() throws IOException {
"}",
"Test", "Test.m");
assertTranslation(translation, "- (void)foo {\n" +
" NSAutoreleasePool *pool__ = [[NSAutoreleasePool alloc] init];\n" +
" {\n }\n" +
" [pool__ release];\n" +
" @autoreleasepool {\n" +
" }\n" +
"}");
}

Expand Down Expand Up @@ -494,9 +493,7 @@ public void testAutoreleasePoolAnonymousClassMethod() throws IOException {
"}",
"Test", "Test.m");
assertTranslation(translation, "- (void)apply {\n" +
" NSAutoreleasePool *pool__ = [[NSAutoreleasePool alloc] init];\n" +
" {\n }\n" +
" [pool__ release];\n" +
" @autoreleasepool {\n }\n" +
"}");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1204,9 +1204,7 @@ public void testAutoreleasePoolForStatement() throws IOException {
"}",
"Test", "Test.m");
assertTranslation(translation, " for (int i = 0; i < 10; i++) {\n" +
" NSAutoreleasePool *pool__ = [[NSAutoreleasePool alloc] init];\n" +
" {\n }\n" +
" [pool__ release];\n" +
" @autoreleasepool {\n }\n" +
" }");
}

Expand All @@ -1220,8 +1218,7 @@ public void testAutoreleasePoolEnhancedForStatement() throws IOException {
" }" +
"}",
"Test", "Test.m");
assertTranslation(translation, "NSAutoreleasePool *pool__ = [[NSAutoreleasePool alloc] init]");
assertTranslation(translation, "[pool__ release]");
assertTranslation(translation, "@autoreleasepool");
}

public void testARCAutoreleasePoolForStatement() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,35 +150,6 @@ public void testLabeledBreakWithNonBlockParent() throws IOException {
assertTrue(((LabeledStatement) labelStmt).getBody() instanceof EmptyStatement);
}

private int methodCount(String source, String methodName, String[] paramTypes) {
CompilationUnit unit = translateType("Test", source);
List<?> types = unit.types();
assertEquals(1, types.size());
TypeDeclaration testType = (TypeDeclaration) types.get(0);
MethodDeclaration[] methods = testType.getMethods();
int nFooMethods = 0;
for (MethodDeclaration m : methods) {
if (m.getName().getIdentifier().equals(methodName) &&
parametersMatch(paramTypes, m.parameters())) {
nFooMethods++;
}
}
return nFooMethods;
}

private boolean parametersMatch(String[] paramTypes, List<?> parameters) {
if (parameters.size() != paramTypes.length) {
return false;
}
for (int i = 0; i < paramTypes.length; i++) {
SingleVariableDeclaration param = (SingleVariableDeclaration) parameters.get(i);
if (!param.getType().toString().equals(paramTypes[i])) {
return false;
}
}
return true;
}

/**
* Verifies that abstract methods to implement an interface are added to an
* abstract class.
Expand Down

0 comments on commit 5bcaadc

Please sign in to comment.