public Attribute<?, ?> filter() { Type<?> type = forModel(metamodel).filter(rootType); Attribute<?, ?> result = null; for (int i = 1; i < pathElements.length; i++) { if (!(type instanceof ManagedType)) { throw new PersistenceException("Cannot navigate through simple property " + pathElements[i] + " of type " + type.getJavaType()); } result = ((ManagedType<?>)type).getAttribute(pathElements[i]); if (result.isCollection()) { type = ((PluralAttribute<?, ?, ?>)result).getElementType(); } else { type = ((SingularAttribute<?, ?>)result).getType(); } } return result; }
public static Type toType(Bindable bindable) { switch ( bindable.getBindableType() ) { case ENTITY_TYPE: { return (EntityType) bindable; } case SINGULAR_ATTRIBUTE: { return ( (SingularAttribute) bindable ).getType(); } case PLURAL_ATTRIBUTE: { return ( (PluralAttribute) bindable ).getElementType(); } default: { throw new ParsingException( "Unexpected Bindable type : " + bindable ); } } }
@Override public Type<?> getIdType() { if ( getHierarchy().getIdentifierDescriptor() instanceof IdentifierDescriptorSingleAttribute ) { final SingularAttribute idAttribute = ( (IdentifierDescriptorSingleAttribute) getHierarchy().getIdentifierDescriptor() ) .getIdAttribute(); if ( idAttribute instanceof SingularAttributeBasic ) { return ( (SingularAttributeBasic) idAttribute ).getOrmType(); } else if ( idAttribute instanceof SingularAttributeEmbedded ) { return ( (SingularAttributeEmbedded) idAttribute ).getEmbeddablePersister(); } else { throw new IllegalStateException( "Expected BASIC or EMBEDDED attribute type for identifier" ); } } return null; }
@Override public Type<java.lang.String> getElementType() { return new Type<java.lang.String>() { @Override public Class<java.lang.String> getJavaType() { return java.lang.String.class; } @Override public javax.persistence.metamodel.Type.PersistenceType getPersistenceType() { return null; } }; }
private Path getSelectedPath(int index, javax.persistence.criteria.Path<?> path) { if (path.getParentPath() != null) { Type<?> type = getType(path.getModel()); if (type.getPersistenceType() == PersistenceType.BASIC || type.getPersistenceType() == PersistenceType.EMBEDDABLE) { return getSelectedPath(index, path.getParentPath()); } return getSelectedPath(index, path.getParentPath()).append(getName(path.getModel())); } if (path.getAlias() == null) { path.alias("alias" + index); } return new Path(path.getAlias()); }
private Class<?> getSelectedType(javax.persistence.criteria.Path<?> path) { Type<?> type = getType(path.getModel()); if (type.getPersistenceType() == PersistenceType.BASIC || type.getPersistenceType() == PersistenceType.EMBEDDABLE) { return getSelectedType(path.getParentPath()); } else { return type.getJavaType(); } }
@SuppressWarnings("unchecked") public static <R> Type<R> getElementType(Attribute<?, ?> attr) { if (attr instanceof SingularAttribute) { return ((SingularAttribute<?,R>) attr).getType(); } else { return ((PluralAttribute<?,?,R>)attr).getElementType(); } }
public static Set<Class> getSubclasses(Class<?> parent, EntityManager entityManager) { return entityManager.getMetamodel().getEntities().stream() .filter(entityType -> parent != entityType.getJavaType() && parent.isAssignableFrom(entityType.getJavaType())) .map(Type::getJavaType) .collect(Collectors.toCollection(HashSet::new)); }
@Before public void initialize() throws ParseException, NoSuchMethodException { Metamodel metamodel = mock(Metamodel.class); SecurePersistenceUnitUtil persistenceUnitUtil = mock(SecurePersistenceUnitUtil.class); accessManager = mock(DefaultAccessManager.class); SecurityContext securityContext = mock(SecurityContext.class); EntityType entityType = mock(EntityType.class); SingularAttribute idAttribute = mock(SingularAttribute.class); SingularAttribute nameAttribute = mock(SingularAttribute.class); SingularAttribute parentAttribute = mock(SingularAttribute.class); PluralAttribute childrenAttribute = mock(PluralAttribute.class); MapAttribute relatedAttribute = mock(MapAttribute.class); Type integerType = mock(Type.class); when(metamodel.getEntities()).thenReturn(Collections.<EntityType<?>>singleton(entityType)); when(metamodel.managedType(MethodAccessTestBean.class)).thenReturn(entityType); when(metamodel.entity(MethodAccessTestBean.class)).thenReturn(entityType); when(accessManager.getContext()).thenReturn(securityContext); when(securityContext.getAliases()).thenReturn(Collections.singleton(CURRENT_PRINCIPAL)); when(securityContext.getAliasValue(CURRENT_PRINCIPAL)).thenReturn(NAME); when(entityType.getName()).thenReturn(MethodAccessTestBean.class.getSimpleName()); when(entityType.getJavaType()).thenReturn((Class)MethodAccessTestBean.class); when(entityType.getAttributes()).thenReturn(new HashSet(Arrays.asList( idAttribute, nameAttribute, parentAttribute, childrenAttribute, relatedAttribute))); when(entityType.getAttribute("id")).thenReturn(idAttribute); when(entityType.getAttribute("name")).thenReturn(nameAttribute); when(entityType.getAttribute("parent")).thenReturn(parentAttribute); when(entityType.getAttribute("children")).thenReturn(childrenAttribute); when(entityType.getAttribute("related")).thenReturn(relatedAttribute); when(idAttribute.getName()).thenReturn("id"); when(idAttribute.isCollection()).thenReturn(false); when(idAttribute.getType()).thenReturn(integerType); when(idAttribute.getPersistentAttributeType()).thenReturn(PersistentAttributeType.BASIC); when(idAttribute.getJavaType()).thenReturn(Integer.TYPE); when(idAttribute.getJavaMember()).thenReturn(MethodAccessTestBean.class.getDeclaredMethod("getId")); when(nameAttribute.getName()).thenReturn("name"); when(nameAttribute.isCollection()).thenReturn(false); when(nameAttribute.getType()).thenReturn(integerType); when(nameAttribute.getPersistentAttributeType()).thenReturn(PersistentAttributeType.BASIC); when(nameAttribute.getJavaType()).thenReturn(String.class); when(nameAttribute.getJavaMember()).thenReturn(MethodAccessTestBean.class.getDeclaredMethod("getName")); when(parentAttribute.getName()).thenReturn("parent"); when(parentAttribute.isCollection()).thenReturn(false); when(parentAttribute.getType()).thenReturn(entityType); when(parentAttribute.getPersistentAttributeType()).thenReturn(PersistentAttributeType.MANY_TO_ONE); when(parentAttribute.getJavaType()).thenReturn(MethodAccessTestBean.class); when(parentAttribute.getJavaMember()).thenReturn(MethodAccessTestBean.class.getDeclaredMethod("getParent")); when(childrenAttribute.getName()).thenReturn("children"); when(childrenAttribute.isCollection()).thenReturn(true); when(childrenAttribute.getElementType()).thenReturn(entityType); when(childrenAttribute.getJavaMember()) .thenReturn(MethodAccessTestBean.class.getDeclaredMethod("getChildren")); when(relatedAttribute.getName()).thenReturn("related"); when(relatedAttribute.isCollection()).thenReturn(true); when(relatedAttribute.getKeyJavaType()).thenReturn(MethodAccessTestBean.class); when(relatedAttribute.getBindableJavaType()).thenReturn(MethodAccessTestBean.class); when(relatedAttribute.getElementType()).thenReturn(entityType); when(relatedAttribute.getJavaMember()) .thenReturn(MethodAccessTestBean.class.getDeclaredMethod("getRelated")); entityFilter = new EntityFilter(metamodel, persistenceUnitUtil, initializeAccessRules(metamodel)); DefaultAccessManager.Instance.register(accessManager); }
@Override @SuppressWarnings("unchecked") public Type<E> getElementType() { return getElementDescriptor(); }
@Override public Type getType() { return getEmbeddablePersister(); }
@Override public Type getType() { return getOrmType(); }
@Override public Type<?> getIdType() { return null; }
@Override public javax.persistence.metamodel.Type.PersistenceType getPersistenceType() { return null; }
@Override public Type<T> getType() { return null; }
@Override public Type<String> getElementType() { // TODO Auto-generated method stub return null; }
@Override public Type<java.lang.String> getElementType() { return new ElementType(); }
@Override public javax.persistence.metamodel.Type.PersistenceType getPersistenceType() { return PersistenceType.BASIC; }
/** * Parse an @Entity * * @param entityFactory * @param metadata * @param sessionFactory */ void parse(QEntityFactory entityFactory, EntityType<?> metadata, SessionFactoryImplementor sessionFactory) { this.metamodelEntity = metadata; this.name = metadata.getName(); for (Attribute<?, ?> attribute : metadata.getAttributes()) { parseFields(entityFactory, sessionFactory, null, attribute); } // Parse top-level properties // Add identifier property { if (!metadata.hasSingleIdAttribute()) throw new IllegalArgumentException("@IdClass Entity not supported! " + metadata.getJavaType()); Type idType = metadata.getIdType(); switch (idType.getPersistenceType()) { case BASIC: break; // No action necessary, will be processed like a normal field case EMBEDDABLE: { EmbeddableType<?> emb = (EmbeddableType<?>) idType; parseEmbeddable(entityFactory, sessionFactory, "id", emb); break; } default: throw new IllegalArgumentException("Cannot handle id type: " + idType.getPersistenceType() + ": " + idType); } } // Add links to descendants { final List<QEntity> descendants = entityFactory.getSubclasses(clazz); if (!descendants.isEmpty()) this.descendants = descendants; } // Figure out the id method/field final String idPropertyName = getIdPropertyName(); if (idPropertyName != null) this.idProperty = new PropertyWrapper(clazz, idPropertyName); }
@SuppressWarnings("unchecked") public <E2 extends IEntity<?>> RelationSetAttribute(SetAttribute<? super E, ? super E2> attribute, MetaJpaConstructor<? super E2, R, ?> constructor) { super((A)(Object)attribute, CollectionType.SET, (Type<R>)attribute.getElementType()); this.constructor = (MetaJpaConstructor<? extends IEntity<?>, R, ?>) constructor; }
@Override public Type<Y> getType() { return proxyTarget == null ? null : proxyTarget.getType(); }
PluralAttributeProxy(A proxyTarget, CollectionType ct, Type<Y> et) { super(proxyTarget); this.ct = ct; this.et = et; }
@Override public Type<Y> getElementType() { return et; }
@SuppressWarnings("unchecked") public <E2 extends IEntity<?>> RelationCollectionAttribute(CollectionAttribute<? super E, ? super E2> attribute, MetaJpaConstructor<? super E2, R, ?> constructor) { super((A)(Object)attribute, CollectionType.COLLECTION, (Type<R>)attribute.getElementType()); this.constructor = (MetaJpaConstructor<? extends IEntity<?>, R, ?>) constructor; }
@SuppressWarnings("unchecked") public <E2 extends IEntity<?>> RelationListAttribute(ListAttribute<? super E, ? super E2> attribute, MetaJpaConstructor<? super E2, R, ?> constructor) { super((A)(Object)attribute, CollectionType.LIST, (Type<R>)attribute.getElementType()); this.constructor = (MetaJpaConstructor<? extends IEntity<?>, R, ?>) constructor; }