Java Code Examples for java.lang.reflect.Type.equals()

The following are Jave code examples for showing how to use equals() of the java.lang.reflect.Type class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: OffIt   File: MockableCallAdapter.java   View Source Code Vote up 6 votes
@Override
public Type responseType() {
    Type rawType = ((ParameterizedType) returnType).getRawType();
    if (rawType.equals(Call.class) || rawType.equals(MockableCall.class) || rawType.equals(com.gabrielsamojlo.offit.Call.class)) {
        Type[] types = ((ParameterizedType) returnType).getActualTypeArguments();
        Type paramType = types[0];
        if (paramType instanceof WildcardType) {
            return ((WildcardType) paramType).getUpperBounds()[0];
        }

        this.type = paramType;

        return paramType;
    } else {
        CallAdapter callAdapter = retrofit.nextCallAdapter(factory, returnType, annotations);
        return callAdapter.responseType();
    }
}
 
Example 2
Project: nifi-swagger-client   File: JSON.java   View Source Code Vote up 6 votes
/**
 * Deserialize the given JSON string to Java object.
 *
 * @param <T> Type
 * @param body The JSON string
 * @param returnType The type to deserialize into
 * @return The deserialized Java object
 */
@SuppressWarnings("unchecked")
public <T> T deserialize(String body, Type returnType) {
    try {
        if (apiClient.isLenientOnJson()) {
            JsonReader jsonReader = new JsonReader(new StringReader(body));
            // see https://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/stream/JsonReader.html#setLenient(boolean)
            jsonReader.setLenient(true);
            return gson.fromJson(jsonReader, returnType);
        } else {
            return gson.fromJson(body, returnType);
        }
    } catch (JsonParseException e) {
        // Fallback processing when failed to parse JSON form response body:
        //   return the response body string directly for the String return type;
        //   parse response body into date or datetime for the Date return type.
        if (returnType.equals(String.class))
            return (T) body;
        else if (returnType.equals(Date.class))
            return (T) apiClient.parseDateOrDatetime(body);
        else throw(e);
    }
}
 
Example 3
Project: unitimes   File: BinaryFileApiHelper.java   View Source Code Vote up 6 votes
@Override
public <P> P getRequest(Type requestType) throws IOException {
	if (requestType.equals(BinaryFile.class)) {
		InputStream is = iRequest.getInputStream();
		ByteArrayOutputStream buffer = new ByteArrayOutputStream();
		int read;
		byte[] data = new byte[16384];
		while ((read = is.read(data, 0, data.length)) != -1)
			buffer.write(data, 0, read);
		buffer.flush();
		BinaryFile output = new BinaryFile(buffer.toByteArray(), iRequest.getContentType(), getFileName());
		return (P)output;
	} else {
		return super.getRequest(requestType);
	}
}
 
Example 4
Project: ctsms   File: BasicAuthenticationProvider.java   View Source Code Vote up 5 votes
@Override
public Injectable<AuthenticationVO> getInjectable(ComponentContext ic, Context a, Type c) {
	if (c.equals(AuthenticationVO.class)) {
		return this;
	}
	return null;
}
 
Example 5
Project: odoo-work   File: TypeToken.java   View Source Code Vote up 5 votes
/**
 * Checks if two types are the same or are equivalent under a variable mapping
 * given in the type map that was provided.
 */
private static boolean matches(Type from, Type to, Map<String, Type> typeMap) {
  return to.equals(from)
      || (from instanceof TypeVariable
      && to.equals(typeMap.get(((TypeVariable<?>) from).getName())));

}
 
Example 6
Project: Retrofit2Demo   File: Example2Activity.java   View Source Code Vote up 5 votes
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    //不能直接用type==new TypeToken<List<Contributor>>(){}.getType()),将会得到false
    //因为==是用于判断两个引用变量是否相等,但是这里的==右边是new的一个新的,所以肯定是“不==”的
    Log.d("测试---》",""+(type==new TypeToken<List<Contributor>>(){}.getType()));
    if (type.equals(new TypeToken<List<Contributor>>(){}.getType())) {
        return CustomConverter.INSTANCE;
    }
    //其它类型我们不处理,返回null就行
    return null;
}
 
Example 7
Project: clingo4j   File: ASTObject.java   View Source Code Vote up 5 votes
default Class<T> getNativeClass() {
    Type[] genericInterfaces = getClass().getGenericInterfaces();
    for (int i=0; i<genericInterfaces.length; i++) {
        Type t = genericInterfaces[i];
        if (t instanceof ParameterizedType) {
            Type rt = ((ParameterizedType) t).getRawType();
            if (rt.equals(ASTObject.class)) {
                return (Class<T>) ((ParameterizedType) t).getActualTypeArguments()[0];
            }
        }
    }
    return null;
}
 
Example 8
Project: woodpecker   File: WoodpeckerHttpResponse.java   View Source Code Vote up 5 votes
public <T> void httpSuccess(String data) {
    if (peck.isResponseTypeStream()) {
        peck.getResponse().onSuccess(peck.getResponseStream());
        try {
            peck.getResponseStream().close();
        } catch (IOException e) {

        }
        return;
    }

    if (data == null) {
        peck.getResponse().onError(new WoodpeckerException(""));
    } else {
        WoodpeckerResponse response = peck.getResponse();
        T t;
        try {
            Type type = response.getType();
            if (type.equals(String.class) || peck.getType() == RequestType.HEAD) {
                response.onSuccess(data);
            } else {
                t = new Gson().fromJson(data, response.getType());
                response.onSuccess(t);
            }
        } catch (JsonSyntaxException jse) {
            response.onError(new WoodpeckerException(data, jse));
        }
    }
}
 
Example 9
Project: AndroidBasicLibs   File: GsonConverterFactory.java   View Source Code Vote up 5 votes
@Override
public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
    if (type != null && type.equals(String.class)) {
        return new JsonResponseBodyConverter<>();
    }
    TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type != null ? type : null));
    return new GsonResponseBodyConverter<>(gson, adapter);
}
 
Example 10
Project: lams   File: TypeToken.java   View Source Code Vote up 5 votes
/**
 * Checks if two types are the same or are equivalent under a variable mapping
 * given in the type map that was provided.
 */
private static boolean matches(Type from, Type to, Map<String, Type> typeMap) {
  return to.equals(from)
      || (from instanceof TypeVariable
      && to.equals(typeMap.get(((TypeVariable<?>) from).getName())));

}
 
Example 11
Project: minijax   File: MinijaxSwaggerTest.java   View Source Code Vote up 5 votes
@Test
public void returnProperBeanParam() throws NoSuchMethodException {
    final Method method = getClass().getDeclaredMethod("testRoute", BaseBean.class, ChildBean.class, RefBean.class, EnumBean.class, Integer.class);
    final List<Pair<Type, Annotation[]>> parameters = getParameters(method.getGenericParameterTypes(), method.getParameterAnnotations());

    for (final Pair<Type, Annotation[]> parameter : parameters) {
        final Type parameterType = parameter.first();
        final List<Parameter> swaggerParams = new MinijaxSwaggerExtension().extractParameters(Arrays.asList(parameter.second()),
                parameterType, new HashSet<Type>(), SwaggerExtensions.chain());
        // Ensure proper number of parameters returned
        if (parameterType.equals(BaseBean.class)) {
            assertEquals(2, swaggerParams.size());
        } else if (parameterType.equals(ChildBean.class)) {
            assertEquals(5, swaggerParams.size());
        } else if (parameterType.equals(RefBean.class)) {
            assertEquals(5, swaggerParams.size());
        } else if (parameterType.equals(EnumBean.class)) {
            assertEquals(1, swaggerParams.size());
            final HeaderParameter enumParam = (HeaderParameter) swaggerParams.get(0);
            assertEquals("string", enumParam.getType());
            final List<String> enumValues = new ArrayList<>(Collections2.transform(Arrays.asList(TestEnum.values()), Functions.toStringFunction()));
            assertEquals(enumValues, enumParam.getEnum());
        } else if (parameterType.equals(Integer.class)) {
            assertEquals(0, swaggerParams.size());
        } else {
            fail(String.format("Parameter of type %s was not expected", parameterType));
        }

        // Ensure the proper parameter type and name is returned (The rest is handled by pre-existing logic)
        for (final Parameter param : swaggerParams) {
            assertEquals(param.getClass().getSimpleName().replace("eter", ""), param.getName());
        }
    }
}
 
Example 12
Project: businessworks   File: MoreTypes.java   View Source Code Vote up 4 votes
/**
 * Returns true if {@code a} and {@code b} are equal.
 */
public static boolean equals(Type a, Type b) {
  if (a == b) {
    // also handles (a == null && b == null)
    return true;

  } else if (a instanceof Class) {
    // Class already specifies equals().
    return a.equals(b);

  } else if (a instanceof ParameterizedType) {
    if (!(b instanceof ParameterizedType)) {
      return false;
    }

    // TODO: save a .clone() call
    ParameterizedType pa = (ParameterizedType) a;
    ParameterizedType pb = (ParameterizedType) b;
    return Objects.equal(pa.getOwnerType(), pb.getOwnerType())
        && pa.getRawType().equals(pb.getRawType())
        && Arrays.equals(pa.getActualTypeArguments(), pb.getActualTypeArguments());

  } else if (a instanceof GenericArrayType) {
    if (!(b instanceof GenericArrayType)) {
      return false;
    }

    GenericArrayType ga = (GenericArrayType) a;
    GenericArrayType gb = (GenericArrayType) b;
    return equals(ga.getGenericComponentType(), gb.getGenericComponentType());

  } else if (a instanceof WildcardType) {
    if (!(b instanceof WildcardType)) {
      return false;
    }

    WildcardType wa = (WildcardType) a;
    WildcardType wb = (WildcardType) b;
    return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds())
        && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());

  } else if (a instanceof TypeVariable) {
    if (!(b instanceof TypeVariable)) {
      return false;
    }
    TypeVariable<?> va = (TypeVariable) a;
    TypeVariable<?> vb = (TypeVariable) b;
    return va.getGenericDeclaration().equals(vb.getGenericDeclaration())
        && va.getName().equals(vb.getName());

  } else {
    // This isn't a type we support. Could be a generic array type, wildcard type, etc.
    return false;
  }
}
 
Example 13
Project: hoard   File: Types.java   View Source Code Vote up 4 votes
/** Returns true if {@code a} and {@code b} are equal. */
private static boolean equals(Type a, Type b) {
  if (a == b) {
    return true; // Also handles (a == null && b == null).
  } else if (a instanceof Class) {
    return a.equals(b); // Class already specifies equals().
  } else if (a instanceof ParameterizedType) {
    if (!(b instanceof ParameterizedType)) return false;
    ParameterizedType pa = (ParameterizedType) a;
    ParameterizedType pb = (ParameterizedType) b;
    Type[] aTypeArguments = pa instanceof ParameterizedTypeImpl
      ? ((ParameterizedTypeImpl) pa).typeArguments
      : pa.getActualTypeArguments();
    Type[] bTypeArguments = pb instanceof ParameterizedTypeImpl
      ? ((ParameterizedTypeImpl) pb).typeArguments
      : pb.getActualTypeArguments();
    return equal(pa.getOwnerType(), pb.getOwnerType())
      && pa.getRawType().equals(pb.getRawType())
      && Arrays.equals(aTypeArguments, bTypeArguments);
  } else if (a instanceof GenericArrayType) {
    if (!(b instanceof GenericArrayType)) return false;
    GenericArrayType ga = (GenericArrayType) a;
    GenericArrayType gb = (GenericArrayType) b;
    return equals(ga.getGenericComponentType(), gb.getGenericComponentType());
  } else if (a instanceof WildcardType) {
    if (!(b instanceof WildcardType)) return false;
    WildcardType wa = (WildcardType) a;
    WildcardType wb = (WildcardType) b;
    return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds())
      && Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());
  } else if (a instanceof TypeVariable) {
    if (!(b instanceof TypeVariable)) return false;
    TypeVariable<?> va = (TypeVariable<?>) a;
    TypeVariable<?> vb = (TypeVariable<?>) b;
    return va.getGenericDeclaration() == vb.getGenericDeclaration()
      && va.getName().equals(vb.getName());
  } else {
    // This isn't a supported type.
    return false;
  }
}
 
Example 14
Project: incubator-servicecomb-java-chassis   File: CseResponseProducerResponseMapperFactory.java   View Source Code Vote up 4 votes
@Override
public boolean isMatch(Type swaggerType, Type producerType) {
  return producerType.equals(Response.class);
}
 
Example 15
Project: letv   File: TypeToken.java   View Source Code Vote up 4 votes
private static boolean matches(Type from, Type to, Map<String, Type> typeMap) {
    return to.equals(from) || ((from instanceof TypeVariable) && to.equals(typeMap.get(((TypeVariable) from).getName())));
}
 
Example 16
Project: OpenJSharp   File: ReflectionNavigator.java   View Source Code Vote up 4 votes
@Override
public boolean isSameType(Type t1, Type t2) {
    return t1.equals(t2);
}
 
Example 17
Project: openjdk-jdk10   File: ReflectionNavigator.java   View Source Code Vote up 4 votes
@Override
public boolean isSameType(Type t1, Type t2) {
    return t1.equals(t2);
}
 
Example 18
Project: pcloud-networking-java   File: Types.java   View Source Code Vote up 4 votes
/**
 * Returns true if {@code a} and {@code b} are equal.
 */
static boolean equals(Type a, Type b) {
    if (a == b) {
        return true; // Also handles (a == null && b == null).

    } else if (a instanceof Class) {
        return a.equals(b); // Class already specifies equals().

    } else if (a instanceof ParameterizedType) {
        if (!(b instanceof ParameterizedType)) return false;
        ParameterizedType pa = (ParameterizedType) a;
        ParameterizedType pb = (ParameterizedType) b;
        Type[] aTypeArguments = pa instanceof ParameterizedTypeImpl ?
                                        ((ParameterizedTypeImpl) pa).typeArguments :
                                        pa.getActualTypeArguments();
        Type[] bTypeArguments = pb instanceof ParameterizedTypeImpl ?
                                        ((ParameterizedTypeImpl) pb).typeArguments :
                                        pb.getActualTypeArguments();
        return equal(pa.getOwnerType(), pb.getOwnerType()) &&
                       pa.getRawType().equals(pb.getRawType()) &&
                       Arrays.equals(aTypeArguments, bTypeArguments);

    } else if (a instanceof GenericArrayType) {
        if (!(b instanceof GenericArrayType)) return false;
        GenericArrayType ga = (GenericArrayType) a;
        GenericArrayType gb = (GenericArrayType) b;
        return equals(ga.getGenericComponentType(), gb.getGenericComponentType());

    } else if (a instanceof WildcardType) {
        if (!(b instanceof WildcardType)) return false;
        WildcardType wa = (WildcardType) a;
        WildcardType wb = (WildcardType) b;
        return Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) &&
                       Arrays.equals(wa.getLowerBounds(), wb.getLowerBounds());

    } else if (a instanceof TypeVariable) {
        if (!(b instanceof TypeVariable)) return false;
        TypeVariable<?> va = (TypeVariable<?>) a;
        TypeVariable<?> vb = (TypeVariable<?>) b;
        return va.getGenericDeclaration() == vb.getGenericDeclaration() &&
                       va.getName().equals(vb.getName());

    } else {
        // This isn't a supported type.
        return false;
    }
}
 
Example 19
Project: ChronoBike   File: StringUtil.java   View Source Code Vote up 4 votes
public static String getUnprefixedMemberName(String csMemberName, String csPrefix, Type fieldType)
{
	if(csMemberName.startsWith(csPrefix))
	{
		csMemberName = csMemberName.substring(csPrefix.length());
		if(fieldType.equals(Integer.TYPE))
		{
			if(csMemberName.startsWith("n"))
				return csMemberName.substring(1);
		}
		else if(fieldType.equals(String.class))
		{
			if(csMemberName.startsWith("cs"))
				return csMemberName.substring(2);
		}
		else if(fieldType.equals(Double.TYPE))
		{
			if(csMemberName.startsWith("d"))
				return csMemberName.substring(1);
		}
		else if(fieldType.equals(Long.TYPE))
		{
			if(csMemberName.startsWith("l"))
				return csMemberName.substring(1);
		}
		else if(fieldType.equals(Short.TYPE))
		{
			if(csMemberName.startsWith("s"))
				return csMemberName.substring(1);
		}
		else if(fieldType.equals(java.sql.Date.class))
		{
			if(csMemberName.startsWith("dt"))
				return csMemberName.substring(2);
		}
		else if(fieldType.equals(Timestamp.class))
		{
			if(csMemberName.startsWith("ts"))
				return csMemberName.substring(2);
		}
	}
	return csMemberName;
}
 
Example 20
Project: boohee_v5.6   File: C$Gson$Types.java   View Source Code Vote up 4 votes
public static boolean equals(Type a, Type b) {
    boolean z = true;
    if (a == b) {
        return true;
    }
    if (a instanceof Class) {
        return a.equals(b);
    }
    if (a instanceof ParameterizedType) {
        if (!(b instanceof ParameterizedType)) {
            return false;
        }
        ParameterizedType pa = (ParameterizedType) a;
        ParameterizedType pb = (ParameterizedType) b;
        if (!(C$Gson$Types.equal(pa.getOwnerType(), pb.getOwnerType()) && pa.getRawType()
                .equals(pb.getRawType()) && Arrays.equals(pa.getActualTypeArguments(), pb
                .getActualTypeArguments()))) {
            z = false;
        }
        return z;
    } else if (a instanceof GenericArrayType) {
        if (!(b instanceof GenericArrayType)) {
            return false;
        }
        return C$Gson$Types.equals(((GenericArrayType) a).getGenericComponentType(), (
                (GenericArrayType) b).getGenericComponentType());
    } else if (a instanceof WildcardType) {
        if (!(b instanceof WildcardType)) {
            return false;
        }
        WildcardType wa = (WildcardType) a;
        WildcardType wb = (WildcardType) b;
        if (!(Arrays.equals(wa.getUpperBounds(), wb.getUpperBounds()) && Arrays.equals(wa
                .getLowerBounds(), wb.getLowerBounds()))) {
            z = false;
        }
        return z;
    } else if (!(a instanceof TypeVariable) || !(b instanceof TypeVariable)) {
        return false;
    } else {
        TypeVariable<?> va = (TypeVariable) a;
        TypeVariable<?> vb = (TypeVariable) b;
        if (!(va.getGenericDeclaration() == vb.getGenericDeclaration() && va.getName().equals
                (vb.getName()))) {
            z = false;
        }
        return z;
    }
}