List of usage examples for java.lang Class isInstance
@HotSpotIntrinsicCandidate public native boolean isInstance(Object obj);
From source file:de.dfki.iui.mmds.dialogue.SiamContext.java
@SuppressWarnings({ "rawtypes", "unchecked" }) @Override// w ww .j ava 2 s. c o m public void setLocal(String name, Object value) { if (value != null && value.toString().equals("[data: null]") && has("stateID")) { // value is variable from scxml model String stateID = (String) get("stateID"); value = null; if (DialogueComponent.INSTANCE.idToScxmlState.containsKey(stateID)) { TransitionTarget tt = DialogueComponent.INSTANCE.idToScxmlState.get(stateID); State state = null; if (tt instanceof State) { state = (State) tt; } while (value == null && state != null) { // search for variable in scxml-model and set variable with // default value in context. for (Data data : state.getDatamodel().getData()) { if (data instanceof XData && ((XData) data).getId().equals(name)) { value = ((XData) data).getObject(); if (name.startsWith("_variable$")) { Object defaultValue = null; String variableName = name.split("\\$")[1]; Variable variable = (Variable) value; if (variable.getDefault() != null && !variable.getDefault().isEmpty()) { if (variable.getEmfType() != null && variable.getEmfType() instanceof EDataType) { defaultValue = variable.getEmfType().getEPackage().getEFactoryInstance() .createFromString((EDataType) variable.getEmfType(), variable.getDefault()); } else if (variable.getType() != null && !variable.getType().isEmpty()) { try { defaultValue = siamStateMachine.siamEvaluator.eval(this, variable.getDefault()); } catch (IllegalArgumentException | SecurityException | SCXMLExpressionException e) { final Writer result = new StringWriter(); final PrintWriter printWriter = new PrintWriter(result); e.printStackTrace(printWriter); Logger.getLogger(this.getClass()).warn(String.format( "Cannot create default variable value \"%s\" for variable \"%s\" of type %s!\n%s", variable.getDefault(), variable.getName(), variable.getType(), result.toString())); } if (defaultValue == null) { Logger.getLogger(this.getClass()).warn(String.format( "Cannot create default variable value \"%s\" for variable \"%s\" of type %s!", variable.getDefault(), variable.getName(), variable.getType())); } } else { Logger.getLogger(this.getClass()).warn(String.format( "Cannot create default value for variable \"%s\". The type %s is not serializable.", variable.getName(), variable.getEmfType())); } } super.setLocal(variableName, defaultValue); } break; } } // go up to containing state and search for variable here if (state.eContainer() instanceof State) { state = (State) state.eContainer(); } else { state = null; } } } } else { // type check. Convert BDataType to DataType or vice versa if // necessary. Variable variable = (Variable) get("_variable$" + name); if (variable != null && value != null) { Class variableInstanceClass; if (variable.getEmfType() != null) { variableInstanceClass = variable.getEmfType().getInstanceClass(); } else if (variable.getType() != null && !variable.getType().isEmpty()) { try { variableInstanceClass = DialogueComponent.INSTANCE.projectContext.getBundleContext() .getBundle().loadClass(variable.getType()); } catch (ClassNotFoundException e) { throw new IllegalArgumentException(String.format( "Cannot assign value to variable \"%s\" of type %s. Class \'%s\' does not exist", name, variable.getType(), variable.getType())); } } else throw new IllegalArgumentException( String.format("No type defined for variable \"%s\" in scope of state \"%s\".", variable.getName(), get("stateID"))); if (!(variableInstanceClass.isInstance(value)) && !areMatchingClassTypes(variableInstanceClass, value.getClass())) { boolean success = false; if (value instanceof BDataType) { ParameterizedType type = (ParameterizedType) value.getClass().getGenericSuperclass(); if (((Class) type.getActualTypeArguments()[0]).getName() .equals(variableInstanceClass.getName())) { if (!((BDataType) value).isResolved() && ((BDataType) value).getExpression().isEmpty()) { value = ""; } else { value = ((BDataType) value).getValue(); } success = true; } else if (((Class) type.getActualTypeArguments()[0]).getName() .equals(String.class.getName())) { if (BDataType.class.isAssignableFrom(variableInstanceClass)) { try { value = variableInstanceClass.getMethod("valueOf", String.class).invoke(null, value.toString()); success = true; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } else if (variableInstanceClass.getSuperclass() == BDataType.class) { BDataType newValue; try { newValue = (BDataType) variableInstanceClass.newInstance(); newValue.setValue(value); value = newValue; success = true; } catch (InstantiationException | IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassCastException e) { success = false; } } if (!success) { String message = String.format( "Cannot assign value of type %s to variable \"%s\" of type %s: Wrong type.", value.getClass().getName(), name, variableInstanceClass.getName()); Logger.getLogger(this.getClass()).error(message); throw new IllegalArgumentException(message); } } } } super.setLocal(name, value); }
From source file:com.wabacus.config.Config.java
public AbsButtonType getResourceButton(ReportRequest rrequest, IComponentConfigBean ccbean, String key, Class buttonType) { if (key == null) return null; if (Tools.isDefineKey("$", key)) { key = Tools.getRealKeyByDefine("$", key); }/*w w w . j a v a 2s . c o m*/ XmlElementBean eleBean = (XmlElementBean) resources.get(rrequest, ccbean.getPageBean(), key, true); if (eleBean == null) { throw new WabacusConfigLoadingException("" + key + "?"); } AbsButtonType buttonObj = ComponentConfigLoadManager.loadButtonConfig(ccbean, eleBean); if (buttonType != null && !buttonType.isInstance(buttonObj)) { throw new WabacusConfigLoadingException( "KEY" + key + "??" + buttonType.getName() + ""); } buttonObj.setLabel(getResourceString(rrequest, ccbean.getPageBean(), buttonObj.getLabel(), true)); return buttonObj; }
From source file:com.mac.tarchan.desktop.event.EventQuery.java
/** * ??????????//from ww w .jav a2 s .c o m * * @param base ?? * @param type * @return ?? EventQuery */ protected EventQuery find(Component base, Class<?> type) { if (base instanceof Container) { Container parent = (Container) base; // System.out.format("Container: %s [%s], %s\n", parent.getName(), parent.getClass().getName(), Arrays.asList(getComponents(parent))); for (Component child : getComponents(parent)) { find(child, type); } } // System.out.format("Component: %s [%s]\n", base.getName(), base.getClass().getName()); if (type.isInstance(base)) { // System.out.format("add list: %s [%s]\n", base.getName(), base.getClass().getName()); list.add(base); } return this; }
From source file:com.openerp.orm.ORM.java
/** * Gets the dB helper from module.//from w w w . j a v a2s. com * * @param module * the module * @return the dB helper from module */ private BaseDBHelper getDBHelperFromModule(Module module) { Class newClass; try { newClass = Class.forName(module.getModuleInstance().getClass().getName()); if (newClass.isInstance(module.getModuleInstance())) { Object receiver = newClass.newInstance(); Class params[] = new Class[1]; params[0] = Context.class; Method method = newClass.getDeclaredMethod("databaseHelper", params); Object obj = method.invoke(receiver, this.context); return (BaseDBHelper) obj; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:hudson.model.Job.java
private <T extends JobProperty> T _getProperty(Class<T> clazz) { for (JobProperty p : properties) { if (clazz.isInstance(p)) return clazz.cast(p); }/*w w w.j a v a2s . c om*/ return null; }
From source file:hudson.model.Fingerprint.java
/** * Finds a facet of the specific type (including subtypes.) * @param <T> Class of the {@link FingerprintFacet} * @return First matching facet of the specified class * @since 1.556/*from w w w. j a v a 2 s. c o m*/ */ public @CheckForNull <T extends FingerprintFacet> T getFacet(Class<T> type) { for (FingerprintFacet f : getFacets()) { if (type.isInstance(f)) return type.cast(f); } return null; }
From source file:hudson.model.Job.java
/** * Removes the property of the given type. * * @return//w w w . java2s . c om * The property that was just removed. * @since 1.279 */ public <T extends JobProperty> T removeProperty(Class<T> clazz) throws IOException { for (JobProperty<? super JobT> p : properties) { if (clazz.isInstance(p)) { removeProperty(p); return clazz.cast(p); } } return null; }
From source file:com.github.dozermapper.core.MappingProcessor.java
private Object mapUsingCustomConverter(Class<?> customConverterClass, Class<?> srcFieldClass, Object srcFieldValue, Class<?> destFieldClass, Object existingDestFieldValue, FieldMap fieldMap, boolean topLevel) { CustomConverter converterInstance = null; // search our injected customconverters for a match if (customConverterObjects != null) { for (CustomConverter customConverterObject : customConverterObjects) { if (customConverterClass.isInstance(customConverterObject)) { // we have a match converterInstance = customConverterObject; }// w w w .j ava 2 s.c o m } } // if converter object instances were not injected, then create new instance // of the converter for each conversion // TODO : Should we really create it each time? if (converterInstance == null) { converterInstance = (CustomConverter) ReflectionUtils.newInstance(customConverterClass); } return mapUsingCustomConverterInstance(converterInstance, srcFieldClass, srcFieldValue, destFieldClass, existingDestFieldValue, fieldMap, topLevel); }
From source file:com.zenesis.qx.remote.RequestHandler.java
/** * Reads an array from JSON, where each value is of the class clazz. Note that while the result * is an array, you cannot assume that it is an array of Object, or use generics because generics * are always Objects - this is because arrays of primitive types are not arrays of Objects * @param jp//from www. ja va 2s. c o m * @param clazz * @return * @throws IOException */ private Object readArray(JsonParser jp, Class clazz) throws IOException { if (jp.getCurrentToken() == JsonToken.VALUE_NULL) return null; boolean isProxyClass = Proxied.class.isAssignableFrom(clazz); ArrayList result = new ArrayList(); for (; jp.nextToken() != JsonToken.END_ARRAY;) { if (isProxyClass) { Integer id = jp.readValueAs(Integer.class); if (id != null) { Proxied obj = getProxied(id); if (obj == null) log.fatal("Cannot read object of class " + clazz + " from id=" + id); else if (!clazz.isInstance(obj)) throw new ClassCastException( "Cannot cast " + obj + " class " + obj.getClass() + " to " + clazz); else result.add(obj); } else result.add(null); } else { Object obj = readSimpleValue(jp, clazz); result.add(obj); } } Object arr = Array.newInstance(clazz, result.size()); for (int i = 0; i < result.size(); i++) Array.set(arr, i, result.get(i)); return arr; //return result.toArray(Array.newInstance(clazz, result.size())); }
From source file:com.zenesis.qx.remote.RequestHandler.java
/** * Reads an array from JSON, where each value is of the class clazz. Note that while the result * is an array, you cannot assume that it is an array of Object, or use generics because generics * are always Objects - this is because arrays of primitive types are not arrays of Objects * @param jp/*from w w w . j a v a 2 s . c o m*/ * @param clazz * @return * @throws IOException */ private Map readMap(JsonParser jp, Class keyClazz, Class clazz) throws IOException { if (jp.getCurrentToken() == JsonToken.VALUE_NULL) return null; boolean isProxyClass = Proxied.class.isAssignableFrom(clazz); if (keyClazz == null) keyClazz = String.class; HashMap result = new HashMap(); for (; jp.nextToken() != JsonToken.END_OBJECT;) { Object key = readSimpleValue(jp, keyClazz); jp.nextToken(); if (isProxyClass) { Integer id = jp.readValueAs(Integer.class); if (id != null) { Proxied obj = getProxied(id); if (!clazz.isInstance(obj)) throw new ClassCastException( "Cannot cast " + obj + " class " + obj.getClass() + " to " + clazz); result.put(key, obj); } else result.put(key, null); } else { Object obj = readSimpleValue(jp, clazz); result.put(key, obj); } } return result; }