Search in sources :

Example 1 with ParameterizedTypeImpl

use of sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl in project RestyPass by darren-fu.

the class DefaultRestyCommand method isAsyncCommand.

/**
 * 是否是异步请求
 *
 * @return 是异步:true, 不是异步请求:false
 */
private boolean isAsyncCommand() {
    Type returnType = this.getReturnType();
    this.asyncFutureReturn = false;
    this.asyncFutureArg = false;
    if (returnType instanceof ParameterizedTypeImpl) {
        // 返回类型是Future
        ParameterizedTypeImpl returnParameterType = (ParameterizedTypeImpl) returnType;
        if ((returnParameterType.getRawType() == Future.class || returnParameterType.getRawType() == RestyFuture.class) && returnParameterType.getActualTypeArguments() != null && returnParameterType.getActualTypeArguments().length > 0) {
            // 替换return type: Future<User> -> User
            this.returnType = returnParameterType.getActualTypeArguments()[0];
            this.asyncFutureReturn = true;
            return true;
        }
    }
    RestyFuture futureArg = getFutureArg(this);
    if (futureArg != null) {
        /**
         * Future类型出参
         * command执行的时候处理此类异步,将执行结果future放入futureArg中
         * @see DefaultRestyCommand#start
         */
        this.asyncFutureArg = true;
        return true;
    }
    return false;
}
Also used : Type(java.lang.reflect.Type) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl)

Example 2 with ParameterizedTypeImpl

use of sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl in project conductor by Netflix.

the class WorkflowClientTest method testSearch.

@Test
public void testSearch() {
    ClientResponse clientResponse = mock(ClientResponse.class);
    SearchResult<WorkflowSummary> workflowSearchResult = new SearchResult<>();
    workflowSearchResult.setTotalHits(1);
    WorkflowSummary workflowSummary = new WorkflowSummary(new Workflow());
    workflowSearchResult.setResults(Collections.singletonList(workflowSummary));
    when(clientResponse.getEntity(argThat((GenericType<SearchResult<WorkflowSummary>> type) -> ((ParameterizedTypeImpl) type.getType()).getRawType().equals(SearchResult.class) && ((ParameterizedTypeImpl) type.getType()).getActualTypeArguments()[0].equals(WorkflowSummary.class)))).thenReturn(workflowSearchResult);
    when(clientHandler.handle(argThat(argument -> argument.getURI().equals(URI.create("http://myuri:8080/workflow/search?query=my_complex_query"))))).thenReturn(clientResponse);
    SearchResult<WorkflowSummary> searchResult = workflowClient.search("my_complex_query");
    assertEquals(1, searchResult.getTotalHits());
    assertEquals(Collections.singletonList(workflowSummary), searchResult.getResults());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandler(com.sun.jersey.api.client.ClientHandler) SearchResult(com.netflix.conductor.common.run.SearchResult) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) ClientResponse(com.sun.jersey.api.client.ClientResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) WorkflowSummary(com.netflix.conductor.common.run.WorkflowSummary) Workflow(com.netflix.conductor.common.run.Workflow) GenericType(com.sun.jersey.api.client.GenericType) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) URI(java.net.URI) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Before(org.junit.Before) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Mockito.mock(org.mockito.Mockito.mock) WorkflowSummary(com.netflix.conductor.common.run.WorkflowSummary) Workflow(com.netflix.conductor.common.run.Workflow) SearchResult(com.netflix.conductor.common.run.SearchResult) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) Test(org.junit.Test)

Example 3 with ParameterizedTypeImpl

use of sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl in project cdmlib by cybertaxonomy.

the class AdvancedBeanInitializer method bulkLoadLazyCollections.

/**
 * Loads all lazy collections added to the node and empty the lazy collections collection of the node.
 * Lazy beans are initialized elsewhere: {@link #bulkLoadLazyBeans(BeanInitNode)}
 * @param node the {@link BeanInitNode} to load the lazy beans for
 */
private void bulkLoadLazyCollections(BeanInitNode node) {
    for (Class<?> ownerClazz : node.getLazyCollections().keySet()) {
        Map<String, Set<Serializable>> lazyParams = node.getLazyCollections().get(ownerClazz);
        for (String param : lazyParams.keySet()) {
            Set<Serializable> idSet = lazyParams.get(param);
            if (idSet != null && !idSet.isEmpty()) {
                if (logger.isTraceEnabled()) {
                    logger.trace("bulk load " + node + " collections ; ownerClass=" + ownerClazz.getSimpleName() + " ; param = " + param);
                }
                Type collectionEntitiyType = null;
                PropertyDescriptor[] descriptors = PropertyUtils.getPropertyDescriptors(ownerClazz);
                for (PropertyDescriptor d : descriptors) {
                    if (d.getName().equals(param)) {
                        Method readMethod = d.getReadMethod();
                        ParameterizedType pt = (ParameterizedType) readMethod.getGenericReturnType();
                        Type[] actualTypeArguments = pt.getActualTypeArguments();
                        if (actualTypeArguments.length == 2) {
                            // this must be a map of <Language, String> (aka LanguageString) there is no other case like this in the cdm
                            // in case of Maps the returned Collection will be the Collection of the values, so collectionEntitiyType is the
                            // second typeArgument
                            collectionEntitiyType = actualTypeArguments[1];
                        } else {
                            collectionEntitiyType = actualTypeArguments[0];
                        }
                        if (collectionEntitiyType instanceof TypeVariable) {
                            collectionEntitiyType = ((TypeVariable) collectionEntitiyType).getBounds()[0];
                        }
                    }
                }
                // TODO use entity name ??
                // get from repository
                List<Object[]> list;
                String hql = "SELECT oc " + " FROM %s as oc LEFT JOIN FETCH oc.%s as col %s" + " WHERE oc.id IN (:idSet) ";
                if (collectionEntitiyType instanceof ParameterizedTypeImpl) {
                    collectionEntitiyType = ((ParameterizedTypeImpl) collectionEntitiyType).getRawType();
                }
                AutoInit autoInit = addAutoinitFetchLoading((Class<?>) collectionEntitiyType, "col");
                hql = String.format(hql, ownerClazz.getSimpleName(), param, autoInit.leftJoinFetch);
                try {
                    if (logger.isTraceEnabled()) {
                        logger.trace(hql);
                    }
                    Query query = genericDao.getHqlQuery(hql);
                    query.setParameterList("idSet", idSet);
                    list = query.list();
                    if (logger.isTraceEnabled()) {
                        logger.trace("size of retrieved list is " + list.size());
                    }
                } catch (HibernateException e) {
                    e.printStackTrace();
                    throw e;
                }
                // getTarget and add to child node
                if (logger.isTraceEnabled()) {
                    logger.trace("initialize bulk loaded " + node + " collections - DONE");
                }
                for (Object parentBean : list) {
                    try {
                        Object propValue = PropertyUtils.getProperty(parentBean, mapFieldToPropertyName(param, parentBean.getClass().getSimpleName()));
                        if (propValue == null) {
                            logger.trace("Collection is null");
                        } else {
                            if (propValue instanceof PersistentMap) {
                                propValue = ((PersistentMap) propValue).values();
                            }
                            for (Object newBean : (Collection<Object>) propValue) {
                                if (newBean instanceof HibernateProxy) {
                                    newBean = initializeInstance(newBean);
                                }
                                if (HibernateProxyHelper.isInstanceOf(newBean, CdmBase.class)) {
                                    autoinitializeBean((CdmBase) newBean, autoInit);
                                }
                                node.addBean(newBean);
                            }
                        }
                    } catch (Exception e) {
                        // TODO better throw an exception ?
                        logger.error("error while getting collection property", e);
                    }
                }
                if (logger.isTraceEnabled()) {
                    logger.trace("bulk load " + node + " collections - DONE");
                }
            }
        }
    }
    for (AbstractPersistentCollection collection : node.getUninitializedCollections()) {
        if (!collection.wasInitialized()) {
            // should not happen anymore
            collection.forceInitialization();
            if (logger.isTraceEnabled()) {
                logger.trace("forceInitialization of collection " + collection);
            }
        } else {
            if (logger.isTraceEnabled()) {
                logger.trace("collection " + collection + " is initialized - OK!");
            }
        }
    }
    node.resetLazyCollections();
}
Also used : Serializable(java.io.Serializable) HashSet(java.util.HashSet) Set(java.util.Set) PersistentMap(org.hibernate.collection.internal.PersistentMap) PropertyDescriptor(java.beans.PropertyDescriptor) Query(org.hibernate.Query) HibernateException(org.hibernate.HibernateException) Method(java.lang.reflect.Method) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) HibernateProxy(org.hibernate.proxy.HibernateProxy) InvocationTargetException(java.lang.reflect.InvocationTargetException) HibernateException(org.hibernate.HibernateException) ParameterizedType(java.lang.reflect.ParameterizedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) TypeVariable(java.lang.reflect.TypeVariable) Collection(java.util.Collection) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection)

Example 4 with ParameterizedTypeImpl

use of sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl in project conductor by Netflix.

the class WorkflowClientTest method testSearchV2.

@Test
public void testSearchV2() {
    ClientResponse clientResponse = mock(ClientResponse.class);
    SearchResult<Workflow> workflowSearchResult = new SearchResult<>();
    workflowSearchResult.setTotalHits(1);
    Workflow workflow = new Workflow();
    workflowSearchResult.setResults(Collections.singletonList(workflow));
    when(clientResponse.getEntity(argThat((GenericType<SearchResult<Workflow>> type) -> ((ParameterizedTypeImpl) type.getType()).getRawType().equals(SearchResult.class) && ((ParameterizedTypeImpl) type.getType()).getActualTypeArguments()[0].equals(Workflow.class)))).thenReturn(workflowSearchResult);
    when(clientHandler.handle(argThat(argument -> argument.getURI().equals(URI.create("http://myuri:8080/workflow/search-v2?query=my_complex_query"))))).thenReturn(clientResponse);
    SearchResult<Workflow> searchResult = workflowClient.searchV2("my_complex_query");
    assertEquals(1, searchResult.getTotalHits());
    assertEquals(Collections.singletonList(workflow), searchResult.getResults());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandler(com.sun.jersey.api.client.ClientHandler) SearchResult(com.netflix.conductor.common.run.SearchResult) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) ClientResponse(com.sun.jersey.api.client.ClientResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) WorkflowSummary(com.netflix.conductor.common.run.WorkflowSummary) Workflow(com.netflix.conductor.common.run.Workflow) GenericType(com.sun.jersey.api.client.GenericType) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) URI(java.net.URI) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Before(org.junit.Before) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Mockito.mock(org.mockito.Mockito.mock) Workflow(com.netflix.conductor.common.run.Workflow) SearchResult(com.netflix.conductor.common.run.SearchResult) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) Test(org.junit.Test)

Example 5 with ParameterizedTypeImpl

use of sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl in project conductor by Netflix.

the class WorkflowClientTest method testSearchWithParams.

@Test
public void testSearchWithParams() {
    ClientResponse clientResponse = mock(ClientResponse.class);
    SearchResult<WorkflowSummary> workflowSearchResult = new SearchResult<>();
    workflowSearchResult.setTotalHits(1);
    WorkflowSummary workflowSummary = new WorkflowSummary(new Workflow());
    workflowSearchResult.setResults(Collections.singletonList(workflowSummary));
    when(clientResponse.getEntity(argThat((GenericType<SearchResult<WorkflowSummary>> type) -> ((ParameterizedTypeImpl) type.getType()).getRawType().equals(SearchResult.class) && ((ParameterizedTypeImpl) type.getType()).getActualTypeArguments()[0].equals(WorkflowSummary.class)))).thenReturn(workflowSearchResult);
    when(clientHandler.handle(argThat(argument -> argument.getURI().equals(URI.create("http://myuri:8080/workflow/search?start=0&size=10&sort=sort&freeText=text&query=my_complex_query"))))).thenReturn(clientResponse);
    SearchResult<WorkflowSummary> searchResult = workflowClient.search(0, 10, "sort", "text", "my_complex_query");
    assertEquals(1, searchResult.getTotalHits());
    assertEquals(Collections.singletonList(workflowSummary), searchResult.getResults());
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) ClientHandler(com.sun.jersey.api.client.ClientHandler) SearchResult(com.netflix.conductor.common.run.SearchResult) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) ClientResponse(com.sun.jersey.api.client.ClientResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) ClientConfig(com.sun.jersey.api.client.config.ClientConfig) WorkflowSummary(com.netflix.conductor.common.run.WorkflowSummary) Workflow(com.netflix.conductor.common.run.Workflow) GenericType(com.sun.jersey.api.client.GenericType) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) URI(java.net.URI) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) Before(org.junit.Before) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Mockito.mock(org.mockito.Mockito.mock) WorkflowSummary(com.netflix.conductor.common.run.WorkflowSummary) Workflow(com.netflix.conductor.common.run.Workflow) SearchResult(com.netflix.conductor.common.run.SearchResult) ParameterizedTypeImpl(sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl) Test(org.junit.Test)

Aggregations

ParameterizedTypeImpl (sun.reflect.generics.reflectiveObjects.ParameterizedTypeImpl)6 SearchResult (com.netflix.conductor.common.run.SearchResult)4 Workflow (com.netflix.conductor.common.run.Workflow)4 WorkflowSummary (com.netflix.conductor.common.run.WorkflowSummary)4 ClientHandler (com.sun.jersey.api.client.ClientHandler)4 ClientResponse (com.sun.jersey.api.client.ClientResponse)4 GenericType (com.sun.jersey.api.client.GenericType)4 ClientConfig (com.sun.jersey.api.client.config.ClientConfig)4 URI (java.net.URI)4 Collections (java.util.Collections)4 TestCase.assertEquals (junit.framework.TestCase.assertEquals)4 Before (org.junit.Before)4 Test (org.junit.Test)4 RunWith (org.junit.runner.RunWith)4 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)4 Mock (org.mockito.Mock)4 Mockito.mock (org.mockito.Mockito.mock)4 Mockito.when (org.mockito.Mockito.when)4 MockitoJUnitRunner (org.mockito.junit.MockitoJUnitRunner)4 Type (java.lang.reflect.Type)2