List of usage examples for java.lang Class getInterfaces
public Class<?>[] getInterfaces()
From source file:org.apache.geode.internal.cache.xmlcache.CacheXmlParser.java
/** * Ending the instantiator registration should leave us with a class name and an Integer ID on the * stack. Pull them off, and setup the instantiator with an anonymous inner class to do the work. *///from w w w .j a v a2s .c o m private void endInstantiator() { final Class c = getClassFromStack(); Class[] ifaces = c.getInterfaces(); boolean found = false; for (Class clazz : ifaces) { if (clazz == DataSerializable.class) { found = true; break; } } if (!found) { throw new CacheXmlException( LocalizedStrings.CacheXmlParser_A_0_IS_NOT_DATA_SERIALIZABLE.toLocalizedString(c.getName())); } // the next thing on the stack should be the Integer registration ID Object o = this.stack.peek(); if (!(o instanceof Integer)) { String s = LocalizedStrings.CacheXmlParser_NO_SERIALIZATION_ID.toLocalizedString(); throw new CacheXmlException(s); } Integer id = (Integer) this.stack.pop(); SerializerCreation sc = (SerializerCreation) this.stack.peek(); sc.registerInstantiator(c, id); }
From source file:org.atemsource.atem.impl.common.attribute.primitive.PrimitiveTypeFactory.java
public PrimitiveType getPrimitiveType(Class primitiveType) { if (String.class.isAssignableFrom(primitiveType)) { return new SimpleTextType(); } else if (Boolean.TYPE.isAssignableFrom(primitiveType)) { final BooleanTypeImpl booleanTypeImpl = new BooleanTypeImpl(); booleanTypeImpl.setNullable(false); return booleanTypeImpl; } else if (Boolean.class.isAssignableFrom(primitiveType)) { return new BooleanTypeImpl(); } else if (double.class.isAssignableFrom(primitiveType)) { final DoubleType doubleTypeImpl = new DoubleType(); doubleTypeImpl.setNullable(false); return doubleTypeImpl; } else if (Double.class.isAssignableFrom(primitiveType)) { return new DoubleType(); } else if (float.class.isAssignableFrom(primitiveType)) { final FloatTypeImpl typeImpl = new FloatTypeImpl(); typeImpl.setNullable(false);// www . ja v a 2 s.c o m return typeImpl; } else if (Float.class.isAssignableFrom(primitiveType)) { return new FloatTypeImpl(); } else if (BigDecimal.class.isAssignableFrom(primitiveType)) { return new BigDecimalTypeImpl(); } else if (int.class.isAssignableFrom(primitiveType)) { final IntegerType integerTypeImpl = new IntegerType(); integerTypeImpl.setNullable(false); return integerTypeImpl; } else if (Integer.class.isAssignableFrom(primitiveType)) { return new IntegerType(); } else if (long.class.isAssignableFrom(primitiveType)) { return new LongType(false); } else if (Long.class.isAssignableFrom(primitiveType)) { return new LongType(true); } else if (Number.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Number.class, true); } else if (Character.TYPE.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Character.TYPE, false); } else if (Character.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Character.class, true); } else if (BigDecimal.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(BigDecimal.class, true); } else if (BigInteger.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(BigInteger.class, true); } else if (Byte.TYPE.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Byte.TYPE, false); } else if (byte[].class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(byte[].class, true); } else if (Byte.class.isAssignableFrom(primitiveType)) { return new SimplePrimitiveType(Byte.class, true); } else if (Enum.class.isAssignableFrom(primitiveType)) { return new SimpleEnumType(primitiveType); } else { PrimitiveType found = classToType.get(primitiveType); Class parentClass = primitiveType; while (found == null && parentClass.getSuperclass() != null) { found = classToType.get(parentClass); parentClass = parentClass.getSuperclass(); } if (found == null) { for (Class<?> interfaze : primitiveType.getInterfaces()) { found = classToType.get(interfaze); if (found != null) { return found; } } } return found; } }
From source file:com.google.dexmaker.ProxyBuilder.java
private void getMethodsToProxy(Set<MethodSetEntry> sink, Set<MethodSetEntry> seenFinalMethods, Class<?> c) { for (Method method : c.getDeclaredMethods()) { for (Class<? extends Annotation> annotation : Constants.annotation) { if (method.getAnnotation(annotation) != null) { MethodSetEntry entry = new MethodSetEntry(method); if (seenFinalMethods.contains(entry)) { continue; }/*from w ww.j a va2 s .c om*/ if (sink.add(entry)) { MethodEntity entity = new MethodEntity(); entity.clazz = c; entity.name = method.getName(); entity.params = method.getParameterTypes(); entity.method = method; methods.add(entity); } } } if (!Constants.method.contains(method.getName())) { // ?? continue; } if ((method.getModifiers() & Modifier.FINAL) != 0) { // Skip final methods, we can't override them. We // also need to remember them, in case the same // method exists in a parent class. seenFinalMethods.add(new MethodSetEntry(method)); continue; } if ((method.getModifiers() & STATIC) != 0) { // Skip static methods, overriding them has no effect. continue; } if ((method.getModifiers() & PRIVATE) != 0) { // Skip static methods, overriding them has no effect. continue; } // if ((method.getModifiers() & Modifier.PROTECTED) != 0) { // // Skip static methods, overriding them has no effect. // continue; // } if (method.getName().equals("finalize") && method.getParameterTypes().length == 0) { // Skip finalize method, it's likely important that it execute as normal. continue; } MethodSetEntry entry = new MethodSetEntry(method); if (seenFinalMethods.contains(entry)) { // This method is final in a child class. // We can't override it. continue; } if (sink.add(entry)) { MethodEntity entity = new MethodEntity(); entity.clazz = c; entity.name = method.getName(); entity.params = method.getParameterTypes(); entity.method = method; methods.add(entity); } } for (Class<?> i : c.getInterfaces()) { getMethodsToProxy(sink, seenFinalMethods, i); } }
From source file:io.coala.json.DynaBean.java
/** * @param referenceType/*from w ww . j a v a2 s . co m*/ * @param <S> * @param <T> * @return */ static final <S, T> JsonDeserializer<T> createJsonDeserializer(final ObjectMapper om, final Class<T> resultType, final Properties... imports) { return new JsonDeserializer<T>() { @Override public T deserializeWithType(final JsonParser jp, final DeserializationContext ctxt, final TypeDeserializer typeDeserializer) throws IOException, JsonProcessingException { return deserialize(jp, ctxt); } @Override public T deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { if (jp.getCurrentToken() == JsonToken.VALUE_NULL) return null; // if( Wrapper.class.isAssignableFrom( resultType ) ) // { // // FIXME // LOG.trace( "deser wrapper intf of {}", jp.getText() ); // return (T) Wrapper.Util.valueOf( jp.getText(), // resultType.asSubclass( Wrapper.class ) ); // } if (Config.class.isAssignableFrom(resultType)) { final Map<String, Object> entries = jp.readValueAs(new TypeReference<Map<String, Object>>() { }); final Iterator<Entry<String, Object>> it = entries.entrySet().iterator(); for (Entry<String, Object> next = null; it.hasNext(); next = it.next()) if (next != null && next.getValue() == null) { LOG.trace("Ignoring null value: {}", next); it.remove(); } return resultType.cast(ConfigFactory.create(resultType.asSubclass(Config.class), entries)); } // else if (Config.class.isAssignableFrom(resultType)) // throw new JsonGenerationException( // "Config does not extend "+Mutable.class.getName()+" required for deserialization: " // + Arrays.asList(resultType // .getInterfaces())); // can't parse directly to interface type final DynaBean bean = new DynaBean(); final TreeNode tree = jp.readValueAsTree(); // override attributes as defined in interface getters final Set<String> attributes = new HashSet<>(); for (Method method : resultType.getMethods()) { if (method.getReturnType().equals(Void.TYPE) || method.getParameterTypes().length != 0) continue; final String attribute = method.getName(); if (attribute.equals("toString") || attribute.equals("hashCode")) continue; attributes.add(attribute); final TreeNode value = tree.get(attribute);// bean.any().get(attributeName); if (value == null) continue; bean.set(method.getName(), om.treeToValue(value, JsonUtil.checkRegistered(om, method.getReturnType(), imports))); } if (tree.isObject()) { // keep superfluous properties as TreeNodes, just in case final Iterator<String> fieldNames = tree.fieldNames(); while (fieldNames.hasNext()) { final String fieldName = fieldNames.next(); if (!attributes.contains(fieldName)) bean.set(fieldName, tree.get(fieldName)); } } else if (tree.isValueNode()) { for (Class<?> type : resultType.getInterfaces()) for (Method method : type.getDeclaredMethods()) { // LOG.trace( "Scanning {}", method ); if (method.isAnnotationPresent(JsonProperty.class)) { final String property = method.getAnnotation(JsonProperty.class).value(); // LOG.trace( "Setting {}: {}", property, // ((ValueNode) tree).textValue() ); bean.set(property, ((ValueNode) tree).textValue()); } } } else throw ExceptionFactory.createUnchecked("Expected {} but parsed: {}", resultType, tree.getClass()); return DynaBean.proxyOf(om, resultType, bean, imports); } }; }
From source file:org.pushio.webapp.helper.json.SerializeConfig.java
public ObjectSerializer getObjectWriter(Class<?> clazz) { ObjectSerializer writer = get(clazz); if (writer == null) { try {//from w w w . j a va 2 s .c o m final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) { if (!(o instanceof AutowiredObjectSerializer)) { continue; } AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o; for (Type forType : autowired.getAutowiredFor()) { put(forType, autowired); } } } catch (ClassCastException ex) { // skip } writer = get(clazz); } if (writer == null) { final ClassLoader classLoader = JSON.class.getClassLoader(); if (classLoader != Thread.currentThread().getContextClassLoader()) { try { for (Object o : ServiceLoader.load(AutowiredObjectSerializer.class, classLoader)) { if (!(o instanceof AutowiredObjectSerializer)) { continue; } AutowiredObjectSerializer autowired = (AutowiredObjectSerializer) o; for (Type forType : autowired.getAutowiredFor()) { put(forType, autowired); } } } catch (ClassCastException ex) { // skip } writer = get(clazz); } } if (writer == null) { if (Map.class.isAssignableFrom(clazz)) { put(clazz, MapSerializer.instance); } else if (List.class.isAssignableFrom(clazz)) { put(clazz, MyListSerializer.instance); } else if (Collection.class.isAssignableFrom(clazz)) { put(clazz, MyCollectionSerializer.instance); } else if (Date.class.isAssignableFrom(clazz)) { put(clazz, DateSerializer.instance); } else if (JSONAware.class.isAssignableFrom(clazz)) { put(clazz, JSONAwareSerializer.instance); } else if (JSONSerializable.class.isAssignableFrom(clazz)) { put(clazz, JSONSerializableSerializer.instance); } else if (JSONStreamAware.class.isAssignableFrom(clazz)) { put(clazz, JSONStreamAwareSerializer.instance); } else if (clazz.isEnum() || (clazz.getSuperclass() != null && clazz.getSuperclass().isEnum())) { put(clazz, EnumSerializer.instance); } else if (clazz.isArray()) { Class<?> componentType = clazz.getComponentType(); ObjectSerializer compObjectSerializer = getObjectWriter(componentType); put(clazz, new ArraySerializer(componentType, compObjectSerializer)); } else if (Throwable.class.isAssignableFrom(clazz)) { put(clazz, new ExceptionSerializer(clazz)); } else if (TimeZone.class.isAssignableFrom(clazz)) { put(clazz, TimeZoneCodec.instance); } else if (Appendable.class.isAssignableFrom(clazz)) { put(clazz, AppendableSerializer.instance); } else if (Charset.class.isAssignableFrom(clazz)) { put(clazz, CharsetCodec.instance); } else if (Enumeration.class.isAssignableFrom(clazz)) { put(clazz, EnumerationSeriliazer.instance); } else if (Calendar.class.isAssignableFrom(clazz)) { put(clazz, CalendarCodec.instance); } else if (Clob.class.isAssignableFrom(clazz)) { put(clazz, ClobSeriliazer.instance); } else { boolean isCglibProxy = false; boolean isJavassistProxy = false; for (Class<?> item : clazz.getInterfaces()) { if (item.getName().equals("net.sf.cglib.proxy.Factory") || item.getName().equals("org.springframework.cglib.proxy.Factory")) { isCglibProxy = true; break; } else if (item.getName().equals("javassist.util.proxy.ProxyObject")) { isJavassistProxy = true; break; } } if (isCglibProxy || isJavassistProxy) { Class<?> superClazz = clazz.getSuperclass(); ObjectSerializer superWriter = getObjectWriter(superClazz); put(clazz, superWriter); return superWriter; } if (Proxy.isProxyClass(clazz)) { put(clazz, createJavaBeanSerializer(clazz)); } else { put(clazz, createJavaBeanSerializer(clazz)); } } writer = get(clazz); } return writer; }
From source file:org.regenstrief.util.Util.java
/** * Retrieves whether the given Class is equal to or a subclass/implementation of the given class * name. Unlike className.equals(c.getName()), this will also check interfaces and super * classes. Unlike Class.isAssignableFrom(Class), this can be invoked even if the className is * not a loaded class./*w ww . j a va 2s.com*/ * * @param c the Class * @param className the class name * @return whether objects of the Class are instances of the Class with the given name **/ public final static boolean instanceOf(final Class<?> c, final String className) { if ((c == null) || (className == null)) { return false; } else if (className.equals(c.getName())) { return true; } else if (instanceOf(c.getSuperclass(), className)) { return true; } final Class<?>[] interfaces = c.getInterfaces(); for (int i = 0, len = length(interfaces); i < len; i++) { if (instanceOf(interfaces[i], className)) { return true; } } return false; }
From source file:lasige.steeldb.jdbc.BFTRowSet.java
/** * Determine whether the SyncProvider's writer implements the * <code>TransactionalWriter<code> interface *//*from w w w . j a v a2 s . c om*/ private void checkTransactionalWriter() { if (rowSetWriter != null) { Class<? extends RowSetWriter> c = rowSetWriter.getClass(); if (c != null) { Class[] theInterfaces = c.getInterfaces(); for (int i = 0; i < theInterfaces.length; i++) { if ((theInterfaces[i].getName()).indexOf("TransactionalWriter") > 0) { tXWriter = true; establishTransactionalWriter(); } } } } }
From source file:com.jhh.hdb.sqlparser.MySemanticAnalyzer.java
private boolean isAcidOutputFormat(Class<? extends OutputFormat> of) { Class<?>[] interfaces = of.getInterfaces(); for (Class<?> iface : interfaces) { if (iface.equals(AcidOutputFormat.class)) { return true; }// w ww .j a va2 s . c o m } return false; }
From source file:com.clark.func.Functions.java
/** * <p>// w w w .j av a2 s .c o m * Return an accessible method (that is, one that can be invoked via * reflection) that implements the specified method, by scanning through all * implemented interfaces and subinterfaces. If no such method can be found, * return <code>null</code>. * </p> * * <p> * There isn't any good reason why this method must be private. It is * because there doesn't seem any reason why other classes should call this * rather than the higher level methods. * </p> * * @param cls * Parent class for the interfaces to be checked * @param methodName * Method name of the method we wish to call * @param parameterTypes * The parameter type signatures * @return the accessible method or <code>null</code> if not found */ private static Method getAccessibleMethodFromInterfaceNest(Class<?> cls, String methodName, Class<?>... parameterTypes) { Method method = null; // Search up the superclass chain for (; cls != null; cls = cls.getSuperclass()) { // Check the implemented interfaces of the parent class Class<?>[] interfaces = cls.getInterfaces(); for (int i = 0; i < interfaces.length; i++) { // Is this interface public? if (!Modifier.isPublic(interfaces[i].getModifiers())) { continue; } // Does the method exist on this interface? try { method = interfaces[i].getDeclaredMethod(methodName, parameterTypes); } catch (NoSuchMethodException e) { /* * Swallow, if no method is found after the loop then this * method returns null. */ } if (method != null) { break; } // Recursively check our parent interfaces method = getAccessibleMethodFromInterfaceNest(interfaces[i], methodName, parameterTypes); if (method != null) { break; } } } return method; }
From source file:st.geekli.api.ResponseParser.java
private static boolean isGeeklistType(Class<?> objectClass) { for (Class<?> interfc : objectClass.getInterfaces()) { if (interfc.equals(GeeklistType.class)) { return true; }/*from w w w . j a v a 2 s .c o m*/ } return false; }