Example usage for java.lang Class isInterface

List of usage examples for java.lang Class isInterface

Introduction

In this page you can find the example usage for java.lang Class isInterface.

Prototype

@HotSpotIntrinsicCandidate
public native boolean isInterface();

Source Link

Document

Determines if the specified Class object represents an interface type.

Usage

From source file:com.netflix.paas.config.base.JavaAssistArchaeusConfigurationFactory.java

@SuppressWarnings({ "unchecked", "static-access" })
public <T> T get(Class<T> configClass, DynamicPropertyFactory propertyFactory,
        AbstractConfiguration configuration) throws Exception {
    final Map<String, Supplier<?>> methods = ConfigurationProxyUtils.getMethodSuppliers(configClass,
            propertyFactory, configuration);

    if (configClass.isInterface()) {
        Class<?> proxyClass = Proxy.getProxyClass(configClass.getClassLoader(), new Class[] { configClass });

        return (T) proxyClass.getConstructor(new Class[] { InvocationHandler.class })
                .newInstance(new Object[] { new InvocationHandler() {
                    @Override/* w ww .j av a 2s.com*/
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                        if (supplier == null) {
                            return method.invoke(proxy, args);
                        }
                        return supplier.get();
                    }
                } });
    } else {
        final Enhancer enhancer = new Enhancer();
        final Object obj = (T) enhancer.create(configClass, new net.sf.cglib.proxy.InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                Supplier<?> supplier = (Supplier<?>) methods.get(method.getName());
                if (supplier == null) {
                    return method.invoke(proxy, args);
                }
                return supplier.get();
            }
        });

        ConfigurationProxyUtils.assignFieldValues(obj, configClass, propertyFactory, configuration);
        return (T) obj;
    }
}

From source file:org.atemsource.atem.impl.pojo.PojoEntityTypeRepository.java

protected void initializeEntityType(AbstractEntityType entityType) {
    entityType.setMetaType(entityTypeCreationContext.getEntityTypeReference(EntityType.class));
    Class clazz = entityType.getEntityClass();
    Class superclass = clazz.getSuperclass();
    if (clazz.isInterface() && clazz.getInterfaces().length == 1) {
        // TODO workaround for interface inheritance hierachy (Attribute)
        superclass = clazz.getInterfaces()[0];
    }/*from   w w w  .  ja  v a  2 s  .  c  o  m*/
    if (superclass != null && !superclass.equals(Object.class)) {
        final EntityType superType = getEntityTypeReference(superclass);
        entityType.setSuperEntityType(superType);
        // TODO this only works for AbstractEntityType but actually should
        // work for all.
        if (superType instanceof AbstractEntityType) {
            ((AbstractEntityType) superType).addSubEntityType(entityType);
        }
    }
    addAttributes(entityType);
    attacheServicesToEntityType(entityType);
    entityType.initializeIncomingAssociations(entityTypeCreationContext);
    entityTypeCreationContext.lazilyInitialized(entityType);
}

From source file:com.mockey.plugin.PluginStore.java

/**
 * //from   www .j  av a  2s .  c om
 * @param className
 * @return Instance of a Class with 'className, if implements
 *         <code>IRequestInspector</code>, otherwise returns null.
 */
private Class<?> doesThisImplementIRequestInspector(String className) {

    try {
        try {
            // HACK:
            Class<?> xx = Class.forName(className);
            if (!xx.getName().equalsIgnoreCase(RequestInspectorDefinedByJson.class.getName())
                    && (!xx.isInterface() && IRequestInspector.class.isAssignableFrom(xx))) {
                return xx;
            }
        } catch (ClassNotFoundException e) {
            Class<?> xx = ClassLoader.getSystemClassLoader().loadClass(className);
            if (!xx.isInterface() && IRequestInspector.class.isAssignableFrom(xx)) {
                return xx;
            }
        }
    } catch (java.lang.NoClassDefFoundError classDefNotFound) {
        logger.debug("Unable to create class: " + className + "; reason: java.lang.NoClassDefFoundError");
    } catch (Exception e) {
        logger.error("Unable to create an instance of a class w/ name " + className, e);
    }

    return null;
}

From source file:org.spring.guice.module.SpringModule.java

@Override
public void configure(Binder binder) {
    for (String name : beanFactory.getBeanDefinitionNames()) {
        BeanDefinition definition = beanFactory.getBeanDefinition(name);
        if (definition.isAutowireCandidate()
                && definition.getRole() == AbstractBeanDefinition.ROLE_APPLICATION) {
            Class<?> type = beanFactory.getType(name);
            @SuppressWarnings("unchecked")
            final Class<Object> cls = (Class<Object>) type;
            final String beanName = name;
            Provider<Object> provider = new BeanFactoryProvider(beanFactory, beanName, type);
            if (!cls.isInterface() && !ClassUtils.isCglibProxyClass(cls)) {
                bindConditionally(binder, name, cls, provider);
            }/*from   ww  w.j av  a  2  s  . c  o m*/
            for (Class<?> iface : ClassUtils.getAllInterfacesForClass(cls)) {
                @SuppressWarnings("unchecked")
                Class<Object> unchecked = (Class<Object>) iface;
                bindConditionally(binder, name, unchecked, provider);
            }
        }
    }
}

From source file:org.bytesoft.bytetcc.supports.spring.CompensableAnnotationValidator.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    Map<String, Class<?>> otherServiceMap = new HashMap<String, Class<?>>();
    Map<String, Compensable> compensables = new HashMap<String, Compensable>();

    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    String[] beanNameArray = beanFactory.getBeanDefinitionNames();
    for (int i = 0; beanNameArray != null && i < beanNameArray.length; i++) {
        String beanName = beanNameArray[i];
        BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName);
        String className = beanDef.getBeanClassName();
        Class<?> clazz = null;

        try {//from w  ww  .ja v  a2s  . c om
            clazz = cl.loadClass(className);
        } catch (ClassNotFoundException ex) {
            continue;
        }

        try {
            Compensable compensable = clazz.getAnnotation(Compensable.class);
            if (compensable == null) {
                otherServiceMap.put(beanName, clazz);
                continue;
            } else {
                compensables.put(beanName, compensable);
            }

            Class<?> interfaceClass = compensable.interfaceClass();
            if (interfaceClass.isInterface() == false) {
                throw new IllegalStateException("Compensable's interfaceClass must be a interface.");
            }
            Method[] methodArray = interfaceClass.getDeclaredMethods();
            for (int j = 0; j < methodArray.length; j++) {
                Method interfaceMethod = methodArray[j];
                Method method = clazz.getMethod(interfaceMethod.getName(), interfaceMethod.getParameterTypes());
                this.validateDeclaredRemotingException(method, clazz);
                this.validateTransactionalPropagation(method, clazz);
            }
        } catch (IllegalStateException ex) {
            throw new FatalBeanException(ex.getMessage(), ex);
        } catch (NoSuchMethodException ex) {
            throw new FatalBeanException(ex.getMessage(), ex);
        } catch (SecurityException ex) {
            throw new FatalBeanException(ex.getMessage(), ex);
        }
    }

    Iterator<Map.Entry<String, Compensable>> itr = compensables.entrySet().iterator();
    while (itr.hasNext()) {
        Map.Entry<String, Compensable> entry = itr.next();
        Compensable compensable = entry.getValue();
        Class<?> interfaceClass = compensable.interfaceClass();
        String confirmableKey = compensable.confirmableKey();
        String cancellableKey = compensable.cancellableKey();
        if (StringUtils.isNotBlank(confirmableKey)) {
            if (compensables.containsKey(confirmableKey)) {
                throw new FatalBeanException(String
                        .format("The confirm bean(id= %s) cannot be a compensable service!", confirmableKey));
            }
            Class<?> clazz = otherServiceMap.get(confirmableKey);
            if (clazz == null) {
                throw new IllegalStateException(
                        String.format("The confirm bean(id= %s) is not exists!", confirmableKey));
            }

            try {
                Method[] methodArray = interfaceClass.getDeclaredMethods();
                for (int j = 0; j < methodArray.length; j++) {
                    Method interfaceMethod = methodArray[j];
                    Method method = clazz.getMethod(interfaceMethod.getName(),
                            interfaceMethod.getParameterTypes());
                    this.validateDeclaredRemotingException(method, clazz);
                    this.validateTransactionalPropagation(method, clazz);
                    this.validateTransactionalRollbackFor(method, clazz, confirmableKey);
                }
            } catch (IllegalStateException ex) {
                throw new FatalBeanException(ex.getMessage(), ex);
            } catch (NoSuchMethodException ex) {
                throw new FatalBeanException(ex.getMessage(), ex);
            } catch (SecurityException ex) {
                throw new FatalBeanException(ex.getMessage(), ex);
            }
        }

        if (StringUtils.isNotBlank(cancellableKey)) {
            if (compensables.containsKey(cancellableKey)) {
                throw new FatalBeanException(String
                        .format("The cancel bean(id= %s) cannot be a compensable service!", confirmableKey));
            }
            Class<?> clazz = otherServiceMap.get(cancellableKey);
            if (clazz == null) {
                throw new IllegalStateException(
                        String.format("The cancel bean(id= %s) is not exists!", cancellableKey));
            }

            try {
                Method[] methodArray = interfaceClass.getDeclaredMethods();
                for (int j = 0; j < methodArray.length; j++) {
                    Method interfaceMethod = methodArray[j];
                    Method method = clazz.getMethod(interfaceMethod.getName(),
                            interfaceMethod.getParameterTypes());
                    this.validateDeclaredRemotingException(method, clazz);
                    this.validateTransactionalPropagation(method, clazz);
                    this.validateTransactionalRollbackFor(method, clazz, cancellableKey);
                }
            } catch (IllegalStateException ex) {
                throw new FatalBeanException(ex.getMessage(), ex);
            } catch (NoSuchMethodException ex) {
                throw new FatalBeanException(ex.getMessage(), ex);
            } catch (SecurityException ex) {
                throw new FatalBeanException(ex.getMessage(), ex);
            }

        }
    }
}

From source file:cc.creativecomputing.events.CCListenerManager.java

/**
 * Creates an EventListenerSupport object which supports the provided listener interface using the specified class
 * loader to create the JDK dynamic proxy.
 * /*from  www  .j av a2  s .  co  m*/
 * @param listenerInterface the listener interface.
 * @param classLoader the class loader.
 * 
 * @throws NullPointerException if <code>listenerInterface</code> or <code>classLoader</code> is <code>null</code>.
 * @throws IllegalArgumentException if <code>listenerInterface</code> is not an interface.
 */
public CCListenerManager(Class<ListenerType> listenerInterface, ClassLoader classLoader) {
    Validate.notNull(listenerInterface, "Listener interface cannot be null.");
    Validate.notNull(classLoader, "ClassLoader cannot be null.");
    Validate.isTrue(listenerInterface.isInterface(), "Class {0} is not an interface",
            listenerInterface.getName());
    _myProxy = listenerInterface.cast(Proxy.newProxyInstance(classLoader, new Class[] { listenerInterface },
            new ProxyInvocationHandler()));
}

From source file:com.github.dactiv.fear.service.RemoteApiCaller.java

/**
 * ??/*from  w ww.j  a va  2  s  .c om*/
 *
 * @param targetClass  class
 * @param method      ??
 * @param paramTypes  ?
 *
 * @return 
 */
private Method findParamMethod(Class<?> targetClass, String method, Class<?>[] paramTypes) {

    // ?
    Method[] methods = (targetClass.isInterface() ? targetClass.getMethods()
            : ReflectionUtils.getAllDeclaredMethods(targetClass));

    // ?
    for (Method m : methods) {
        // ??
        Class<?>[] methodParamTypes = m.getParameterTypes();
        // ??????
        if (m.getName().equals(method) && methodParamTypes.length == paramTypes.length) {
            // ??
            Boolean flag = Boolean.TRUE;
            // ??flag  true
            for (int i = 0; i < methodParamTypes.length; i++) {

                Class<?> paramTarget = paramTypes[i];
                Class<?> paramSource = methodParamTypes[i];

                if (paramTarget != null && paramTarget != paramSource
                        && !paramSource.isAssignableFrom(paramTarget)) {
                    flag = Boolean.FALSE;
                }

            }

            if (flag) {
                cacheService(targetClass, m, paramTypes);
                return m;
            }

        }

    }

    return null;
}

From source file:com.github.haixing_hu.bean.DefaultBeanClass.java

/**
 * Sets the type of the beans created by this bean class.
 *
 * @param beanType/*from  w  w  w.  j av  a 2 s.com*/
 *          the class object of the type of the beans created by this bean
 *          class.
 */
protected void setBeanType(@Nullable final Class<? extends Bean> beanType) {
    if (beanType == null) {
        this.beanType = DefaultBean.class;
    } else if (beanType.isInterface()) {
        throw new IllegalArgumentException("Class " + beanType.getName() + " is an interface, not a class");
    } else {
        this.beanType = beanType;
    }
    // Identify the Constructor we will use in newInstance()
    try {
        constructor = this.beanType.getConstructor(constructorSignature);
    } catch (final NoSuchMethodException e) {
        throw new IllegalArgumentException(
                "Class " + this.beanType.getName() + " does not have an appropriate constructor");
    }
}

From source file:org.gradle.model.internal.manage.schema.store.ModelSchemaExtractor.java

public <T> void validateType(ModelType<T> type) {
    Class<T> typeClass = type.getConcreteClass();
    if (!isManaged(typeClass)) {
        throw invalid(type, String.format("must be annotated with %s", Managed.class.getName()));
    }//from   w ww  . j  a va2s .co  m

    if (!typeClass.isInterface()) {
        throw invalid(type, "must be defined as an interface");
    }

    if (typeClass.getInterfaces().length > 0) {
        throw invalid(type, "cannot extend other types");
    }

    if (typeClass.getTypeParameters().length > 0) {
        throw invalid(type, "cannot be a parameterized type");
    }
}

From source file:com.jetyun.pgcd.rpc.serializer.impl.BeanSerializer.java

public boolean canSerialize(Class clazz, Class jsonClazz) {
    return (!clazz.isArray() && !clazz.isPrimitive() && !clazz.isInterface()
            && (jsonClazz == null || jsonClazz == JSONObject.class));
}