Search in sources :

Example 51 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project cuba by cuba-platform.

the class MetadataTools method isLazyFetchedLocalAttribute.

/**
 * Determine whether the given property is a local property with a LAZY fetch type.
 */
public boolean isLazyFetchedLocalAttribute(MetaProperty metaProperty) {
    Objects.requireNonNull(metaProperty, "metaProperty is null");
    AnnotatedElement annotatedElement = metaProperty.getAnnotatedElement();
    Basic annotation = annotatedElement.getAnnotation(Basic.class);
    return annotation != null && annotation.fetch() == FetchType.LAZY;
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement)

Example 52 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project cuba by cuba-platform.

the class EntityRestore method buildLayout.

protected void buildLayout() {
    Object value = entities.getValue();
    if (value != null) {
        MetaClass metaClass = (MetaClass) value;
        MetaProperty deleteTsMetaProperty = metaClass.getProperty("deleteTs");
        if (deleteTsMetaProperty != null) {
            if (entitiesTable != null) {
                tablePanel.remove(entitiesTable);
            }
            if (filter != null) {
                tablePanel.remove(filter);
            }
            entitiesTable = uiComponents.create(Table.NAME);
            entitiesTable.setFrame(frame);
            restoreButton = uiComponents.create(Button.class);
            restoreButton.setId("restore");
            restoreButton.setCaption(getMessage("entityRestore.restore"));
            ButtonsPanel buttonsPanel = uiComponents.create(ButtonsPanel.class);
            buttonsPanel.add(restoreButton);
            entitiesTable.setButtonsPanel(buttonsPanel);
            RowsCount rowsCount = uiComponents.create(RowsCount.class);
            entitiesTable.setRowsCount(rowsCount);
            SimpleDateFormat dateTimeFormat = new SimpleDateFormat(getMessage("dateTimeFormat"));
            Function<Object, String> dateTimeFormatter = propertyValue -> {
                if (propertyValue == null) {
                    return StringUtils.EMPTY;
                }
                return dateTimeFormat.format(propertyValue);
            };
            // collect properties in order to add non-system columns first
            LinkedList<Table.Column<Entity>> nonSystemPropertyColumns = new LinkedList<>();
            LinkedList<Table.Column<Entity>> systemPropertyColumns = new LinkedList<>();
            List<MetaProperty> metaProperties = new ArrayList<>();
            for (MetaProperty metaProperty : metaClass.getProperties()) {
                // don't show embedded, transient & multiple referred entities
                Range range = metaProperty.getRange();
                if (isEmbedded(metaProperty) || metadataTools.isNotPersistent(metaProperty) || range.getCardinality().isMany() || metadataTools.isSystemLevel(metaProperty) || (range.isClass() && metadataTools.isSystemLevel(range.asClass()))) {
                    continue;
                }
                metaProperties.add(metaProperty);
                Table.Column<Entity> column = new Table.Column<>(metaClass.getPropertyPath(metaProperty.getName()));
                if (!metadataTools.isSystem(metaProperty)) {
                    column.setCaption(getPropertyCaption(metaClass, metaProperty));
                    nonSystemPropertyColumns.add(column);
                } else {
                    column.setCaption(metaProperty.getName());
                    column.setDescription(getSystemAttributeDescription(metaClass, metaProperty));
                    systemPropertyColumns.add(column);
                }
                if (range.isDatatype() && range.asDatatype().getJavaClass().equals(Date.class)) {
                    column.setFormatter(dateTimeFormatter);
                }
            }
            for (Table.Column<Entity> column : nonSystemPropertyColumns) {
                entitiesTable.addColumn(column);
            }
            for (Table.Column<Entity> column : systemPropertyColumns) {
                entitiesTable.addColumn(column);
            }
            DsContext dsContext = getDsContext();
            if (entitiesDs != null) {
                ((DsContextImplementation) dsContext).unregister(entitiesDs);
            }
            entitiesDs = DsBuilder.create(dsContext).setId("entitiesDs").setMetaClass(metaClass).setView(buildView(metaClass, metaProperties)).buildGroupDatasource();
            entitiesDs.setQuery("select e from " + metaClass.getName() + " e " + "where e.deleteTs is not null order by e.deleteTs");
            entitiesDs.setSoftDeletion(false);
            entitiesDs.refresh();
            entitiesTable.setDatasource(entitiesDs);
            String filterId = metaClass.getName().replace("$", "") + "GenericFilter";
            filter = uiComponents.create(Filter.class);
            filter.setId(filterId);
            filter.setFrame(getFrame());
            StringBuilder sb = new StringBuilder();
            for (MetaProperty property : metaClass.getProperties()) {
                AnnotatedElement annotatedElement = property.getAnnotatedElement();
                if (annotatedElement.getAnnotation(com.haulmont.chile.core.annotations.MetaProperty.class) != null) {
                    sb.append(property.getName()).append("|");
                }
            }
            Element filterElement = dom4JTools.readDocument(String.format("<filter id=\"%s\">\n" + "    <properties include=\".*\" exclude=\"\"/>\n" + "</filter>", filterId)).getRootElement();
            String excludedProperties = sb.toString();
            if (StringUtils.isNotEmpty(excludedProperties)) {
                Element properties = filterElement.element("properties");
                properties.attribute("exclude").setValue(excludedProperties.substring(0, excludedProperties.lastIndexOf("|")));
            }
            ((HasXmlDescriptor) filter).setXmlDescriptor(filterElement);
            filter.setUseMaxResults(true);
            filter.setDatasource(entitiesDs);
            entitiesTable.setSizeFull();
            entitiesTable.setMultiSelect(true);
            Action restoreAction = new ItemTrackingAction("restore").withCaption(getMessage("entityRestore.restore")).withPrimary(true).withHandler(event -> showRestoreDialog());
            entitiesTable.addAction(restoreAction);
            restoreButton.setAction(restoreAction);
            tablePanel.add(filter);
            tablePanel.add(entitiesTable);
            tablePanel.expand(entitiesTable, "100%", "100%");
            entitiesTable.refresh();
            ((FilterImplementation) filter).loadFiltersAndApplyDefault();
        }
    }
}
Also used : SoftDelete(com.haulmont.cuba.core.entity.SoftDelete) java.util(java.util) Dom4jTools(com.haulmont.cuba.core.sys.xmlparsing.Dom4jTools) DsBuilder(com.haulmont.cuba.gui.data.DsBuilder) EntityRestoreService(com.haulmont.cuba.core.app.EntityRestoreService) SimpleDateFormat(java.text.SimpleDateFormat) EnableRestore(com.haulmont.cuba.core.entity.annotation.EnableRestore) StringUtils(org.apache.commons.lang3.StringUtils) Function(java.util.function.Function) MetaClass(com.haulmont.chile.core.model.MetaClass) com.haulmont.cuba.core.global(com.haulmont.cuba.core.global) Inject(javax.inject.Inject) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) GroupDatasource(com.haulmont.cuba.gui.data.GroupDatasource) com.haulmont.cuba.gui.components(com.haulmont.cuba.gui.components) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) Range(com.haulmont.chile.core.model.Range) MetaProperty(com.haulmont.chile.core.model.MetaProperty) Status(com.haulmont.cuba.gui.components.Action.Status) Type(com.haulmont.cuba.gui.components.DialogAction.Type) Element(org.dom4j.Element) UiComponents(com.haulmont.cuba.gui.UiComponents) Entity(com.haulmont.cuba.core.entity.Entity) AnnotatedElement(java.lang.reflect.AnnotatedElement) Entity(com.haulmont.cuba.core.entity.Entity) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) DsContext(com.haulmont.cuba.gui.data.DsContext) ItemTrackingAction(com.haulmont.cuba.gui.components.actions.ItemTrackingAction) Element(org.dom4j.Element) AnnotatedElement(java.lang.reflect.AnnotatedElement) AnnotatedElement(java.lang.reflect.AnnotatedElement) MetaProperty(com.haulmont.chile.core.model.MetaProperty) DsContextImplementation(com.haulmont.cuba.gui.data.impl.DsContextImplementation) Range(com.haulmont.chile.core.model.Range) MetaClass(com.haulmont.chile.core.model.MetaClass) SimpleDateFormat(java.text.SimpleDateFormat)

Example 53 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project cuba by cuba-platform.

the class CompanionDependencyInjector method inject.

public void inject() {
    Map<AnnotatedElement, Class> toInject = new HashMap<>();
    List<Class<?>> classes = ClassUtils.getAllSuperclasses(companion.getClass());
    classes.add(0, companion.getClass());
    Collections.reverse(classes);
    for (Field field : getAllFields(classes)) {
        Class aClass = injectionAnnotation(field);
        if (aClass != null) {
            toInject.put(field, aClass);
        }
    }
    for (Method method : companion.getClass().getMethods()) {
        Class aClass = injectionAnnotation(method);
        if (aClass != null) {
            toInject.put(method, aClass);
        }
    }
    for (Map.Entry<AnnotatedElement, Class> entry : toInject.entrySet()) {
        doInjection(entry.getKey(), entry.getValue());
    }
}
Also used : Field(java.lang.reflect.Field) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method)

Example 54 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project junit5 by junit-team.

the class TempDirectory method getPathOrFile.

private Object getPathOrFile(AnnotatedElement sourceElement, Class<?> type, CleanupMode cleanupMode, ExtensionContext extensionContext) {
    Namespace namespace = // 
    getScope(extensionContext) == Scope.PER_DECLARATION ? // 
    NAMESPACE.append(sourceElement) : NAMESPACE;
    Path path = // 
    extensionContext.getStore(namespace).getOrComputeIfAbsent(KEY, __ -> createTempDir(cleanupMode, extensionContext), // 
    CloseablePath.class).get();
    return (type == Path.class) ? path : path.toFile();
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) ReflectionUtils.makeAccessible(org.junit.platform.commons.util.ReflectionUtils.makeAccessible) AnnotationUtils.findAnnotation(org.junit.platform.commons.util.AnnotationUtils.findAnnotation) CloseableResource(org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource) ON_SUCCESS(org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS) JupiterConfiguration(org.junit.jupiter.engine.config.JupiterConfiguration) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Constructor(java.lang.reflect.Constructor) LoggerFactory(org.junit.platform.commons.logging.LoggerFactory) NEVER(org.junit.jupiter.api.io.CleanupMode.NEVER) Parameter(java.lang.reflect.Parameter) Path(java.nio.file.Path) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) Logger(org.junit.platform.commons.logging.Logger) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) AnnotationUtils.findAnnotatedFields(org.junit.platform.commons.util.AnnotationUtils.findAnnotatedFields) CleanupMode(org.junit.jupiter.api.io.CleanupMode) Field(java.lang.reflect.Field) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) CONTINUE(java.nio.file.FileVisitResult.CONTINUE) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) Collectors.joining(java.util.stream.Collectors.joining) File(java.io.File) FileVisitResult(java.nio.file.FileVisitResult) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) DEFAULT(org.junit.jupiter.api.io.CleanupMode.DEFAULT) EnumConfigurationParameterConverter(org.junit.jupiter.engine.config.EnumConfigurationParameterConverter) TreeMap(java.util.TreeMap) BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) TempDir(org.junit.jupiter.api.io.TempDir) ExceptionUtils(org.junit.platform.commons.util.ExceptionUtils) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) TEMP_DIR_SCOPE_PROPERTY_NAME(org.junit.jupiter.engine.config.JupiterConfiguration.TEMP_DIR_SCOPE_PROPERTY_NAME) Collections(java.util.Collections) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) SortedMap(java.util.SortedMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace)

Example 55 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project jersey by jersey.

the class ParamInjectionResolver method hasEncodedAnnotation.

private boolean hasEncodedAnnotation(Injectee injectee) {
    AnnotatedElement element = injectee.getParent();
    final boolean isConstructor = element instanceof Constructor;
    final boolean isMethod = element instanceof Method;
    // if injectee is method or constructor, check its parameters
    if (isConstructor || isMethod) {
        Annotation[] annotations;
        if (isMethod) {
            annotations = ((Method) element).getParameterAnnotations()[injectee.getPosition()];
        } else {
            annotations = ((Constructor) element).getParameterAnnotations()[injectee.getPosition()];
        }
        for (Annotation annotation : annotations) {
            if (annotation.annotationType().equals(Encoded.class)) {
                return true;
            }
        }
    }
    // check injectee itself (method, constructor or field)
    if (element.isAnnotationPresent(Encoded.class)) {
        return true;
    }
    // check class which contains injectee
    Class<?> clazz = injectee.getInjecteeClass();
    return clazz.isAnnotationPresent(Encoded.class);
}
Also used : Constructor(java.lang.reflect.Constructor) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation)

Aggregations

AnnotatedElement (java.lang.reflect.AnnotatedElement)106 Method (java.lang.reflect.Method)23 Annotation (java.lang.annotation.Annotation)17 Field (java.lang.reflect.Field)17 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)11 HashMap (java.util.HashMap)9 Test (org.junit.jupiter.api.Test)8 Member (java.lang.reflect.Member)7 LinkedHashSet (java.util.LinkedHashSet)7 List (java.util.List)7 Constructor (java.lang.reflect.Constructor)6 Type (java.lang.reflect.Type)6 Map (java.util.Map)6 HashSet (java.util.HashSet)5 By (org.openqa.selenium.By)5 Statement (org.junit.runners.model.Statement)4 FindBy (org.openqa.selenium.support.FindBy)4 EntityType (com.querydsl.codegen.EntityType)3 Collection (java.util.Collection)3