Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 67c6169

Browse files
committed
Merge pull request #60 from google/programmable-api
Programmable api
2 parents fbc0542 + 3d94f71 commit 67c6169

File tree

9 files changed

+290
-181
lines changed

9 files changed

+290
-181
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2016 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.classyshark;
18+
19+
import com.google.classyshark.silverghost.contentreader.ContentReader;
20+
import com.google.classyshark.silverghost.translator.Translator;
21+
import com.google.classyshark.silverghost.translator.TranslatorFactory;
22+
import com.google.classyshark.silverghost.translator.dex.DexMethodsDumper;
23+
import com.google.classyshark.silverghost.translator.dex.DexStringsDumper;
24+
import java.io.File;
25+
import java.util.LinkedList;
26+
import java.util.List;
27+
28+
/**
29+
* The ClassyShark API usually used by build & continues integration toolchains
30+
*/
31+
public class Shark {
32+
33+
private File archiveFile;
34+
35+
private Shark(File archiveFile) {
36+
this.archiveFile = archiveFile;
37+
}
38+
39+
public static Shark with(File archiveFile) {
40+
return new Shark(archiveFile);
41+
}
42+
43+
/**
44+
* @param className class name to generate such as "com.bumptech.glide.request.target.BaseTarget"
45+
* @return
46+
*/
47+
public String getGeneratedClass(String className) {
48+
String result;
49+
ContentReader loader = new ContentReader(archiveFile);
50+
loader.load();
51+
52+
Translator translator =
53+
TranslatorFactory.createTranslator(className, archiveFile,
54+
loader.getAllClassNames());
55+
try {
56+
translator.apply();
57+
result = translator.toString();
58+
} catch (NullPointerException npe) {
59+
System.out.println("Class doesn't exist in the writeArchive");
60+
return new String();
61+
}
62+
return result;
63+
}
64+
65+
/**
66+
* @return list of class names
67+
*/
68+
public List<String> getAllClassNames() {
69+
ContentReader loader = new ContentReader(archiveFile);
70+
loader.load();
71+
return loader.getAllClassNames();
72+
}
73+
74+
/**
75+
* @return manifest
76+
*/
77+
public String getManifest() {
78+
if (!archiveFile.getName().endsWith(".apk")) {
79+
return new String();
80+
}
81+
Translator translator =
82+
TranslatorFactory.createTranslator("AndroidManifest.xml", archiveFile);
83+
translator.apply();
84+
return translator.toString();
85+
}
86+
87+
/**
88+
* @return all methods
89+
*/
90+
public List<String> getAllMethods() {
91+
if (!archiveFile.getName().endsWith(".apk")) {
92+
return new LinkedList<>();
93+
}
94+
List<String> allMethods = DexMethodsDumper.dumpMethods(archiveFile);
95+
return allMethods;
96+
}
97+
98+
/**
99+
* @return all strings from all string tables
100+
*/
101+
public List<String> getAllStrings() {
102+
if (!archiveFile.getName().endsWith(".apk")) {
103+
return new LinkedList<>();
104+
}
105+
106+
List<String> allStrings = DexStringsDumper.dumpStrings(archiveFile);
107+
return allStrings;
108+
}
109+
110+
public static void main(String[] args) {
111+
File apk =
112+
new File("/Users/bfarber/Desktop/Scenarios/4 APKs/"
113+
+ "com.google.samples.apps.iosched-333.apk");
114+
Shark shark = Shark.with(apk);
115+
116+
System.out.println(
117+
shark.getGeneratedClass("com.bumptech.glide.request.target.BaseTarget"));
118+
System.out.println(shark.getAllClassNames());
119+
System.out.println(shark.getManifest());
120+
System.out.println(shark.getAllMethods());
121+
System.out.println(shark.getAllStrings());
122+
}
123+
}

ClassySharkWS/src/com/google/classyshark/cli/CliMode.java

+19-32
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package com.google.classyshark.cli;
1818

19-
import com.google.classyshark.gui.panel.io.Export2FileWriter;
19+
import com.google.classyshark.silverghost.exporter.Exporter;
2020
import com.google.classyshark.silverghost.contentreader.ContentReader;
2121
import com.google.classyshark.silverghost.translator.Translator;
2222
import com.google.classyshark.silverghost.translator.TranslatorFactory;
@@ -41,71 +41,58 @@ public static void with(List<String> args) {
4141

4242
final String operand = args.get(0).toLowerCase();
4343
switch (operand) {
44-
case "-dump":
44+
case "-export":
4545
if (args.size() == 2) {
46-
dumpApk(args);
46+
exportArchive(args);
4747
} else {
48-
dumpClassFromApk(args);
48+
exportClassFromApk(args);
4949
}
5050
break;
51-
case "-stringdump":
52-
dumpStrings(args);
53-
break;
5451
case "-inspect":
55-
processApk(args);
52+
inspectApk(args);
5653
break;
5754
default:
5855
System.err.println("wrong operand ==> " + operand);
5956
}
6057
}
6158

62-
private static void dumpStrings(List<String> args) {
63-
try {
64-
Export2FileWriter.writeAllDexStringTables(new File(args.get(1)));
65-
Export2FileWriter.writeAllMethodNames(new File(args.get(1)));
66-
} catch (Exception e) {
67-
System.out.println("Internal error - couldn't write file");
68-
}
69-
}
70-
71-
private static void dumpApk(List<String> args) {
72-
ContentReader loader = new ContentReader(new File(args.get(1)));
59+
private static void exportArchive(List<String> args) {
60+
File apk = new File(args.get(1));
61+
ContentReader loader = new ContentReader(apk);
7362
loader.load();
7463

7564
try {
76-
File binaryArchive = new File(args.get(1));
77-
Export2FileWriter.writeAllClassNames(loader.getAllClassNames(),
78-
binaryArchive);
79-
Export2FileWriter.writeManifest(binaryArchive);
80-
Export2FileWriter.writeAllDexStringTables(binaryArchive);
65+
Exporter.writeArchive(apk, loader.getAllClassNames());
8166
} catch (Exception e) {
8267
System.out.println("Internal error - couldn't write file");
8368
}
8469
}
8570

86-
private static void dumpClassFromApk(List<String> args) {
87-
ContentReader loader = new ContentReader(new File(args.get(1)));
71+
private static void exportClassFromApk(List<String> args) {
72+
File apk = new File(args.get(1));
73+
String className = args.get(2);
74+
75+
ContentReader loader = new ContentReader(apk);
8876
loader.load();
8977

9078
Translator translator =
91-
TranslatorFactory.createTranslator(args.get(2),
92-
new File(args.get(1)), loader.getAllClassNames());
93-
79+
TranslatorFactory.createTranslator(className, apk,
80+
loader.getAllClassNames());
9481
try {
9582
translator.apply();
9683
} catch (NullPointerException npe) {
97-
System.out.println("Class doesn't exist in the archive");
84+
System.out.println("Class doesn't exist in the writeArchive");
9885
return;
9986
}
10087

10188
try {
102-
Export2FileWriter.writeCurrentClass(translator);
89+
Exporter.writeCurrentClass(translator);
10390
} catch (Exception e) {
10491
System.out.println("Internal error - couldn't write file");
10592
}
10693
}
10794

108-
private static void processApk(List<String> args) {
95+
private static void inspectApk(List<String> args) {
10996
if (!new File(args.get(1)).getName().endsWith(".apk")) {
11097
System.out.println("Not an apk file ==> " +
11198
"java -jar ClassyShark.jar " + "-inspect APK_FILE");

ClassySharkWS/src/com/google/classyshark/gui/panel/ClassySharkPanel.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import com.google.classyshark.gui.panel.chart.RingChartPanel;
2020
import com.google.classyshark.gui.panel.displayarea.DisplayArea;
2121
import com.google.classyshark.gui.panel.io.CurrentFolderConfig;
22-
import com.google.classyshark.gui.panel.io.Export2FileWriter;
22+
import com.google.classyshark.silverghost.exporter.Exporter;
2323
import com.google.classyshark.gui.panel.io.FileChooserUtils;
2424
import com.google.classyshark.gui.panel.io.RecentArchivesConfig;
2525
import com.google.classyshark.gui.panel.methodscount.MethodsCountPanel;
@@ -192,12 +192,8 @@ public void onExportButtonPressed() {
192192
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>() {
193193
@Override
194194
protected Void doInBackground() throws Exception {
195-
Export2FileWriter.writeAllClassNames(reducer.getAllClassNames(),
196-
binaryArchive);
197-
Export2FileWriter.writeCurrentClass(translator);
198-
Export2FileWriter.writeAllClassNames(reducer.getAllClassNames(),
199-
binaryArchive);
200-
Export2FileWriter.writeAllDexStringTables(binaryArchive);
195+
Exporter.writeCurrentClass(translator);
196+
Exporter.writeArchive(binaryArchive, reducer.getAllClassNames());
201197
return null;
202198
}
203199

ClassySharkWS/src/com/google/classyshark/gui/panel/displayarea/DisplayArea.java

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,7 @@ public void displayAllClassesNames(List<String> classNames) {
219219
e.printStackTrace();
220220
}
221221

222+
// TODO not sure, remove the line below
222223
jTextPane.setDocument(blank);
223224

224225
System.out.println("UI update " + (System.currentTimeMillis() - start) + " ms");
@@ -250,6 +251,8 @@ public void displayClass(String classString) {
250251
jTextPane.setDocument(doc);
251252
}
252253

254+
// TODO add here logic fo highlighter
255+
// TODO by adding flag to Translator.ELEMENT
253256
public void displayClass(List<Translator.ELEMENT> elements) {
254257
displayDataState = DisplayDataState.INSIDE_CLASS;
255258
clearText();

ClassySharkWS/src/com/google/classyshark/gui/panel/io/Export2FileWriter.java

-100
This file was deleted.

0 commit comments

Comments
 (0)