Skip to content

Commit

Permalink
prevent NullPointerException in UIUtils.getPathLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
jpstotz committed Jul 12, 2024
1 parent 99853e7 commit 4c073ef
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,30 @@ public static JavaDecompilerPlugin getDefault() {
}

public static void logError(Throwable t, String message) {
JavaDecompilerPlugin.getDefault().getLog().log(new Status(IStatus.ERROR, JavaDecompilerConstants.PLUGIN_ID, 0, message, t));
JavaDecompilerPlugin.getDefault().getLog()
.log(new Status(IStatus.ERROR, JavaDecompilerConstants.PLUGIN_ID, 0, message, t));
}

public static void logWarn(Throwable t, String message) {
JavaDecompilerPlugin.getDefault().getLog()
.log(new Status(IStatus.WARNING, JavaDecompilerConstants.PLUGIN_ID, 0, message, t));
}

public static void logInfo(String message) {
JavaDecompilerPlugin.getDefault().getLog().log(new Status(IStatus.INFO, JavaDecompilerConstants.PLUGIN_ID, 0, message, null));
JavaDecompilerPlugin.getDefault().getLog()
.log(new Status(IStatus.INFO, JavaDecompilerConstants.PLUGIN_ID, 0, message, null));
}

/**
*
* @param severity severity constant see {@link IStatus#INFO}
* {@link IStatus#WARNING} {@link IStatus#ERROR}
* @param t
* @param message
*/
public static void log(int severity, Throwable t, String message) {
JavaDecompilerPlugin.getDefault().getLog().log(new Status(severity, JavaDecompilerConstants.PLUGIN_ID, 0, message, t));
JavaDecompilerPlugin.getDefault().getLog()
.log(new Status(severity, JavaDecompilerConstants.PLUGIN_ID, 0, message, t));
}

public static ImageDescriptor getImageDescriptor(String path) {
Expand Down Expand Up @@ -130,7 +145,8 @@ protected void initializeDefaultPreferences(IPreferenceStore store) {
store.setDefault(JavaDecompilerConstants.CLASS_FILE_ATTR_SHOW_VARIABLE_TABLE, false);
store.setDefault(JavaDecompilerConstants.CLASS_FILE_ATTR_SHOW_EXCEPTION_TABLE, false);
store.setDefault(JavaDecompilerConstants.CLASS_FILE_ATTR_SHOW_MAXS, false);
store.setDefault(JavaDecompilerConstants.BRANCH_TARGET_ADDRESS_RENDERING, JavaDecompilerConstants.BRANCH_TARGET_ADDRESS_RELATIVE);
store.setDefault(JavaDecompilerConstants.BRANCH_TARGET_ADDRESS_RENDERING,
JavaDecompilerConstants.BRANCH_TARGET_ADDRESS_RELATIVE);
store.setDefault(JavaDecompilerConstants.CLASS_FILE_ATTR_RENDER_TRYCATCH_BLOCKS, true);
store.setDefault(JavaDecompilerConstants.CLASS_FILE_ATTR_SHOW_SOURCE_LINE_NUMBERS, true);
store.setDefault(JavaDecompilerConstants.CLASS_FILE_ATTR_SHOW_MAXS, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
Expand Down Expand Up @@ -92,18 +93,18 @@ public void run() {
}

public static JavaDecompilerClassFileEditor getActiveDecompilerEditor() {
final JavaDecompilerClassFileEditor[] editors = new JavaDecompilerClassFileEditor[1];
final AtomicReference<JavaDecompilerClassFileEditor> editors = new AtomicReference<>();
Display.getDefault().syncExec(new Runnable() {

@Override
public void run() {
IWorkbenchPart editor = getActiveEditor(true);
if (editor instanceof JavaDecompilerClassFileEditor) {
editors[0] = (JavaDecompilerClassFileEditor) editor;
editors.set((JavaDecompilerClassFileEditor) editor);
}
}
});
return editors[0];
return editors.get();
}

public static List getActiveSelection() {
Expand All @@ -116,38 +117,33 @@ public static List getActiveSelection() {
}

public static String getActivePerspectiveId() {
final String[] ids = new String[1];
final AtomicReference<String> ids = new AtomicReference<>();
Display.getDefault().syncExec(new Runnable() {

@Override
public void run() {
IWorkbench wb = PlatformUI.getWorkbench();
if (wb == null) {
ids[0] = null;
return;
}

IWorkbenchWindow win = wb.getActiveWorkbenchWindow();
if (win == null) {
ids[0] = null;
return;
}
IWorkbenchPage page = win.getActivePage();
if (page == null) {
ids[0] = null;
return;
}

IPerspectiveDescriptor perspective = page.getPerspective();
if (perspective == null) {
ids[0] = null;
return;
}
ids[0] = perspective.getId();

ids.set(perspective.getId());
}
});
return ids[0];
return ids.get();
}

public static boolean isDebug() {
Expand Down Expand Up @@ -231,8 +227,7 @@ public static boolean isOpenClassEditor() {
IWorkbenchWindow[] windows = PlatformUI.getWorkbench().getWorkbenchWindows();

if (windows != null) {
for (int i = 0; i < windows.length; i++) {
IWorkbenchWindow window = windows[i];
for (IWorkbenchWindow window : windows) {
for (IWorkbenchPage pg : window.getPages()) {
if (pg == null) {
continue;
Expand Down Expand Up @@ -260,8 +255,9 @@ public static List getExportSelections() {
return selectedJars;
}

if (selectedJars.size() > 1)
if (selectedJars.size() > 1) {
return null;
}

final List selectedPackages = getSelectedElements(window.getSelectionService(), IPackageFragment.class);
final List selectedClasses = getSelectedElements(window.getSelectionService(), IClassFile.class);
Expand All @@ -278,7 +274,8 @@ public static boolean isPackageFlat() {
try {
IWorkbenchPart view = getActiveEditor(true);
if (view != null) {
if (view.getSite().getId().equals("org.eclipse.ui.navigator.ProjectExplorer")) //$NON-NLS-1$
String id = view.getSite().getId();
if (id.equals("org.eclipse.ui.navigator.ProjectExplorer")) //$NON-NLS-1$
{
CommonNavigator explorer = (CommonNavigator) view;
Field field = CommonNavigator.class.getDeclaredField("commonManager"); //$NON-NLS-1$
Expand All @@ -294,10 +291,12 @@ public static boolean isPackageFlat() {
isFlat = model.getBooleanProperty(Values.IS_LAYOUT_FLAT);
}
}
} else if (view.getSite().getId().equals("org.eclipse.jdt.ui.PackageExplorer")) //$NON-NLS-1$
} else if (id.equals("org.eclipse.jdt.ui.PackageExplorer")) //$NON-NLS-1$
{
PackageExplorerPart explorer = (PackageExplorerPart) view;
isFlat = explorer.isFlatLayout();
} else {
JavaDecompilerPlugin.logWarn(null, "isPackageFlat: unsupported view: " + id);
}
}
} catch (Exception e) {
Expand All @@ -317,8 +316,15 @@ public static String getPathLocation(IPath path) {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IWorkspaceRoot root = workspace.getRoot();
IResource resource = root.findMember(path);
if (resource != null) {
return resource.getLocation().toOSString();
if (resource == null) {
JavaDecompilerPlugin.logWarn(null, "Failed to find member of path " + path);
} else {
IPath location = resource.getLocation();
if (location == null) {
JavaDecompilerPlugin.logWarn(null, "Failed to get location of path " + resource);
} else {
return location.toOSString();
}
}
return null;
}
Expand Down

0 comments on commit 4c073ef

Please sign in to comment.