Skip to content

Commit

Permalink
generics and code style
Browse files Browse the repository at this point in the history
  • Loading branch information
jpstotz committed Jul 7, 2024
1 parent 7c2ab3a commit 883fa76
Showing 1 changed file with 39 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -50,6 +51,8 @@

public class ExportSourceAction extends Action {

private static final String[] ZIP_FILTER = new String[] { "*.zip" };

private List selection = null;
private boolean isFlat = false;

Expand All @@ -63,8 +66,9 @@ public ExportSourceAction(List selection) {

@Override
public void run() {
if (selection == null || selection.isEmpty())
if (selection == null || selection.isEmpty()) {
return;
}

IPreferenceStore prefs = JavaDecompilerPlugin.getDefault().getPreferenceStore();
final String decompilerType = prefs.getString(JavaDecompilerConstants.DECOMPILER_TYPE);
Expand All @@ -84,8 +88,7 @@ public void run() {
fileName = fileName.substring(0, index);
}
dialog.setFileName(fileName + "-src"); //$NON-NLS-1$
dialog.setFilterExtensions(new String[] { "*.zip" //$NON-NLS-1$
});
dialog.setFilterExtensions(ZIP_FILTER);
String file = dialog.open();
if (file != null && file.trim().length() > 0) {
final String projectFile = file.trim();
Expand Down Expand Up @@ -115,8 +118,7 @@ public void run() {
fileName = fileName.substring(0, index);
}
dialog.setFileName(fileName + "-src"); //$NON-NLS-1$
dialog.setFilterExtensions(new String[] { "*.zip" //$NON-NLS-1$
});
dialog.setFilterExtensions(ZIP_FILTER);
String file = dialog.open();
if (file != null && file.trim().length() > 0) {
final String projectFile = file.trim();
Expand Down Expand Up @@ -209,13 +211,14 @@ private void exportPackageSources(IProgressMonitor monitor, final String decompi
+ "/export/" //$NON-NLS-1$
+ System.currentTimeMillis());

Map classes = new HashMap();
Map<IJavaElement, List<IJavaElement>> classesMap = new HashMap();
for (int i = 0; i < children.length; i++) {
if (monitor.isCanceled())
if (monitor.isCanceled()) {
return;
}
IJavaElement child = children[i];
try {
collectClasses(child, classes, monitor);
collectClasses(child, classesMap, monitor);
} catch (JavaModelException e) {
IStatus status = new Status(IStatus.ERROR, JavaDecompilerConstants.PLUGIN_ID,
Messages.getString("ExportSourceAction.Status.Error.CollectPackage"), //$NON-NLS-1$
Expand All @@ -226,23 +229,25 @@ private void exportPackageSources(IProgressMonitor monitor, final String decompi

monitor.worked(20000);

IPackageFragment[] pkgs = (IPackageFragment[]) classes.keySet().toArray(new IPackageFragment[0]);
IPackageFragment[] pkgs = classesMap.keySet().toArray(new IPackageFragment[0]);
int step = 880000 / pkgs.length;
for (int i = 0; i < pkgs.length; i++) {
if (monitor.isCanceled())
if (monitor.isCanceled()) {
return;
}
IPackageFragment pkg = pkgs[i];
List clazzs = (List) classes.get(pkg);
if (clazzs.size() == 0) {
List<IJavaElement> clazzList = classesMap.get(pkg);
if (clazzList.isEmpty()) {
monitor.worked(step);
continue;
}
int total = 0;
int classStep = step / clazzs.size();
for (int j = 0; j < clazzs.size(); j++) {
if (monitor.isCanceled())
int classStep = step / clazzList.size();
for (int j = 0; j < clazzList.size(); j++) {
if (monitor.isCanceled()) {
return;
IJavaElement clazz = (IJavaElement) clazzs.get(j);
}
IJavaElement clazz = clazzList.get(j);
if (clazz instanceof IClassFile && clazz.getParent() instanceof IPackageFragment) {
String className = pkg.getElementName();
if (pkg.getElementName().length() > 0) {
Expand All @@ -251,13 +256,15 @@ private void exportPackageSources(IProgressMonitor monitor, final String decompi
monitor.subTask(className);
try {
IClassFile cf = (IClassFile) clazz;
if (cf.getElementName().indexOf('$') != -1)
if (cf.getElementName().indexOf('$') != -1) {
continue;
}
String result = DecompileUtil.decompile(cf, decompilerType, always, reuseBuf, true);
if (result != null) {
String packageName = pkg.getElementName().replace('.', '/');
if (packageName.length() > 0)
if (packageName.length() > 0) {
packageName += "/"; //$NON-NLS-1$
}
FileUtil.writeToFile(
new File(workingDir, packageName + cf.getElementName().replaceAll("\\..+", "") //$NON-NLS-1$ //$NON-NLS-2$
+ ".java"), //$NON-NLS-1$
Expand Down Expand Up @@ -316,38 +323,36 @@ private void exportPackageSources(IProgressMonitor monitor, final String decompi
}
}

private void collectClasses(IJavaElement element, Map classes, IProgressMonitor monitor) throws JavaModelException {
private void collectClasses(IJavaElement element, Map<IJavaElement, List<IJavaElement>> classesMap,
IProgressMonitor monitor) throws JavaModelException {
if (element instanceof IPackageFragment) {
IPackageFragment pkg = (IPackageFragment) element;
if (!classes.containsKey(pkg)) {
if (!classesMap.containsKey(pkg)) {
monitor.subTask(pkg.getElementName());
List list = new ArrayList();
IJavaElement[] children = pkg.getChildren();
for (int i = 0; i < children.length; i++) {
list.add(children[i]);
}
classes.put(pkg, list);
List<IJavaElement> list = new ArrayList<>();
Collections.addAll(list, pkg.getChildren());
classesMap.put(pkg, list);
}
if (!isFlat) {
IPackageFragmentRoot root = (IPackageFragmentRoot) pkg.getParent();
IJavaElement[] children = root.getChildren();
for (int i = 0; i < root.getChildren().length; i++) {
IPackageFragment child = (IPackageFragment) children[i];
if (child.getElementName().startsWith(pkg.getElementName() + ".") //$NON-NLS-1$
&& !classes.containsKey(child)) {
collectClasses(child, classes, monitor);
&& !classesMap.containsKey(child)) {
collectClasses(child, classesMap, monitor);
}
}
}
} else if (element instanceof IClassFile) {
IPackageFragment pkg = (IPackageFragment) ((IClassFile) element).getParent();
if (!classes.containsKey(pkg)) {
if (!classesMap.containsKey(pkg)) {
monitor.subTask(pkg.getElementName());
List list = new ArrayList();
List<IJavaElement> list = new ArrayList<>();
list.add(element);
classes.put(pkg, list);
classesMap.put(pkg, list);
} else {
((List) classes.get(pkg)).add(element);
classesMap.get(pkg).add(element);
}
}
}
Expand All @@ -368,9 +373,9 @@ private void exportClass(String decompilerType, boolean reuseBuf, boolean always
String projectFile = file.trim();
try {
String result = DecompileUtil.decompile(cf, decompilerType, always, reuseBuf, true);
if (result != null)
if (result != null) {
FileUtil.writeToFile(new File(projectFile), result);
else {
} else {
IStatus status = new Status(IStatus.ERROR, JavaDecompilerConstants.PLUGIN_ID,
Messages.getFormattedString("ExportSourceAction.Status.Error.DecompileFailed", //$NON-NLS-1$
new String[] { className }));
Expand Down

0 comments on commit 883fa76

Please sign in to comment.