@@ -44,20 +44,6 @@ public class OpenJarEntryInEditor extends AbstractHandler implements IHandler {
4444
4545 private static final String JAR_URI_PARAM = "jarUri" ;
4646
47- record JarUri (IPath jar , IPath path ) {}
48-
49- private static JarUri createJarUri (URI uri ) {
50- if (!"jar" .equals (uri .getScheme ())) {
51- throw new IllegalArgumentException ();
52- }
53- String s = uri .getSchemeSpecificPart ();
54- int idx = s .indexOf ('!' );
55- if (idx <= 0 ) {
56- throw new IllegalArgumentException ();
57- }
58- return new JarUri (new Path (URI .create (s .substring (0 , idx )).getPath ()), new Path (s .substring (idx + 1 )));
59- }
60-
6147 @ Override
6248 public Object execute (ExecutionEvent event ) throws ExecutionException {
6349 String projectName = event .getParameter (OpenJavaElementInEditor .PROJECT_NAME );
@@ -69,12 +55,12 @@ public Object execute(ExecutionEvent event) throws ExecutionException {
6955 URI uri = URI .create (jarUriStr );
7056 IJavaProject javaProject = JavaCore .create (project );
7157 if (javaProject != null ) {
72- JarUri jarUri = createJarUri (uri );
73- Object inputElement = findJavaObj (javaProject , jarUri ).orElseGet (() -> {
74- try {
75- JarURLConnection c = (JarURLConnection ) uri .toURL ().openConnection ();
58+ try {
59+ final JarURLConnection c = (JarURLConnection ) uri .toURL ().openConnection ();
60+ Object inputElement = findJavaObj (javaProject , c ).orElseGet (() -> {
7661 return new IStorage () {
7762
63+ @ SuppressWarnings ("unchecked" )
7864 @ Override
7965 public <T > T getAdapter (Class <T > adapter ) {
8066 if (URI .class .equals (adapter )) {
@@ -129,11 +115,6 @@ public String toString() {
129115
130116
131117 };
132- } catch (IOException e ) {
133- LanguageServerCommonsActivator .getInstance ().getLog ().log (new Status (IStatus .ERROR ,
134- LanguageServerCommonsActivator .PLUGIN_ID , "Cannot load JAR entry: " + uri ));
135- return null ;
136- }
137118 });
138119 if (inputElement != null ) {
139120 try {
@@ -142,6 +123,11 @@ public String toString() {
142123 LanguageServerCommonsActivator .getInstance ().getLog ().log (e .getStatus ());
143124 }
144125 }
126+ } catch (IOException e ) {
127+ LanguageServerCommonsActivator .getInstance ().getLog ().log (new Status (IStatus .ERROR ,
128+ LanguageServerCommonsActivator .PLUGIN_ID , "Cannot load JAR entry: " + uri ));
129+ return null ;
130+ }
145131 } else {
146132 LanguageServerCommonsActivator .getInstance ().getLog ().log (new Status (IStatus .WARNING ,
147133 LanguageServerCommonsActivator .PLUGIN_ID , "Cannot find project: " + projectName ));
@@ -151,20 +137,23 @@ public String toString() {
151137 return null ;
152138 }
153139
154- private static Optional <Object > findJavaObj (IJavaProject j , JarUri uri ) {
140+ private static Optional <Object > findJavaObj (IJavaProject j , JarURLConnection c ) {
155141 try {
142+ IPath jarPath = new Path (c .getJarFileURL ().getPath ());
143+ // jar entry name doesn't start with '/' but the JarEntryResource full path does start from '/'
144+ IPath entryPath = new Path ("/" + c .getEntryName ());
156145 for (IPackageFragmentRoot fr : j .getAllPackageFragmentRoots ()) {
157- if (uri . jar () .equals (fr .getPath ())) {
146+ if (jarPath .equals (fr .getPath ())) {
158147 for (Object o : fr .getNonJavaResources ()) {
159148 if (o instanceof IJarEntryResource je ) {
160- return findJarEntry (je , uri . path () );
149+ return findJarEntry (je , entryPath );
161150 }
162151 }
163- if ("class" .equals (uri . path () .getFileExtension ())) {
164- String packageName = uri . path () .removeLastSegments (1 ).toString ().replace (IPath .SEPARATOR , '.' );
152+ if ("class" .equals (entryPath .getFileExtension ())) {
153+ String packageName = entryPath .removeLastSegments (1 ).toString ().replace (IPath .SEPARATOR , '.' );
165154 IPackageFragment pkg = fr .getPackageFragment (packageName );
166155 if (pkg != null ) {
167- IClassFile cf = pkg .getClassFile (uri . path () .lastSegment ());
156+ IClassFile cf = pkg .getClassFile (entryPath .lastSegment ());
168157 if (cf != null ) {
169158 return Optional .of (cf );
170159 }
0 commit comments