本文整理汇总了Java中jdk.nashorn.internal.runtime.linker.Bootstrap.getLinkerServices方法的典型用法代码示例。如果您正苦于以下问题:Java Bootstrap.getLinkerServices方法的具体用法?Java Bootstrap.getLinkerServices怎么用?Java Bootstrap.getLinkerServices使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类jdk.nashorn.internal.runtime.linker.Bootstrap
的用法示例。
在下文中一共展示了Bootstrap.getLinkerServices方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: convert
import jdk.nashorn.internal.runtime.linker.Bootstrap; //导入方法依赖的package包/类
/**
* Convert the given object to the given type.
*
* @param obj object to be converted
* @param type destination type to convert to
* @return converted object
*/
public static Object convert(final Object obj, final Object type) {
if (obj == null) {
return null;
}
final Class<?> clazz;
if (type instanceof Class) {
clazz = (Class<?>)type;
} else if (type instanceof StaticClass) {
clazz = ((StaticClass)type).getRepresentedClass();
} else {
throw new IllegalArgumentException("type expected");
}
final LinkerServices linker = Bootstrap.getLinkerServices();
final Object objToConvert = unwrap(obj);
final MethodHandle converter = linker.getTypeConverter(objToConvert.getClass(), clazz);
if (converter == null) {
// no supported conversion!
throw new UnsupportedOperationException("conversion not supported");
}
try {
return converter.invoke(objToConvert);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
}
}
示例2: convert
import jdk.nashorn.internal.runtime.linker.Bootstrap; //导入方法依赖的package包/类
/**
* Convert the given object to the given type.
*
* @param obj object to be converted
* @param type destination type to convert to. type is either a Class
* or nashorn representation of a Java type returned by Java.type() call in script.
* @return converted object
*/
public static Object convert(final Object obj, final Object type) {
if (obj == null) {
return null;
}
final Class<?> clazz;
if (type instanceof Class) {
clazz = (Class<?>)type;
} else if (type instanceof StaticClass) {
clazz = ((StaticClass)type).getRepresentedClass();
} else {
throw new IllegalArgumentException("type expected");
}
final LinkerServices linker = Bootstrap.getLinkerServices();
final Object objToConvert = unwrap(obj);
final MethodHandle converter = linker.getTypeConverter(objToConvert.getClass(), clazz);
if (converter == null) {
// no supported conversion!
throw new UnsupportedOperationException("conversion not supported");
}
try {
return converter.invoke(objToConvert);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
}
}
示例3: convert
import jdk.nashorn.internal.runtime.linker.Bootstrap; //导入方法依赖的package包/类
/**
* Convert the given object to the given type.
*
* @param obj object to be converted
* @param type destination type to convert to. type is either a Class
* or nashorn representation of a Java type returned by Java.type() call in script.
* @return converted object
*/
public static Object convert(final Object obj, final Object type) {
if (obj == null) {
return null;
}
final Class<?> clazz;
if (type instanceof Class) {
clazz = (Class<?>)type;
} else if (type instanceof StaticClass) {
clazz = ((StaticClass)type).getRepresentedClass();
} else {
throw new IllegalArgumentException("type expected");
}
final LinkerServices linker = Bootstrap.getLinkerServices();
final Object objToConvert = unwrap(obj);
final MethodHandle converter = linker.getTypeConverter(objToConvert.getClass(), clazz);
if (converter == null) {
// no supported conversion!
throw new UnsupportedOperationException("conversion not supported");
}
try {
return converter.invoke(objToConvert);
} catch (final RuntimeException | Error e) {
throw e;
} catch (final Throwable t) {
throw new RuntimeException(t);
}
}