本文整理汇总了Java中com.google.common.base.Defaults.defaultValue方法的典型用法代码示例。如果您正苦于以下问题:Java Defaults.defaultValue方法的具体用法?Java Defaults.defaultValue怎么用?Java Defaults.defaultValue使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.base.Defaults
的用法示例。
在下文中一共展示了Defaults.defaultValue方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleInvocation
import com.google.common.base.Defaults; //导入方法依赖的package包/类
@Override
protected Object handleInvocation(List<String> path, Method method) throws Throwable
{
if (method.getName().equals("values"))
{
return path.isEmpty()
? values
: nextImmutable.getInPath(values, path);
}
final List<String> pathWithMethod = pathWith(method);
final Class<?> returnType = method.getReturnType();
final Object value = nextImmutable.getInPath(values, pathWithMethod);
final Object returnValue = value == null
? Defaults.defaultValue(returnType)
: value;
final boolean isCastable = returnValue == null || isAssignable(returnValue.getClass(), returnType);
return isCastable
? returnValue
: immutableFor(returnType, pathWithMethod);
}
示例2: doUnwrap
import com.google.common.base.Defaults; //导入方法依赖的package包/类
private static <T> Object doUnwrap(Exchanging<T> exch, T... target) {
if (null == target) {
return null;
}
Class<?> clazz = Primitives.unwrap(target.getClass().getComponentType());
Object r = Array.newInstance(clazz, target.length);
if (0 == target.length) {
return r;
}
T el = null;
Object defaultVal = Defaults.defaultValue(clazz);
for (int i = 0; i < target.length; i++) {
Array.set(r, i, (null == (el = target[i])) ? defaultVal : exch.unwraps(el));
}
return r;
}
示例3: invoke
import com.google.common.base.Defaults; //导入方法依赖的package包/类
public Object invoke(final Object proxy,
final Method m,
final Object[] args) throws Throwable {
Object result = null;
try {
result = m.invoke(target,
args);
} catch (Exception e) {
if (errorCallBack != null) {
errorCallBack.error(result,
e);
}
if (m.getReturnType().isPrimitive()) {
return Defaults.defaultValue(m.getReturnType());
} else {
return result;
}
}
if (successCallBack != null) {
successCallBack.callback(result);
}
return result;
}
示例4: resetTransientValues
import com.google.common.base.Defaults; //导入方法依赖的package包/类
/**
* this nullifies and resets transient values that are supposed not to be stored
*
* @param zeObject The object where non transient fields will be nullified.
*/
void resetTransientValues(@Nullable Object zeObject) {
if (zeObject != null) {
Field[] fields = zeObject.getClass().getDeclaredFields();
for (Field field : fields) {
// ignore jacoco injected field
if (Modifier.isTransient(field.getModifiers()) && !field.getName().endsWith("jacocoData")) { //$NON-NLS-1$
Object defaultValue = Defaults.defaultValue(field.getType());
field.setAccessible(true);
try {
field.set(zeObject, defaultValue);
} catch (IllegalArgumentException | IllegalAccessException e) {
LOG.error("failed to reset the transient field [" + field + "] before storage", e);
}
}
}
} // else null so do nothing
}
示例5: allBuilderMethodsForwarded
import com.google.common.base.Defaults; //导入方法依赖的package包/类
@Test
public void allBuilderMethodsForwarded() throws Exception {
for (Method method : ManagedChannelBuilder.class.getDeclaredMethods()) {
if (Modifier.isStatic(method.getModifiers()) || Modifier.isPrivate(method.getModifiers())) {
continue;
}
Class<?>[] argTypes = method.getParameterTypes();
Object[] args = new Object[argTypes.length];
for (int i = 0; i < argTypes.length; i++) {
args[i] = Defaults.defaultValue(argTypes[i]);
}
method.invoke(testChannelBuilder, args);
method.invoke(verify(mockDelegate), args);
}
}
示例6: allBuilderMethodsReturnThis
import com.google.common.base.Defaults; //导入方法依赖的package包/类
@Test
public void allBuilderMethodsReturnThis() throws Exception {
for (Method method : ManagedChannelBuilder.class.getDeclaredMethods()) {
if (Modifier.isStatic(method.getModifiers()) || Modifier.isPrivate(method.getModifiers())) {
continue;
}
if (method.getName().equals("build")) {
continue;
}
Class<?>[] argTypes = method.getParameterTypes();
Object[] args = new Object[argTypes.length];
for (int i = 0; i < argTypes.length; i++) {
args[i] = Defaults.defaultValue(argTypes[i]);
}
Object returnedValue = method.invoke(testChannelBuilder, args);
assertThat(returnedValue).isSameAs(testChannelBuilder);
}
}
示例7: invoke
import com.google.common.base.Defaults; //导入方法依赖的package包/类
@Override
public Object invoke( Object proxy, Method method, Object[] args )
throws Throwable
{
ParameterProperty curProperty = properties.getProperty( method );
if ( properties.getIdProperty() == curProperty )
{
// for primitives we need to return something, so return default
if ( curProperty.isPrimitiveType() )
{
return Defaults.defaultValue( Primitives.unwrap( curProperty.getType() ) );
}
else
{
return null;
}
}
else
{
throw new IllegalArgumentException(
"Only can perform nested query on id field, but was " + curProperty );
}
}
示例8: createDefaultParamValue
import com.google.common.base.Defaults; //导入方法依赖的package包/类
private Object createDefaultParamValue(Class<?> clazz)
{
if (clazz.isPrimitive())
return Defaults.defaultValue(clazz);
if (byte[].class.equals(clazz))
return "FOO".getBytes();
return null;
}
示例9: getDefaultPrimitiveValue
import com.google.common.base.Defaults; //导入方法依赖的package包/类
/**
*
* @param type
* @return the default primitive value as a String. Returns null if unable to determine default value
*/
private String getDefaultPrimitiveValue(TypeName type) {
String valueString = null;
try {
Class<?> primitiveClass = Primitives.unwrap(Class.forName(type.box().toString()));
if (primitiveClass != null) {
Object defaultValue = Defaults.defaultValue(primitiveClass);
if (defaultValue != null) {
valueString = defaultValue.toString();
if (!Strings.isNullOrEmpty(valueString)) {
switch (type.toString()) {
case "double":
valueString = valueString + "d";
break;
case "float":
valueString = valueString + "f";
break;
case "long":
valueString = valueString + "L";
break;
case "char":
valueString = "'" + valueString + "'";
break;
}
}
}
}
} catch (ClassNotFoundException ignored) {
//Swallow and return null
}
return valueString;
}
示例10: getDefaultValue
import com.google.common.base.Defaults; //导入方法依赖的package包/类
@Nullable
private Object getDefaultValue(@NotNull Class<?> type) {
if (type == Optional.class) {
// If it's Guava optional then handle it as an absent value
return Optional.absent();
} else if (type.isPrimitive()) {
// If parameter is a primitive set the appropriate default value
return Defaults.defaultValue(type);
}
return null;
}
示例11: getProxy
import com.google.common.base.Defaults; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public <T> T getProxy(TypeToken<T> type) {
Class<? super T> cls = type.getRawType();
if (isTerminal(type)) {
return (T) Defaults.defaultValue(cls);
}
Enhancer e = new Enhancer();
if (Enhancer.isEnhanced(cls)) {
e.setSuperclass(cls.getSuperclass());
e.setInterfaces(cls.getInterfaces());
} else {
e.setSuperclass(cls);
}
e.setCallbackFilter(FINALIZE_FILTER);
e.setCallbacks(new Callback[] { NoOp.INSTANCE, new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
invocations.add(new MethodInvocation<Object>(type, method, Arrays.asList(args)));
return getProxy(type.resolveType(method.getGenericReturnType()));
}
} });
try {
return (T) e.create();
} catch (Exception ex) {
throw new RuntimeException("Error while creating proxy of " + type, ex);
}
}
示例12: coerce
import com.google.common.base.Defaults; //导入方法依赖的package包/类
@Override
public Object coerce(Object obj, Type type) {
if (obj == null) return Defaults.defaultValue(clazzFor(type));
Class<? extends Object> wrappedType = Primitives.wrap(clazzFor(type));
Coercer coercer = primitiveCoercers.get(wrappedType);
return coercer.coerce(obj, wrappedType);
}
示例13: getDefault
import com.google.common.base.Defaults; //导入方法依赖的package包/类
/**
* Returns a default value for the method based upon {@code @Default} metadata on the getter
* to return values. If there is no {@code @Default} annotation on the getter, then a <a
* href="https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html">default</a> as
* per the Java Language Specification for the expected return type is returned.
*
* @param proxy The proxy object for which we are attempting to get the default.
* @param method The getter method that was invoked.
* @return The default value from an {@link Default} annotation if present, otherwise a default
* value as per the Java Language Specification.
*/
@SuppressWarnings({"unchecked", "rawtypes"})
private Object getDefault(PipelineOptions proxy, Method method) {
if (method.getReturnType().equals(RuntimeValueProvider.class)) {
throw new RuntimeException(String.format(
"Method %s should not have return type "
+ "RuntimeValueProvider, use ValueProvider instead.", method.getName()));
}
if (method.getReturnType().equals(StaticValueProvider.class)) {
throw new RuntimeException(String.format(
"Method %s should not have return type "
+ "StaticValueProvider, use ValueProvider instead.", method.getName()));
}
@Nullable Object defaultObject = null;
for (Annotation annotation : method.getAnnotations()) {
defaultObject = returnDefaultHelper(annotation, proxy, method);
if (defaultObject != null) {
break;
}
}
if (method.getReturnType().equals(ValueProvider.class)) {
String propertyName = gettersToPropertyNames.get(method.getName());
return defaultObject == null
? new RuntimeValueProvider(
method.getName(), propertyName,
(Class<? extends PipelineOptions>) method.getDeclaringClass(),
proxy.getOptionsId())
: new RuntimeValueProvider(
method.getName(), propertyName,
(Class<? extends PipelineOptions>) method.getDeclaringClass(),
defaultObject, proxy.getOptionsId());
} else if (defaultObject != null) {
return defaultObject;
}
/*
* We need to make sure that we return something appropriate for the return type. Thus we return
* a default value as defined by the JLS.
*/
return Defaults.defaultValue(method.getReturnType());
}
示例14: primitiveIf
import com.google.common.base.Defaults; //导入方法依赖的package包/类
@Override protected Object primitiveIf() {
return Defaults.defaultValue(this.clazz);
}
示例15: eightWrapIf
import com.google.common.base.Defaults; //导入方法依赖的package包/类
@Override protected Object eightWrapIf() {
return Defaults.defaultValue(Primitives.unwrap(this.clazz));
}