Skip to content

Commit 5040333

Browse files
committed
Remove all of the ext stuff
1 parent b3a800b commit 5040333

File tree

10 files changed

+98
-916
lines changed

10 files changed

+98
-916
lines changed

android/src/cucumber/api/android/CucumberInstrumentation.java

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,18 @@
1212
import cucumber.runtime.RuntimeOptions;
1313
import cucumber.runtime.android.AndroidFormatter;
1414
import cucumber.runtime.android.AndroidObjectFactory;
15-
import cucumber.runtime.android.AndroidReflections;
1615
import cucumber.runtime.android.AndroidResourceLoader;
16+
import cucumber.runtime.android.DexReflections;
1717
import cucumber.runtime.io.Reflections;
1818
import cucumber.runtime.io.ResourceLoader;
1919
import cucumber.runtime.java.JavaBackend;
2020
import cucumber.runtime.model.*;
21-
import ext.android.test.ClassPathPackageInfoSource;
21+
import dalvik.system.DexFile;
2222
import gherkin.formatter.Formatter;
2323
import gherkin.formatter.Reporter;
2424
import gherkin.formatter.model.*;
2525

26+
import java.io.IOException;
2627
import java.lang.annotation.Annotation;
2728
import java.lang.reflect.Method;
2829
import java.util.ArrayList;
@@ -53,7 +54,7 @@ public class CucumberInstrumentation extends Instrumentation {
5354
public void onCreate(Bundle arguments) {
5455
super.onCreate(arguments);
5556

56-
if(arguments == null) {
57+
if (arguments == null) {
5758
throw new CucumberException("No arguments");
5859
}
5960
Log.d(TAG, "************** KEYS **************");
@@ -73,16 +74,14 @@ class => cucumber.android.test.CucumberActivitySteps
7374
*/
7475

7576

76-
7777
// Maven: [log, coverage, coverageFile, debug]
7878
// IDEA: [log, coverage, coverageFile, debug]
7979

8080
Context context = getContext();
8181
classLoader = context.getClassLoader();
8282

8383
String apkPath = context.getPackageCodePath();
84-
ClassPathPackageInfoSource.setApkPaths(new String[]{apkPath});
85-
ClassPathPackageInfoSource source = new ClassPathPackageInfoSource();
84+
Reflections reflections = new DexReflections(newDexFile(apkPath));
8685

8786
// For glue and features either use the provided arguments or try to find a RunWithCucumber annotated class.
8887
// If nothing works, default values will be used instead.
@@ -112,8 +111,7 @@ class => cucumber.android.test.CucumberActivitySteps
112111
Log.w(TAG, e.toString());
113112
}
114113
} else {
115-
// throw new CucumberException("bad args");
116-
for (Class<?> clazz : source.getPackageInfo(context.getPackageName()).getTopLevelClassesRecursive()) {
114+
for (Class<?> clazz : reflections.getDescendants(Object.class, context.getPackageName())) {
117115
if (readRunWithCucumberAnnotation(clazz)) break;
118116
}
119117
}
@@ -127,15 +125,21 @@ class => cucumber.android.test.CucumberActivitySteps
127125

128126
resourceLoader = new AndroidResourceLoader(context);
129127

130-
Reflections androidReflections = new AndroidReflections(source);
131-
132128
List<Backend> backends = new ArrayList<Backend>();
133-
backends.add(new JavaBackend(new AndroidObjectFactory(this), androidReflections));
129+
backends.add(new JavaBackend(new AndroidObjectFactory(this), reflections));
134130
runtime = new Runtime(resourceLoader, classLoader, backends, runtimeOptions);
135131

136132
start();
137133
}
138134

135+
private DexFile newDexFile(String apkPath) {
136+
try {
137+
return new DexFile(apkPath);
138+
} catch (IOException e) {
139+
throw new CucumberException("Failed to open " + apkPath);
140+
}
141+
}
142+
139143
/**
140144
* @return true if the class is RunWithCucumber annotated, false otherwise
141145
*/

android/src/cucumber/runtime/android/AndroidReflections.java

Lines changed: 0 additions & 43 deletions
This file was deleted.

android/src/cucumber/runtime/android/AndroidResourceLoader.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
import java.util.ArrayList;
1111
import java.util.List;
1212

13+
/**
14+
* Loads non-class resources such as .feature files.
15+
*/
1316
public class AndroidResourceLoader implements ResourceLoader {
1417
private Context context;
1518

@@ -25,20 +28,20 @@ public Iterable<Resource> resources(String path, String suffix) {
2528
addResourceRecursive(resources, assetManager, path, suffix);
2629
return resources;
2730
} catch (IOException e) {
28-
throw new CucumberException("Error loading resources.", e);
31+
throw new CucumberException("Error loading resources from " + path + " with suffix " + suffix, e);
2932
}
3033
}
3134

32-
private void addResourceRecursive(List<Resource> res, AssetManager am, String path, String suffix) throws IOException {
35+
private void addResourceRecursive(List<Resource> resources, AssetManager assetManager, String path, String suffix) throws IOException {
3336
if (path.endsWith(suffix)) {
34-
res.add(new AndroidResource(context, path));
37+
resources.add(new AndroidResource(context, path));
3538
} else {
36-
for (String name : am.list(path)) {
39+
for (String name : assetManager.list(path)) {
3740
if (name.endsWith(suffix)) {
3841
Resource as = new AndroidResource(context, String.format("%s/%s", path, name));
39-
res.add(as);
42+
resources.add(as);
4043
} else {
41-
addResourceRecursive(res, am, String.format("%s/%s", path, name), suffix);
44+
addResourceRecursive(resources, assetManager, String.format("%s/%s", path, name), suffix);
4245
}
4346
}
4447
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package cucumber.runtime.android;
2+
3+
import cucumber.runtime.CucumberException;
4+
import cucumber.runtime.io.Reflections;
5+
import dalvik.system.DexFile;
6+
7+
import java.lang.annotation.Annotation;
8+
import java.util.ArrayList;
9+
import java.util.Collection;
10+
import java.util.Enumeration;
11+
import java.util.List;
12+
13+
public class DexReflections implements Reflections {
14+
private static final ClassLoader CLASS_LOADER = DexReflections.class.getClassLoader();
15+
private final DexFile dexFile;
16+
17+
public DexReflections(DexFile dexFile) {
18+
this.dexFile = dexFile;
19+
}
20+
21+
@Override
22+
public Collection<Class<? extends Annotation>> getAnnotations(String packageName) {
23+
return getDescendants(Annotation.class, packageName);
24+
}
25+
26+
@Override
27+
public <T> Collection<Class<? extends T>> getDescendants(Class<T> parentType, String packageName) {
28+
List<Class<? extends T>> result = new ArrayList<Class<? extends T>>();
29+
30+
Enumeration<String> entries = dexFile.entries();
31+
while (entries.hasMoreElements()) {
32+
String className = entries.nextElement();
33+
if (isInPackage(className, packageName) && !isGenerated(className)) {
34+
Class<? extends T> clazz = loadClass(className);
35+
if (clazz != null && !parentType.equals(clazz) && parentType.isAssignableFrom(clazz)) {
36+
result.add(clazz.asSubclass(parentType));
37+
}
38+
}
39+
}
40+
return result;
41+
}
42+
43+
@SuppressWarnings("unchecked")
44+
private <T> Class<? extends T> loadClass(String className) {
45+
try {
46+
return (Class<? extends T>) Class.forName(className, false, CLASS_LOADER);
47+
} catch (ClassNotFoundException e) {
48+
throw new CucumberException(e);
49+
}
50+
}
51+
52+
private boolean isInPackage(String className, String packageName) {
53+
int lastDotIndex = className.lastIndexOf(".");
54+
String classPackage = lastDotIndex == -1 ? "" : className.substring(0, lastDotIndex);
55+
return classPackage.startsWith(packageName);
56+
}
57+
58+
private boolean isGenerated(String className) {
59+
int lastDotIndex = className.lastIndexOf(".");
60+
String shortName = lastDotIndex == -1 ? className : className.substring(lastDotIndex + 1);
61+
return shortName.equals("Manifest") || shortName.equals("R") || shortName.startsWith("R$");
62+
}
63+
64+
65+
@Override
66+
public <T> T instantiateExactlyOneSubclass(Class<T> parentType, String packageName, Class[] constructorParams, Object[] constructorArgs) {
67+
throw new UnsupportedOperationException();
68+
}
69+
70+
@Override
71+
public <T> Collection<? extends T> instantiateSubclasses(Class<T> parentType, String packageName, Class[] constructorParams, Object[] constructorArgs) {
72+
throw new UnsupportedOperationException();
73+
}
74+
}

android/src/ext/android/test/ClassPathPackageInfo.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

0 commit comments

Comments
 (0)