List of usage examples for java.lang Class cast
@SuppressWarnings("unchecked") @HotSpotIntrinsicCandidate public T cast(Object obj)
From source file:net.minecraftforge.common.util.EnumHelper.java
private static <T extends Enum<?>> T makeEnum(Class<T> enumClass, @Nullable String value, int ordinal, Class<?>[] additionalTypes, @Nullable Object[] additionalValues) throws Exception { int additionalParamsCount = additionalValues == null ? 0 : additionalValues.length; Object[] params = new Object[additionalParamsCount + 2]; params[0] = value;/*from w w w . j a v a 2 s. c om*/ params[1] = ordinal; if (additionalValues != null) { System.arraycopy(additionalValues, 0, params, 2, additionalValues.length); } return enumClass.cast( newInstance.invoke(getConstructorAccessor(enumClass, additionalTypes), new Object[] { params })); }
From source file:com.kegare.caveworld.util.CaveUtils.java
public static <T extends Entity> T createEntity(Class<T> clazz, World world) { try {//w ww . ja v a 2s.c o m String name = String.valueOf(EntityList.classToStringMapping.get(clazz)); Entity entity = EntityList.createEntityByName(Strings.nullToEmpty(name), world); if (entity == null || entity.getClass() != clazz) { return null; } return clazz.cast(entity); } catch (Exception e) { CaveLog.warning("Failed to create entity: %s", clazz.getSimpleName()); return null; } }
From source file:eu.europa.ec.fisheries.uvms.exchange.search.SearchFieldMapper.java
public static <T> T buildValueFromClassType(SearchValue entry, Class<T> valueType) { StringBuilder builder = new StringBuilder(); try {//from w w w .j a v a2 s. c o m if (valueType.isAssignableFrom(String.class)) { if (entry.getValue().contains("*")) { String value = entry.getValue().replace("*", "%"); builder.append("'").append(value).append("'"); } else { builder.append(entry.getValue()); } return valueType.cast(builder.toString()); } else if (valueType.isAssignableFrom(Boolean.class)) { if ("TRUE".equalsIgnoreCase(entry.getValue()) || "T".equalsIgnoreCase(entry.getValue())) { return valueType.cast(Boolean.TRUE); } else { return valueType.cast(Boolean.FALSE); } } else if (valueType.isAssignableFrom(Date.class)) { return valueType.cast(DateUtils.parseToUTCDateTime(entry.getValue())); } else if (valueType.isAssignableFrom(Integer.class)) { return valueType.cast(Integer.valueOf(entry.getValue())); } else if (valueType.isAssignableFrom(TypeRefType.class)) { return valueType.cast(TypeRefType.valueOf(entry.getValue())); } else if (valueType.isAssignableFrom(ExchangeLogStatusTypeType.class)) { return valueType.cast(ExchangeLogStatusTypeType.valueOf(entry.getValue())); } return valueType.cast(entry.getValue()); } catch (ClassCastException cce) { LOG.error("Error casting parameter: " + entry.getField().getFieldName() + " having value: " + entry.getValue(), cce); return null; } }
From source file:hudson.security.SecurityRealm.java
/** * Picks up the instance of the given type from the spring context. * If there are multiple beans of the same type or if there are none, * this method treats that as an {@link IllegalArgumentException}. * * This method is intended to be used to pick up a Acegi object from * spring once the bean definition file is parsed. *///ww w . ja va 2 s .c o m public static <T> T findBean(Class<T> type, ApplicationContext context) { Map m = context.getBeansOfType(type); switch (m.size()) { case 0: throw new IllegalArgumentException("No beans of " + type + " are defined"); case 1: return type.cast(m.values().iterator().next()); default: throw new IllegalArgumentException("Multiple beans of " + type + " are defined: " + m); } }
From source file:org.apache.hadoop.fs.s3a.TestS3AConfiguration.java
/** * Reads and returns a field from an object using reflection. If the field * cannot be found, is null, or is not the expected type, then this method * fails the test.//from ww w. j av a2 s. co m * * @param target object to read * @param fieldType type of field to read, which will also be the return type * @param fieldName name of field to read * @return field that was read * @throws IllegalAccessException if access not allowed */ private static <T> T getField(Object target, Class<T> fieldType, String fieldName) throws IllegalAccessException { Object obj = FieldUtils.readField(target, fieldName, true); assertNotNull(String.format("Could not read field named %s in object with class %s.", fieldName, target.getClass().getName()), obj); assertTrue(String.format("Unexpected type found for field named %s, expected %s, actual %s.", fieldName, fieldType.getName(), obj.getClass().getName()), fieldType.isAssignableFrom(obj.getClass())); return fieldType.cast(obj); }
From source file:org.apache.taverna.databundle.DataBundles.java
private static <T> Stream<T> resolveItemAsStream(Path path, Class<T> type, ResolveOptions options) throws UncheckedIOException { try {/*from ww w.ja v a2 s.co m*/ Object value = resolve(path, options); if (type.isInstance(value)) { return Stream.of(type.cast(value)); } return Stream.empty(); } catch (IOException e) { throw new UncheckedIOException(e); } }
From source file:gov.nasa.ensemble.common.CommonUtils.java
/** * Returns a new list of the items in list, casting all of them to the provided class. So, it will throw a ClassCastException if any of the items can not be cast to the provided class. * //from w w w .j a va 2s . co m * @param klass * @param list * @return a new list of the items in list */ public static final <T> List<T> castList(Class<T> klass, List list, boolean unmodifiable) { if (list == null) return null; List<T> result = new ArrayList<T>(list.size()); for (Object object : list) { result.add(klass.cast(object)); } return unmodifiable ? Collections.unmodifiableList(result) : result; }
From source file:gov.nasa.ensemble.common.CommonUtils.java
public static <T> T getAdapter(Object adaptable, Class<T> adapterClass) { if (adaptable == null) { return null; }//from ww w. j a v a 2 s .c o m if (adaptable instanceof IAdaptable) { IAdaptable iAdaptable = (IAdaptable) adaptable; T adapter = adapterClass.cast(iAdaptable.getAdapter(adapterClass)); if (adapter != null) { return adapter; } } IAdapterManager manager = Platform.getAdapterManager(); T adapter = adapterClass.cast(manager.loadAdapter(adaptable, adapterClass.getName())); return adapter; }
From source file:com.wms.utils.DataUtil.java
public static <T> T getMapValue(Map<String, Object> params, String key, Class<T> type) { Object obj = params.get(key); if (obj == null) { return null; }//from w w w .j a va2 s . c o m if (obj.getClass().isAssignableFrom(obj.getClass())) { return type.cast(obj); } return null; }
From source file:org.apache.brooklyn.rest.client.BrooklynApi.java
public static <T> T getEntity(Response response, Class<T> type) { if (response instanceof ClientResponse) { ClientResponse<?> clientResponse = (ClientResponse<?>) response; return clientResponse.getEntity(type); } else if (response instanceof BuiltResponse) { // Handle BuiltResponsePreservingError turning objects into Strings if (response.getEntity() instanceof String && !type.equals(String.class)) { return new Gson().fromJson(response.getEntity().toString(), type); }/*from w w w .ja v a 2 s . c o m*/ } // Last-gasp attempt. return type.cast(response.getEntity()); }