Skip to content

Commit 85cad91

Browse files
committed
BridJ: moved BridJ.open(URL), .open(File) and .show(File) to Platform class
1 parent 8f2a163 commit 85cad91

4 files changed

Lines changed: 105 additions & 99 deletions

File tree

libraries/Runtime/BridJ/src/main/java/org/bridj/BridJ.java

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
* }</pre>
3838
* </li><li>You can also register a class explicitely with {@link BridJ#register(java.lang.Class)}
3939
* </li><li>To alter the name of a library, use {@link BridJ#setNativeLibraryActualName(String, String)} and {@link BridJ#addNativeLibraryAlias(String, String)}
40-
* </li><li>To open files and URLs in a platform-specific way, use {@link BridJ#open(File)}, {@link BridJ#open(URL)}, {@link BridJ#show(File)}
4140
* </li>
4241
* </ul>
4342
* @author ochafik
@@ -681,96 +680,4 @@ public static void main(String[] args) {
681680
System.exit(1);
682681
}
683682
}
684-
685-
/**
686-
* Opens an URL with the default system action.
687-
* @param url url to open
688-
* @throws NoSuchMethodException if opening an URL on the current platform is not supported
689-
*/
690-
public static final void open(URL url) throws NoSuchMethodException {
691-
if (url.getProtocol().equals("file")) {
692-
open(new File(url.getFile()));
693-
} else {
694-
if (Platform.isMacOSX()) {
695-
execArgs("open", url.toString());
696-
} else if (Platform.isWindows()) {
697-
execArgs("rundll32", "url.dll,FileProtocolHandler", url.toString());
698-
} else if (Platform.isUnix() && hasUnixCommand("gnome-open")) {
699-
execArgs("gnome-open", url.toString());
700-
} else if (Platform.isUnix() && hasUnixCommand("konqueror")) {
701-
execArgs("konqueror", url.toString());
702-
} else if (Platform.isUnix() && hasUnixCommand("mozilla")) {
703-
execArgs("mozilla", url.toString());
704-
} else {
705-
throw new NoSuchMethodException("Cannot open urls on this platform");
706-
}
707-
}
708-
}
709-
710-
/**
711-
* Opens a file with the default system action.
712-
* @param file file to open
713-
* @throws NoSuchMethodException if opening a file on the current platform is not supported
714-
*/
715-
public static final void open(File file) throws NoSuchMethodException {
716-
if (Platform.isMacOSX()) {
717-
execArgs("open", file.getAbsolutePath());
718-
} else if (Platform.isWindows()) {
719-
if (file.isDirectory()) {
720-
execArgs("explorer", file.getAbsolutePath());
721-
} else {
722-
execArgs("start", file.getAbsolutePath());
723-
}
724-
}
725-
if (Platform.isUnix() && hasUnixCommand("gnome-open")) {
726-
execArgs("gnome-open", file.toString());
727-
} else if (Platform.isUnix() && hasUnixCommand("konqueror")) {
728-
execArgs("konqueror", file.toString());
729-
} else if (Platform.isSolaris() && file.isDirectory()) {
730-
execArgs("/usr/dt/bin/dtfile", "-folder", file.getAbsolutePath());
731-
} else {
732-
throw new NoSuchMethodException("Cannot open files on this platform");
733-
}
734-
}
735-
736-
/**
737-
* Show a file in its parent directory, if possible selecting the file (not possible on all platforms).
738-
* @param file file to show in the system's default file navigator
739-
* @throws NoSuchMethodException if showing a file on the current platform is not supported
740-
*/
741-
public static final void show(File file) throws NoSuchMethodException, IOException {
742-
if (Platform.isWindows()) {
743-
exec("explorer /e,/select,\"" + file.getCanonicalPath() + "\"");
744-
} else {
745-
open(file.getAbsoluteFile().getParentFile());
746-
}
747-
}
748-
749-
static final void execArgs(String... cmd) throws NoSuchMethodException {
750-
try {
751-
Runtime.getRuntime().exec(cmd);
752-
} catch (Exception ex) {
753-
ex.printStackTrace();
754-
throw new NoSuchMethodException(ex.toString());
755-
}
756-
}
757-
758-
static final void exec(String cmd) throws NoSuchMethodException {
759-
try {
760-
Runtime.getRuntime().exec(cmd).waitFor();
761-
} catch (Exception ex) {
762-
ex.printStackTrace();
763-
throw new NoSuchMethodException(ex.toString());
764-
}
765-
}
766-
767-
static final boolean hasUnixCommand(String name) {
768-
try {
769-
Process p = Runtime.getRuntime().exec(new String[]{"which", name});
770-
return p.waitFor() == 0;
771-
} catch (Exception ex) {
772-
ex.printStackTrace();
773-
return false;
774-
}
775-
}
776683
}

libraries/Runtime/BridJ/src/main/java/org/bridj/Platform.java

Lines changed: 101 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.bridj;
22

33
import java.io.*;
4+
import java.net.URL;
45

56
import java.lang.reflect.Method;
67
import java.lang.reflect.Modifier;
@@ -13,7 +14,12 @@
1314
import static org.bridj.JNI.*;
1415

1516
/**
16-
* Methods that give information about the execution platform (OS, architecture, native sizes...)
17+
* Information about the execution platform (OS, architecture, native sizes...) and platform-specific actions.
18+
* <ul>
19+
* <li>To know if the JVM platform is 32 bits or 64 bits, use {@link Platform#is64Bits()}
20+
* </li><li>To know if the OS is an Unix-like system, use {@link Platform#isUnix()}
21+
* </li><li>To open files and URLs in a platform-specific way, use {@link Platform#open(File)}, {@link Platform#open(URL)}, {@link Platform#show(File)}
22+
* </li></ul>
1723
* @author ochafik
1824
*/
1925
public class Platform {
@@ -103,7 +109,7 @@ static String getEmbeddedLibraryResource(String name) {
103109
}
104110
throw new RuntimeException("Platform not supported ! (os.name='" + osName + "', os.arch='" + System.getProperty("os.arch") + "')");
105111
}
106-
public static File extractEmbeddedLibraryResource(String name) throws IOException {
112+
static File extractEmbeddedLibraryResource(String name) throws IOException {
107113
String libraryResource = getEmbeddedLibraryResource(name);
108114
int i = libraryResource.lastIndexOf('.');
109115
String ext = i < 0 ? "" : libraryResource.substring(i);
@@ -131,4 +137,97 @@ public static File extractEmbeddedLibraryResource(String name) throws IOExceptio
131137

132138
return libFile;
133139
}
140+
141+
142+
/**
143+
* Opens an URL with the default system action.
144+
* @param url url to open
145+
* @throws NoSuchMethodException if opening an URL on the current platform is not supported
146+
*/
147+
public static final void open(URL url) throws NoSuchMethodException {
148+
if (url.getProtocol().equals("file")) {
149+
open(new File(url.getFile()));
150+
} else {
151+
if (Platform.isMacOSX()) {
152+
execArgs("open", url.toString());
153+
} else if (Platform.isWindows()) {
154+
execArgs("rundll32", "url.dll,FileProtocolHandler", url.toString());
155+
} else if (Platform.isUnix() && hasUnixCommand("gnome-open")) {
156+
execArgs("gnome-open", url.toString());
157+
} else if (Platform.isUnix() && hasUnixCommand("konqueror")) {
158+
execArgs("konqueror", url.toString());
159+
} else if (Platform.isUnix() && hasUnixCommand("mozilla")) {
160+
execArgs("mozilla", url.toString());
161+
} else {
162+
throw new NoSuchMethodException("Cannot open urls on this platform");
163+
}
164+
}
165+
}
166+
167+
/**
168+
* Opens a file with the default system action.
169+
* @param file file to open
170+
* @throws NoSuchMethodException if opening a file on the current platform is not supported
171+
*/
172+
public static final void open(File file) throws NoSuchMethodException {
173+
if (Platform.isMacOSX()) {
174+
execArgs("open", file.getAbsolutePath());
175+
} else if (Platform.isWindows()) {
176+
if (file.isDirectory()) {
177+
execArgs("explorer", file.getAbsolutePath());
178+
} else {
179+
execArgs("start", file.getAbsolutePath());
180+
}
181+
}
182+
if (Platform.isUnix() && hasUnixCommand("gnome-open")) {
183+
execArgs("gnome-open", file.toString());
184+
} else if (Platform.isUnix() && hasUnixCommand("konqueror")) {
185+
execArgs("konqueror", file.toString());
186+
} else if (Platform.isSolaris() && file.isDirectory()) {
187+
execArgs("/usr/dt/bin/dtfile", "-folder", file.getAbsolutePath());
188+
} else {
189+
throw new NoSuchMethodException("Cannot open files on this platform");
190+
}
191+
}
192+
193+
/**
194+
* Show a file in its parent directory, if possible selecting the file (not possible on all platforms).
195+
* @param file file to show in the system's default file navigator
196+
* @throws NoSuchMethodException if showing a file on the current platform is not supported
197+
*/
198+
public static final void show(File file) throws NoSuchMethodException, IOException {
199+
if (Platform.isWindows()) {
200+
exec("explorer /e,/select,\"" + file.getCanonicalPath() + "\"");
201+
} else {
202+
open(file.getAbsoluteFile().getParentFile());
203+
}
204+
}
205+
206+
static final void execArgs(String... cmd) throws NoSuchMethodException {
207+
try {
208+
Runtime.getRuntime().exec(cmd);
209+
} catch (Exception ex) {
210+
ex.printStackTrace();
211+
throw new NoSuchMethodException(ex.toString());
212+
}
213+
}
214+
215+
static final void exec(String cmd) throws NoSuchMethodException {
216+
try {
217+
Runtime.getRuntime().exec(cmd).waitFor();
218+
} catch (Exception ex) {
219+
ex.printStackTrace();
220+
throw new NoSuchMethodException(ex.toString());
221+
}
222+
}
223+
224+
static final boolean hasUnixCommand(String name) {
225+
try {
226+
Process p = Runtime.getRuntime().exec(new String[]{"which", name});
227+
return p.waitFor() == 0;
228+
} catch (Exception ex) {
229+
ex.printStackTrace();
230+
return false;
231+
}
232+
}
134233
}

libraries/Runtime/BridJ/src/main/java/org/bridj/demangling/Demangler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ public boolean matches(Method method) {
260260
try {
261261
if (ref != null) {
262262
boolean res = ref.matches(method);
263-
if (!res) {
264-
System.err.println("Symbol " + symbol + " was a good candidate but expected demangled signature " + ref + " did not match the method " + method);
263+
if (!res && BridJ.debug) {
264+
System.err.println("Symbol " + symbol + " was a good candidate but expected demangled signature " + ref + " did not match the method " + method);
265265
}
266266
return res;
267267
}
@@ -281,7 +281,7 @@ void parse() {
281281
} catch (DemanglingException ex) {
282282
if (BridJ.verbose)
283283
ex.printStackTrace();
284-
BridJ.log(Level.WARNING, "Symbol parsing failed : " + ex.getMessage(), ex);
284+
BridJ.log(Level.WARNING, "Symbol parsing failed : " + ex.getMessage());
285285
}
286286
refParsed = true;
287287
}

libraries/Runtime/BridJ/src/main/java/org/bridj/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
BridJ core classes and C runtime ({@link org.bridj.Pointer}, {@link org.bridj.BridJ}, {@link org.bridj.SizeT}...).
2+
BridJ core classes and C runtime ({@link org.bridj.Pointer}, {@link org.bridj.BridJ}, {@link org.bridj.SizeT}, {@link org.bridj.Platform}...).
33
<p>
44
See <a href="http://code.google.com/p/bridj/wiki/FAQ">BridJ's wiki</a> for more information.
55
*/

0 commit comments

Comments
 (0)