334 335 private static Class<?> simpleType(final String typeName) throws ClassNotFoundException { 336 final Class<?> primClass = TypeUtilities.getPrimitiveTypeByName(typeName); 337 return primClass != null ? primClass : Global.getThisContext().findClass(typeName); 338 } 339 340 private static Class<?> arrayType(final String typeName) throws ClassNotFoundException { 341 return Array.newInstance(type(typeName.substring(0, typeName.length() - 2)), 0).getClass(); 342 } 343 344 /** 345 * Returns a type object for a subclass of the specified Java class (or implementation of the specified interface) 346 * that acts as a script-to-Java adapter for it. See {@link #type(Object, Object)} for a discussion of type objects, 347 * and see {@link JavaAdapterFactory} for details on script-to-Java adapters. Note that you can also implement 348 * interfaces and subclass abstract classes using {@code new} operator on a type object for an interface or abstract 349 * class. However, to extend a non-abstract class, you will have to use this method. Example: 350 * <pre> 351 * var ArrayList = Java.type("java.util.ArrayList") 352 * var ArrayListExtender = Java.extend(ArrayList) 353 * var printSizeInvokedArrayList = new ArrayListExtender() { 354 * function size() { print("size invoked!"); } 355 * } 356 * var printAddInvokedArrayList = new ArrayListExtender() { 357 * function add(x, y) { 358 * if(typeof(y) === "undefined") { 359 * print("add(e) invoked!"); 360 * } else { 361 * print("add(i, e) invoked!"); 362 * } 363 * } 364 * </pre> 365 * We can see several important concepts in the above example: 366 * <ul> 367 * <li>Every Java class will have exactly one extender subclass in Nashorn - repeated invocations of {@code extend} 368 * for the same type will yield the same extender type. It's a generic adapter that delegates to whatever JavaScript 369 * functions its implementation object has on a per-instance basis.</li> 370 * <li>If the Java method is overloaded (as in the above example {@code List.add()}), then your JavaScript adapter 371 * must be prepared to deal with all overloads.</li> 372 * <li>You can't invoke {@code super.*()} from adapters for now.</li> 373 * @param self not used 374 * @param types the original types. The caller must pass at least one Java type object of class {@link StaticClass} 375 * representing either a public interface or a non-final public class with at least one public or protected 376 * constructor. If more than one type is specified, at most one can be a class and the rest have to be interfaces. 377 * Invoking the method twice with exactly the same types in the same order will return the same adapter | 334 335 private static Class<?> simpleType(final String typeName) throws ClassNotFoundException { 336 final Class<?> primClass = TypeUtilities.getPrimitiveTypeByName(typeName); 337 return primClass != null ? primClass : Global.getThisContext().findClass(typeName); 338 } 339 340 private static Class<?> arrayType(final String typeName) throws ClassNotFoundException { 341 return Array.newInstance(type(typeName.substring(0, typeName.length() - 2)), 0).getClass(); 342 } 343 344 /** 345 * Returns a type object for a subclass of the specified Java class (or implementation of the specified interface) 346 * that acts as a script-to-Java adapter for it. See {@link #type(Object, Object)} for a discussion of type objects, 347 * and see {@link JavaAdapterFactory} for details on script-to-Java adapters. Note that you can also implement 348 * interfaces and subclass abstract classes using {@code new} operator on a type object for an interface or abstract 349 * class. However, to extend a non-abstract class, you will have to use this method. Example: 350 * <pre> 351 * var ArrayList = Java.type("java.util.ArrayList") 352 * var ArrayListExtender = Java.extend(ArrayList) 353 * var printSizeInvokedArrayList = new ArrayListExtender() { 354 * size: function() { print("size invoked!"); } 355 * } 356 * var printAddInvokedArrayList = new ArrayListExtender() { 357 * add: function(x, y) { 358 * if(typeof(y) === "undefined") { 359 * print("add(e) invoked!"); 360 * } else { 361 * print("add(i, e) invoked!"); 362 * } 363 * } 364 * </pre> 365 * We can see several important concepts in the above example: 366 * <ul> 367 * <li>Every Java class will have exactly one extender subclass in Nashorn - repeated invocations of {@code extend} 368 * for the same type will yield the same extender type. It's a generic adapter that delegates to whatever JavaScript 369 * functions its implementation object has on a per-instance basis.</li> 370 * <li>If the Java method is overloaded (as in the above example {@code List.add()}), then your JavaScript adapter 371 * must be prepared to deal with all overloads.</li> 372 * <li>You can't invoke {@code super.*()} from adapters for now.</li> 373 * @param self not used 374 * @param types the original types. The caller must pass at least one Java type object of class {@link StaticClass} 375 * representing either a public interface or a non-final public class with at least one public or protected 376 * constructor. If more than one type is specified, at most one can be a class and the rest have to be interfaces. 377 * Invoking the method twice with exactly the same types in the same order will return the same adapter |