Example usage for java.lang.reflect ParameterizedType getActualTypeArguments

List of usage examples for java.lang.reflect ParameterizedType getActualTypeArguments

Introduction

In this page you can find the example usage for java.lang.reflect ParameterizedType getActualTypeArguments.

Prototype

Type[] getActualTypeArguments();

Source Link

Document

Returns an array of Type objects representing the actual type arguments to this type.

Usage

From source file:org.medici.bia.dao.JpaDao.java

/**
 * /*from w w  w. ja  v a2  s. co  m*/
 */
@SuppressWarnings("unchecked")
protected JpaDao() {
    ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
    entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[1];
}

From source file:org.eclipse.wb.internal.core.utils.reflect.ReflectionUtils.java

/**
 * @param runtime/*from  w  w w . ja  v  a 2 s.  co  m*/
 *          is <code>true</code> if we need name for class loading, <code>false</code> if we need
 *          name for source generation.
 * 
 * @return the fully qualified name of given {@link Type}.
 */
public static String getFullyQualifiedName(Type type, boolean runtime) {
    Assert.isNotNull(type);
    // Class
    if (type instanceof Class<?>) {
        Class<?> clazz = (Class<?>) type;
        // array
        if (clazz.isArray()) {
            return getFullyQualifiedName(clazz.getComponentType(), runtime) + "[]";
        }
        // object
        String name = clazz.getName();
        if (!runtime) {
            name = name.replace('$', '.');
        }
        return name;
    }
    // GenericArrayType
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = (GenericArrayType) type;
        return getFullyQualifiedName(genericArrayType.getGenericComponentType(), runtime) + "[]";
    }
    // ParameterizedType
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        Type rawType = parameterizedType.getRawType();
        // raw type
        StringBuilder sb = new StringBuilder();
        sb.append(getFullyQualifiedName(rawType, runtime));
        // type arguments
        sb.append("<");
        boolean firstTypeArgument = true;
        for (Type typeArgument : parameterizedType.getActualTypeArguments()) {
            if (!firstTypeArgument) {
                sb.append(",");
            }
            firstTypeArgument = false;
            sb.append(getFullyQualifiedName(typeArgument, runtime));
        }
        sb.append(">");
        // done
        return sb.toString();
    }
    // WildcardType
    if (type instanceof WildcardType) {
        WildcardType wildcardType = (WildcardType) type;
        return "? extends " + getFullyQualifiedName(wildcardType.getUpperBounds()[0], runtime);
    }
    // TypeVariable
    TypeVariable<?> typeVariable = (TypeVariable<?>) type;
    return typeVariable.getName();
}

From source file:org.broadinstitute.sting.utils.help.GenericDocumentationHandler.java

/**
 * Returns a human readable string that describes the Type type of a GATK argument.
 * <p/>/* w  w w.ja  va2  s.co m*/
 * This will include parameterized types, so that Set{T} shows up as Set(T) and not
 * just Set in the docs.
 *
 * @param type
 * @return
 */
protected String argumentTypeString(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        List<String> subs = new ArrayList<String>();
        for (Type actualType : parameterizedType.getActualTypeArguments())
            subs.add(argumentTypeString(actualType));
        return argumentTypeString(((ParameterizedType) type).getRawType()) + "[" + Utils.join(",", subs) + "]";
    } else if (type instanceof GenericArrayType) {
        return argumentTypeString(((GenericArrayType) type).getGenericComponentType()) + "[]";
    } else if (type instanceof WildcardType) {
        throw new RuntimeException("We don't support wildcards in arguments: " + type);
    } else if (type instanceof Class<?>) {
        return ((Class) type).getSimpleName();
    } else {
        throw new StingException("Unknown type: " + type);
    }
}

From source file:com.evolveum.midpoint.repo.sql.helpers.ObjectDeltaUpdater.java

private Class getRealOutputType(Attribute attribute) {
    Class type = attribute.getJavaType();
    if (!Collection.class.isAssignableFrom(type)) {
        return type;
    }/* w  w  w. j av a  2 s .  co m*/

    Method method = (Method) attribute.getJavaMember();
    ParameterizedType parametrized = (ParameterizedType) method.getGenericReturnType();
    Type t = parametrized.getActualTypeArguments()[0];
    if (t instanceof Class) {
        return (Class) t;
    }

    parametrized = (ParameterizedType) t;
    return (Class) parametrized.getRawType();
}

From source file:jef.tools.reflect.ClassEx.java

public Type getImplType(TypeVariable<?> declaration) {
    if (declaration.getGenericDeclaration() == this.cls && this.genericType instanceof ParameterizedType) {
        ParameterizedType pType = (ParameterizedType) genericType;
        int n = 0;
        for (TypeVariable<?> tv : cls.getTypeParameters()) {
            if (tv == declaration)
                break;
            n++;// ww w .  jav a 2 s. co m
        }
        return pType.getActualTypeArguments()[n];
    }
    return null;
}

From source file:org.sparkcommerce.openadmin.server.dao.provider.metadata.AdornedTargetCollectionFieldMetadataProvider.java

protected void buildAdornedTargetCollectionMetadata(Class<?> parentClass, Class<?> targetClass,
        Map<String, FieldMetadata> attributes, FieldInfo field,
        FieldMetadataOverride adornedTargetCollectionMetadata, DynamicEntityDao dynamicEntityDao) {
    AdornedTargetCollectionMetadata serverMetadata = (AdornedTargetCollectionMetadata) attributes
            .get(field.getName());/*  w w w  .  j a  v  a 2  s .c o m*/

    Class<?> resolvedClass = parentClass == null ? targetClass : parentClass;
    AdornedTargetCollectionMetadata metadata;
    if (serverMetadata != null) {
        metadata = serverMetadata;
    } else {
        metadata = new AdornedTargetCollectionMetadata();
    }
    metadata.setTargetClass(targetClass.getName());
    metadata.setFieldName(field.getName());

    if (adornedTargetCollectionMetadata.getReadOnly() != null) {
        metadata.setMutable(!adornedTargetCollectionMetadata.getReadOnly());
    }
    if (adornedTargetCollectionMetadata.getShowIfProperty() != null) {
        metadata.setShowIfProperty(adornedTargetCollectionMetadata.getShowIfProperty());
    }

    org.sparkcommerce.openadmin.dto.OperationTypes dtoOperationTypes = new org.sparkcommerce.openadmin.dto.OperationTypes(
            OperationType.ADORNEDTARGETLIST, OperationType.ADORNEDTARGETLIST, OperationType.ADORNEDTARGETLIST,
            OperationType.ADORNEDTARGETLIST, OperationType.BASIC);
    if (adornedTargetCollectionMetadata.getAddType() != null) {
        dtoOperationTypes.setAddType(adornedTargetCollectionMetadata.getAddType());
    }
    if (adornedTargetCollectionMetadata.getRemoveType() != null) {
        dtoOperationTypes.setRemoveType(adornedTargetCollectionMetadata.getRemoveType());
    }
    if (adornedTargetCollectionMetadata.getFetchType() != null) {
        dtoOperationTypes.setFetchType(adornedTargetCollectionMetadata.getFetchType());
    }
    if (adornedTargetCollectionMetadata.getInspectType() != null) {
        dtoOperationTypes.setInspectType(adornedTargetCollectionMetadata.getInspectType());
    }
    if (adornedTargetCollectionMetadata.getUpdateType() != null) {
        dtoOperationTypes.setUpdateType(adornedTargetCollectionMetadata.getUpdateType());
    }

    //don't allow additional non-persistent properties or additional foreign keys for an advanced collection datasource - they don't make sense in this context
    PersistencePerspective persistencePerspective;
    if (serverMetadata != null) {
        persistencePerspective = metadata.getPersistencePerspective();
        persistencePerspective.setOperationTypes(dtoOperationTypes);
    } else {
        persistencePerspective = new PersistencePerspective(dtoOperationTypes, new String[] {},
                new ForeignKey[] {});
        metadata.setPersistencePerspective(persistencePerspective);
    }

    String parentObjectProperty = null;
    if (serverMetadata != null) {
        parentObjectProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getLinkedObjectPath();
    }
    if (!StringUtils.isEmpty(adornedTargetCollectionMetadata.getParentObjectProperty())) {
        parentObjectProperty = adornedTargetCollectionMetadata.getParentObjectProperty();
    }
    if (parentObjectProperty == null && !StringUtils.isEmpty(field.getOneToManyMappedBy())) {
        parentObjectProperty = field.getOneToManyMappedBy();
    }
    if (parentObjectProperty == null && !StringUtils.isEmpty(field.getManyToManyMappedBy())) {
        parentObjectProperty = field.getManyToManyMappedBy();
    }
    if (StringUtils.isEmpty(parentObjectProperty)) {
        throw new IllegalArgumentException(
                "Unable to infer a parentObjectProperty for the @AdminPresentationAdornedTargetCollection annotated field("
                        + field.getName()
                        + "). If not using the mappedBy property of @OneToMany or @ManyToMany, please make sure to explicitly define the parentObjectProperty property");
    }

    String sortProperty = null;
    if (serverMetadata != null) {
        sortProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getSortField();
    }
    if (!StringUtils.isEmpty(adornedTargetCollectionMetadata.getSortProperty())) {
        sortProperty = adornedTargetCollectionMetadata.getSortProperty();
    }

    metadata.setParentObjectClass(resolvedClass.getName());
    if (adornedTargetCollectionMetadata.getMaintainedAdornedTargetFields() != null) {
        metadata.setMaintainedAdornedTargetFields(
                adornedTargetCollectionMetadata.getMaintainedAdornedTargetFields());
    }
    if (adornedTargetCollectionMetadata.getGridVisibleFields() != null) {
        metadata.setGridVisibleFields(adornedTargetCollectionMetadata.getGridVisibleFields());
    }
    String parentObjectIdProperty = null;
    if (serverMetadata != null) {
        parentObjectIdProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getLinkedIdProperty();
    }
    if (adornedTargetCollectionMetadata.getParentObjectIdProperty() != null) {
        parentObjectIdProperty = adornedTargetCollectionMetadata.getParentObjectIdProperty();
    }
    String targetObjectProperty = null;
    if (serverMetadata != null) {
        targetObjectProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getTargetObjectPath();
    }
    if (adornedTargetCollectionMetadata.getTargetObjectProperty() != null) {
        targetObjectProperty = adornedTargetCollectionMetadata.getTargetObjectProperty();
    }

    String joinEntityClass = null;
    if (serverMetadata != null) {
        joinEntityClass = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getJoinEntityClass();
    }
    if (adornedTargetCollectionMetadata.getJoinEntityClass() != null) {
        joinEntityClass = adornedTargetCollectionMetadata.getJoinEntityClass();
    }

    Class<?> collectionTarget = null;
    try {
        checkCeiling: {
            try {
                ParameterizedType pt = (ParameterizedType) field.getGenericType();
                java.lang.reflect.Type collectionType = pt.getActualTypeArguments()[0];
                String ceilingEntityName = ((Class<?>) collectionType).getName();
                collectionTarget = entityConfiguration.lookupEntityClass(ceilingEntityName);
                break checkCeiling;
            } catch (NoSuchBeanDefinitionException e) {
                // We weren't successful at looking at entity configuration to find the type of this collection.
                // We will continue and attempt to find it via the Hibernate annotations
            }
            if (!StringUtils.isEmpty(field.getOneToManyTargetEntity())
                    && !void.class.getName().equals(field.getOneToManyTargetEntity())) {
                collectionTarget = Class.forName(field.getOneToManyTargetEntity());
                break checkCeiling;
            }
            if (!StringUtils.isEmpty(field.getManyToManyTargetEntity())
                    && !void.class.getName().equals(field.getManyToManyTargetEntity())) {
                collectionTarget = Class.forName(field.getManyToManyTargetEntity());
                break checkCeiling;
            }
        }
        if (StringUtils.isNotBlank(joinEntityClass)) {
            collectionTarget = Class.forName(joinEntityClass);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    if (collectionTarget == null) {
        throw new IllegalArgumentException(
                "Unable to infer the type of the collection from the targetEntity property of a OneToMany or ManyToMany collection.");
    }
    Field collectionTargetField = dynamicEntityDao.getFieldManager().getField(collectionTarget,
            targetObjectProperty);
    ManyToOne manyToOne = collectionTargetField.getAnnotation(ManyToOne.class);
    String ceiling = null;
    checkCeiling: {
        if (manyToOne != null && manyToOne.targetEntity() != void.class) {
            ceiling = manyToOne.targetEntity().getName();
            break checkCeiling;
        }
        ceiling = collectionTargetField.getType().getName();
    }
    if (!StringUtils.isEmpty(ceiling)) {
        metadata.setCollectionCeilingEntity(ceiling);
    }

    String targetObjectIdProperty = null;
    if (serverMetadata != null) {
        targetObjectIdProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getTargetIdProperty();
    }
    if (adornedTargetCollectionMetadata.getTargetObjectIdProperty() != null) {
        targetObjectIdProperty = adornedTargetCollectionMetadata.getTargetObjectIdProperty();
    }
    Boolean isAscending = true;
    if (serverMetadata != null) {
        isAscending = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getSortAscending();
    }
    if (adornedTargetCollectionMetadata.isSortAscending() != null) {
        isAscending = adornedTargetCollectionMetadata.isSortAscending();
    }

    if (serverMetadata != null) {
        AdornedTargetList adornedTargetList = (AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
        adornedTargetList.setCollectionFieldName(field.getName());
        adornedTargetList.setLinkedObjectPath(parentObjectProperty);
        adornedTargetList.setLinkedIdProperty(parentObjectIdProperty);
        adornedTargetList.setTargetObjectPath(targetObjectProperty);
        adornedTargetList.setTargetIdProperty(targetObjectIdProperty);
        adornedTargetList.setJoinEntityClass(joinEntityClass);
        adornedTargetList.setAdornedTargetEntityClassname(collectionTarget.getName());
        adornedTargetList.setSortField(sortProperty);
        adornedTargetList.setSortAscending(isAscending);
        adornedTargetList.setMutable(metadata.isMutable());
    } else {
        AdornedTargetList adornedTargetList = new AdornedTargetList(field.getName(), parentObjectProperty,
                parentObjectIdProperty, targetObjectProperty, targetObjectIdProperty,
                collectionTarget.getName(), sortProperty, isAscending);
        adornedTargetList.setJoinEntityClass(joinEntityClass);
        adornedTargetList.setMutable(metadata.isMutable());
        persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.ADORNEDTARGETLIST,
                adornedTargetList);
    }

    if (adornedTargetCollectionMetadata.getExcluded() != null) {
        if (LOG.isDebugEnabled()) {
            if (adornedTargetCollectionMetadata.getExcluded()) {
                LOG.debug("buildAdornedTargetCollectionMetadata:Excluding " + field.getName()
                        + " because it was explicitly declared in config");
            } else {
                LOG.debug("buildAdornedTargetCollectionMetadata:Showing " + field.getName()
                        + " because it was explicitly declared in config");
            }
        }
        metadata.setExcluded(adornedTargetCollectionMetadata.getExcluded());
    }
    if (adornedTargetCollectionMetadata.getFriendlyName() != null) {
        metadata.setFriendlyName(adornedTargetCollectionMetadata.getFriendlyName());
    }
    if (adornedTargetCollectionMetadata.getSecurityLevel() != null) {
        metadata.setSecurityLevel(adornedTargetCollectionMetadata.getSecurityLevel());
    }
    if (adornedTargetCollectionMetadata.getOrder() != null) {
        metadata.setOrder(adornedTargetCollectionMetadata.getOrder());
    }

    if (adornedTargetCollectionMetadata.getTab() != null) {
        metadata.setTab(adornedTargetCollectionMetadata.getTab());
    }
    if (adornedTargetCollectionMetadata.getTabOrder() != null) {
        metadata.setTabOrder(adornedTargetCollectionMetadata.getTabOrder());
    }

    if (adornedTargetCollectionMetadata.getCustomCriteria() != null) {
        metadata.setCustomCriteria(adornedTargetCollectionMetadata.getCustomCriteria());
    }

    if (adornedTargetCollectionMetadata.getUseServerSideInspectionCache() != null) {
        persistencePerspective.setUseServerSideInspectionCache(
                adornedTargetCollectionMetadata.getUseServerSideInspectionCache());
    }

    if (adornedTargetCollectionMetadata.isIgnoreAdornedProperties() != null) {
        metadata.setIgnoreAdornedProperties(adornedTargetCollectionMetadata.isIgnoreAdornedProperties());
    }
    if (adornedTargetCollectionMetadata.getCurrencyCodeField() != null) {
        metadata.setCurrencyCodeField(adornedTargetCollectionMetadata.getCurrencyCodeField());
    }

    attributes.put(field.getName(), metadata);
}

From source file:org.op4j.devutils.selected.ImplFile.java

public void computeMethodImplementations(final ImplType implType, final Class<?> interfaceClass)
        throws Exception {

    final List<Method> interfaceMethods = new ArrayList<Method>();
    interfaceMethods.addAll(Arrays.asList(interfaceClass.getDeclaredMethods()));

    final Set<Type> extendedInterfaces = new HashSet<Type>(
            Arrays.asList(interfaceClass.getGenericInterfaces()));
    Type getReturnType = null;/*  w ww. j  ava2 s .  co  m*/
    for (final Type extendedInterface : extendedInterfaces) {
        if (extendedInterface instanceof ParameterizedType) {
            final ParameterizedType pType = (ParameterizedType) extendedInterface;
            if (((Class<?>) pType.getRawType()).equals(UniqOperator.class)) {
                getReturnType = pType.getActualTypeArguments()[0];
            }
        }
    }

    try {
        interfaceMethods.add(UniqOpOperator.class.getMethod("get"));
    } catch (NoSuchMethodException e) {
        // nothing to do
    }

    if (this.className.contains("Array")) {
        this.element = "T[]";
    } else if (this.className.contains("List")) {
        this.element = "List<T>";
    } else if (this.className.contains("Set")) {
        this.element = "Set<T>";
    } else if (this.className.contains("Map")) {
        this.element = "Map<K,V>";
    } else {
        this.element = "T";
    }

    for (final Method interfaceMethod : interfaceMethods) {

        final String methodName = interfaceMethod.getName();
        this.methodNames.add(methodName);

        final Type[] parameterTypes = interfaceMethod.getGenericParameterTypes();

        if (methodName.startsWith("exec")) {
            this.currentLevelType = (new TypeRep(
                    ((WildcardType) ((ParameterizedType) parameterTypes[0]).getActualTypeArguments()[0])
                            .getLowerBounds()[0])).getStringRep();
            if (this.currentLevelType.endsWith("[]")) {
                this.currentLevelElement = this.currentLevelType.substring(0,
                        this.currentLevelType.length() - 2);
            } else if (this.currentLevelType.startsWith("List<") && this.currentLevelType.endsWith(">")) {
                this.currentLevelElement = this.currentLevelType.substring(5,
                        this.currentLevelType.length() - 1);
            } else if (this.currentLevelType.startsWith("Set<") && this.currentLevelType.endsWith(">")) {
                this.currentLevelElement = this.currentLevelType.substring(4,
                        this.currentLevelType.length() - 1);
            } else if (this.currentLevelType.startsWith("Map<") && this.currentLevelType.endsWith(">")) {
                this.currentLevelElement = "Map.Entry<"
                        + this.currentLevelType.substring(4, this.currentLevelType.length() - 1) + ">";
            } else {
                this.currentLevelElement = "%%CURRENTELEMENTSHOULDNOTBEHERE%%";
            }
        }

        final String returnTypeStr = (methodName.equals("get")
                ? (implType == ImplType.OP ? this.element : "Function<I," + this.element + ">")
                : (methodName.equals("getAsArrayOf")
                        ? (implType == ImplType.OP ? this.element + "[]" : "Function<I," + this.element + "[]>")
                        : (methodName.equals("getAsList")
                                ? (implType == ImplType.OP ? "List<" + this.element + ">"
                                        : "Function<I,List<" + this.element + ">>")
                                : new TypeRep(interfaceMethod.getGenericReturnType()).getStringRep()
                                        .replaceAll("ILevel", "Level"))));

        final StringBuilder parameterStrBuilder = new StringBuilder();
        parameterStrBuilder.append("(");

        List<String> normalisedParamTypes = new ArrayList<String>();
        List<String> normalisedRawParamTypes = new ArrayList<String>();
        for (int j = 0; j < parameterTypes.length; j++) {
            normalisedParamTypes.add(getNormalisedParamType(parameterTypes[j], methodName, j));
            normalisedRawParamTypes.add(
                    StringUtils.substringBefore(getNormalisedParamType(parameterTypes[j], methodName, j), "<"));
        }

        String[] paramNamesForMethod = paramNames
                .get(methodName + "%" + StringUtils.join(normalisedRawParamTypes, ","));
        if (paramNamesForMethod == null) {
            paramNamesForMethod = paramNames.get(methodName + "%" + parameterTypes.length);
            if (paramNamesForMethod == null) {
                paramNamesForMethod = paramNames.get(methodName);
            }
        }

        for (int j = 0; j < normalisedParamTypes.size(); j++) {

            if (j > 0) {
                parameterStrBuilder.append(", ");
            }

            parameterStrBuilder.append("final " + normalisedParamTypes.get(j) + " ");

            if (paramNamesForMethod == null) {
                throw new RuntimeException("No name for parameter " + j + " of method " + methodName
                        + " in interface " + this.interfaceTypeRep.getStringRep());
            }
            parameterStrBuilder.append(paramNamesForMethod[j]);

        }
        parameterStrBuilder.append(")");

        final StringBuilder strBuilder = new StringBuilder();
        strBuilder.append(
                "    public " + returnTypeStr + " " + methodName + parameterStrBuilder.toString() + " {\n");
        strBuilder.append("        return null;\n");
        strBuilder.append("    }\n");

        this.methodImplementations.add(strBuilder.toString());

    }

}

From source file:org.broadleafcommerce.openadmin.server.dao.provider.metadata.AdornedTargetCollectionFieldMetadataProvider.java

protected void buildAdornedTargetCollectionMetadata(Class<?> parentClass, Class<?> targetClass,
        Map<String, FieldMetadata> attributes, FieldInfo field,
        FieldMetadataOverride adornedTargetCollectionMetadata, DynamicEntityDao dynamicEntityDao) {
    AdornedTargetCollectionMetadata serverMetadata = (AdornedTargetCollectionMetadata) attributes
            .get(field.getName());//ww  w . java  2 s  .  c om

    Class<?> resolvedClass = parentClass == null ? targetClass : parentClass;
    AdornedTargetCollectionMetadata metadata;
    if (serverMetadata != null) {
        metadata = serverMetadata;
    } else {
        metadata = new AdornedTargetCollectionMetadata();
    }
    metadata.setTargetClass(targetClass.getName());
    metadata.setFieldName(field.getName());

    if (adornedTargetCollectionMetadata.getReadOnly() != null) {
        metadata.setMutable(!adornedTargetCollectionMetadata.getReadOnly());
    }
    if (adornedTargetCollectionMetadata.getShowIfProperty() != null) {
        metadata.setShowIfProperty(adornedTargetCollectionMetadata.getShowIfProperty());
    }

    org.broadleafcommerce.openadmin.dto.OperationTypes dtoOperationTypes = new org.broadleafcommerce.openadmin.dto.OperationTypes(
            OperationType.ADORNEDTARGETLIST, OperationType.ADORNEDTARGETLIST, OperationType.ADORNEDTARGETLIST,
            OperationType.ADORNEDTARGETLIST, OperationType.BASIC);
    if (adornedTargetCollectionMetadata.getAddType() != null) {
        dtoOperationTypes.setAddType(adornedTargetCollectionMetadata.getAddType());
    }
    if (adornedTargetCollectionMetadata.getRemoveType() != null) {
        dtoOperationTypes.setRemoveType(adornedTargetCollectionMetadata.getRemoveType());
    }
    if (adornedTargetCollectionMetadata.getFetchType() != null) {
        dtoOperationTypes.setFetchType(adornedTargetCollectionMetadata.getFetchType());
    }
    if (adornedTargetCollectionMetadata.getInspectType() != null) {
        dtoOperationTypes.setInspectType(adornedTargetCollectionMetadata.getInspectType());
    }
    if (adornedTargetCollectionMetadata.getUpdateType() != null) {
        dtoOperationTypes.setUpdateType(adornedTargetCollectionMetadata.getUpdateType());
    }

    //don't allow additional non-persistent properties or additional foreign keys for an advanced collection datasource - they don't make sense in this context
    PersistencePerspective persistencePerspective;
    if (serverMetadata != null) {
        persistencePerspective = metadata.getPersistencePerspective();
        persistencePerspective.setOperationTypes(dtoOperationTypes);
    } else {
        persistencePerspective = new PersistencePerspective(dtoOperationTypes, new String[] {},
                new ForeignKey[] {});
        metadata.setPersistencePerspective(persistencePerspective);
    }

    String parentObjectProperty = null;
    if (serverMetadata != null) {
        parentObjectProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getLinkedObjectPath();
    }
    if (!StringUtils.isEmpty(adornedTargetCollectionMetadata.getParentObjectProperty())) {
        parentObjectProperty = adornedTargetCollectionMetadata.getParentObjectProperty();
    }
    if (parentObjectProperty == null && !StringUtils.isEmpty(field.getOneToManyMappedBy())) {
        parentObjectProperty = field.getOneToManyMappedBy();
    }
    if (parentObjectProperty == null && !StringUtils.isEmpty(field.getManyToManyMappedBy())) {
        parentObjectProperty = field.getManyToManyMappedBy();
    }
    if (StringUtils.isEmpty(parentObjectProperty)) {
        throw new IllegalArgumentException(
                "Unable to infer a parentObjectProperty for the @AdminPresentationAdornedTargetCollection annotated field("
                        + field.getName()
                        + "). If not using the mappedBy property of @OneToMany or @ManyToMany, please make sure to explicitly define the parentObjectProperty property");
    }

    String sortProperty = null;
    if (serverMetadata != null) {
        sortProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getSortField();
    }
    if (!StringUtils.isEmpty(adornedTargetCollectionMetadata.getSortProperty())) {
        sortProperty = adornedTargetCollectionMetadata.getSortProperty();
    }

    metadata.setParentObjectClass(resolvedClass.getName());
    if (adornedTargetCollectionMetadata.getMaintainedAdornedTargetFields() != null) {
        metadata.setMaintainedAdornedTargetFields(
                adornedTargetCollectionMetadata.getMaintainedAdornedTargetFields());
    }
    if (adornedTargetCollectionMetadata.getGridVisibleFields() != null) {
        metadata.setGridVisibleFields(adornedTargetCollectionMetadata.getGridVisibleFields());
    }
    String parentObjectIdProperty = null;
    if (serverMetadata != null) {
        parentObjectIdProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getLinkedIdProperty();
    }
    if (adornedTargetCollectionMetadata.getParentObjectIdProperty() != null) {
        parentObjectIdProperty = adornedTargetCollectionMetadata.getParentObjectIdProperty();
    }
    String targetObjectProperty = null;
    if (serverMetadata != null) {
        targetObjectProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getTargetObjectPath();
    }
    if (adornedTargetCollectionMetadata.getTargetObjectProperty() != null) {
        targetObjectProperty = adornedTargetCollectionMetadata.getTargetObjectProperty();
    }
    if (StringUtils.isEmpty(parentObjectIdProperty)) {
        throw new IllegalArgumentException("targetObjectProperty not defined");
    }

    String joinEntityClass = null;
    if (serverMetadata != null) {
        joinEntityClass = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getJoinEntityClass();
    }
    if (adornedTargetCollectionMetadata.getJoinEntityClass() != null) {
        joinEntityClass = adornedTargetCollectionMetadata.getJoinEntityClass();
    }

    Class<?> collectionTarget = null;
    try {
        checkCeiling: {
            try {
                ParameterizedType pt = (ParameterizedType) field.getGenericType();
                java.lang.reflect.Type collectionType = pt.getActualTypeArguments()[0];
                String ceilingEntityName = ((Class<?>) collectionType).getName();
                collectionTarget = entityConfiguration.lookupEntityClass(ceilingEntityName);
                break checkCeiling;
            } catch (NoSuchBeanDefinitionException e) {
                // We weren't successful at looking at entity configuration to find the type of this collection.
                // We will continue and attempt to find it via the Hibernate annotations
            }
            if (!StringUtils.isEmpty(field.getOneToManyTargetEntity())
                    && !void.class.getName().equals(field.getOneToManyTargetEntity())) {
                collectionTarget = Class.forName(field.getOneToManyTargetEntity());
                break checkCeiling;
            }
            if (!StringUtils.isEmpty(field.getManyToManyTargetEntity())
                    && !void.class.getName().equals(field.getManyToManyTargetEntity())) {
                collectionTarget = Class.forName(field.getManyToManyTargetEntity());
                break checkCeiling;
            }
        }
        if (StringUtils.isNotBlank(joinEntityClass)) {
            collectionTarget = Class.forName(joinEntityClass);
        }
    } catch (ClassNotFoundException e) {
        throw new RuntimeException(e);
    }
    if (collectionTarget == null) {
        throw new IllegalArgumentException(
                "Unable to infer the type of the collection from the targetEntity property of a OneToMany or ManyToMany collection.");
    }
    Field collectionTargetField = dynamicEntityDao.getFieldManager().getField(collectionTarget,
            targetObjectProperty);
    ManyToOne manyToOne = collectionTargetField.getAnnotation(ManyToOne.class);
    String ceiling = null;
    checkCeiling: {
        if (manyToOne != null && manyToOne.targetEntity() != void.class) {
            ceiling = manyToOne.targetEntity().getName();
            break checkCeiling;
        }
        ceiling = collectionTargetField.getType().getName();
    }
    if (!StringUtils.isEmpty(ceiling)) {
        metadata.setCollectionCeilingEntity(ceiling);
    }

    String targetObjectIdProperty = null;
    if (serverMetadata != null) {
        targetObjectIdProperty = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getTargetIdProperty();
    }
    if (adornedTargetCollectionMetadata.getTargetObjectIdProperty() != null) {
        targetObjectIdProperty = adornedTargetCollectionMetadata.getTargetObjectIdProperty();
    }
    Boolean isAscending = true;
    if (serverMetadata != null) {
        isAscending = ((AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST))
                        .getSortAscending();
    }
    if (adornedTargetCollectionMetadata.isSortAscending() != null) {
        isAscending = adornedTargetCollectionMetadata.isSortAscending();
    }

    if (serverMetadata != null) {
        AdornedTargetList adornedTargetList = (AdornedTargetList) serverMetadata.getPersistencePerspective()
                .getPersistencePerspectiveItems().get(PersistencePerspectiveItemType.ADORNEDTARGETLIST);
        adornedTargetList.setCollectionFieldName(field.getName());
        adornedTargetList.setLinkedObjectPath(parentObjectProperty);
        adornedTargetList.setLinkedIdProperty(parentObjectIdProperty);
        adornedTargetList.setTargetObjectPath(targetObjectProperty);
        adornedTargetList.setTargetIdProperty(targetObjectIdProperty);
        adornedTargetList.setJoinEntityClass(joinEntityClass);
        adornedTargetList.setIdProperty((String) dynamicEntityDao.getIdMetadata(collectionTarget).get("name"));
        adornedTargetList.setAdornedTargetEntityClassname(collectionTarget.getName());
        adornedTargetList.setSortField(sortProperty);
        adornedTargetList.setSortAscending(isAscending);
        adornedTargetList.setMutable(metadata.isMutable());
    } else {
        AdornedTargetList adornedTargetList = new AdornedTargetList(field.getName(), parentObjectProperty,
                parentObjectIdProperty, targetObjectProperty, targetObjectIdProperty,
                collectionTarget.getName(), sortProperty, isAscending);
        adornedTargetList.setJoinEntityClass(joinEntityClass);
        adornedTargetList.setIdProperty((String) dynamicEntityDao.getIdMetadata(collectionTarget).get("name"));
        adornedTargetList.setMutable(metadata.isMutable());
        persistencePerspective.addPersistencePerspectiveItem(PersistencePerspectiveItemType.ADORNEDTARGETLIST,
                adornedTargetList);
    }

    if (adornedTargetCollectionMetadata.getExcluded() != null) {
        if (LOG.isDebugEnabled()) {
            if (adornedTargetCollectionMetadata.getExcluded()) {
                LOG.debug("buildAdornedTargetCollectionMetadata:Excluding " + field.getName()
                        + " because it was explicitly declared in config");
            } else {
                LOG.debug("buildAdornedTargetCollectionMetadata:Showing " + field.getName()
                        + " because it was explicitly declared in config");
            }
        }
        metadata.setExcluded(adornedTargetCollectionMetadata.getExcluded());
    }
    if (adornedTargetCollectionMetadata.getFriendlyName() != null) {
        metadata.setFriendlyName(adornedTargetCollectionMetadata.getFriendlyName());
    }
    if (adornedTargetCollectionMetadata.getSecurityLevel() != null) {
        metadata.setSecurityLevel(adornedTargetCollectionMetadata.getSecurityLevel());
    }
    if (adornedTargetCollectionMetadata.getOrder() != null) {
        metadata.setOrder(adornedTargetCollectionMetadata.getOrder());
    }

    if (adornedTargetCollectionMetadata.getTab() != null) {
        metadata.setTab(adornedTargetCollectionMetadata.getTab());
    }
    if (adornedTargetCollectionMetadata.getTabOrder() != null) {
        metadata.setTabOrder(adornedTargetCollectionMetadata.getTabOrder());
    }

    if (adornedTargetCollectionMetadata.getCustomCriteria() != null) {
        metadata.setCustomCriteria(adornedTargetCollectionMetadata.getCustomCriteria());
    }

    if (adornedTargetCollectionMetadata.getUseServerSideInspectionCache() != null) {
        persistencePerspective.setUseServerSideInspectionCache(
                adornedTargetCollectionMetadata.getUseServerSideInspectionCache());
    }

    if (adornedTargetCollectionMetadata.isIgnoreAdornedProperties() != null) {
        metadata.setIgnoreAdornedProperties(adornedTargetCollectionMetadata.isIgnoreAdornedProperties());
    }
    if (adornedTargetCollectionMetadata.getCurrencyCodeField() != null) {
        metadata.setCurrencyCodeField(adornedTargetCollectionMetadata.getCurrencyCodeField());
    }

    attributes.put(field.getName(), metadata);
}

From source file:org.apache.axis2.jaxws.server.dispatcher.ProviderDispatcher.java

private Class<?> getProviderType() {

    if (_providerType != null) {
        return _providerType;
    }//from www  . j  av a2s .co m
    Class providerType = null;

    Type[] giTypes = serviceImplClass.getGenericInterfaces();
    for (Type giType : giTypes) {
        ParameterizedType paramType = null;
        try {
            paramType = (ParameterizedType) giType;
        } catch (ClassCastException e) {
            // this may not be a parameterized interface
            continue;
        }
        Class interfaceName = (Class) paramType.getRawType();

        if (interfaceName == javax.xml.ws.Provider.class) {
            if (paramType.getActualTypeArguments().length > 1) {
                throw ExceptionFactory.makeWebServiceException(Messages.getMessage("pTypeErr"));
            }
            providerType = (Class) paramType.getActualTypeArguments()[0];
        }
    }
    _providerType = providerType;
    return providerType;
}

From source file:org.broadinstitute.gatk.utils.help.GenericDocumentationHandler.java

/**
 * Returns a human readable string that describes the Type type of a GATK argument.
 * <p/>/*from   w  w w  .ja  v a  2  s  .c  om*/
 * This will include parameterized types, so that Set{T} shows up as Set(T) and not
 * just Set in the docs.
 *
 * @param type
 * @return
 */
protected String argumentTypeString(Type type) {
    if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
        List<String> subs = new ArrayList<String>();
        for (Type actualType : parameterizedType.getActualTypeArguments())
            subs.add(argumentTypeString(actualType));
        return argumentTypeString(((ParameterizedType) type).getRawType()) + "[" + Utils.join(",", subs) + "]";
    } else if (type instanceof GenericArrayType) {
        return argumentTypeString(((GenericArrayType) type).getGenericComponentType()) + "[]";
    } else if (type instanceof WildcardType) {
        throw new RuntimeException("We don't support wildcards in arguments: " + type);
    } else if (type instanceof Class<?>) {
        return ((Class) type).getSimpleName();
    } else {
        throw new GATKException("Unknown type: " + type);
    }
}