idx int64 0 165k | question stringlengths 73 5.81k | target stringlengths 5 918 |
|---|---|---|
14,300 | private MethodHandle findIdReadMethod ( ) { try { Method readMethod = clazz . getMethod ( READ_METHOD_NAME ) ; Class < ? > dataClass = readMethod . getReturnType ( ) ; DataType dataType = IdentifierMetadata . DataType . forClass ( dataClass ) ; if ( dataType == null ) { String pattern = "Method %s in class %s must have a return type of long, Long or String" ; String error = String . format ( pattern , READ_METHOD_NAME , clazz . getName ( ) ) ; throw new EntityManagerException ( error ) ; } return MethodHandles . lookup ( ) . unreflect ( readMethod ) ; } catch ( NoSuchMethodException | SecurityException | IllegalAccessException exp ) { String error = String . format ( "Class %s must have a public %s method" , clazz . getName ( ) , READ_METHOD_NAME ) ; throw new EntityManagerException ( error , exp ) ; } } | Creates and returns the MethodHandle for reading the underlying ID . |
14,301 | private MethodHandle findConstructor ( ) { try { MethodHandle mh = MethodHandles . publicLookup ( ) . findConstructor ( clazz , MethodType . methodType ( void . class , getIdType ( ) ) ) ; return mh ; } catch ( NoSuchMethodException | IllegalAccessException exp ) { String pattern = "Class %s requires a public constructor with one parameter of type %s" ; String error = String . format ( pattern , clazz . getName ( ) , getIdType ( ) ) ; throw new EntityManagerException ( error , exp ) ; } } | Creates and returns the MethodHandle for the constructor . |
14,302 | public static Object instantiate ( MetadataBase metadata ) { try { return metadata . getConstructorMetadata ( ) . getConstructorMethodHandle ( ) . invoke ( ) ; } catch ( Throwable t ) { throw new EntityManagerException ( t ) ; } } | Creates and returns a new instance of a persistence class for the given metadata . The returned object will be an instance of the primary persistence class or its Builder . |
14,303 | public static PropertyMetadata getPropertyMetadata ( Field field ) { Property property = field . getAnnotation ( Property . class ) ; try { PropertyMetadata propertyMetadata = new PropertyMetadata ( field ) ; return propertyMetadata ; } catch ( NoAccessorMethodException | NoMutatorMethodException exp ) { if ( property != null ) { throw exp ; } } return null ; } | Returns the metadata for the given field . |
14,304 | public static Object instantiateObject ( Class < ? > clazz ) { try { Constructor < ? > constructor = clazz . getConstructor ( ) ; return constructor . newInstance ( ) ; } catch ( Exception exp ) { throw new EntityManagerException ( exp ) ; } } | Creates a new object of given class by invoking the class default public constructor . |
14,305 | public static Object getFieldValue ( FieldMetadata fieldMetadata , Object target ) { MethodHandle readMethod = fieldMetadata . getReadMethod ( ) ; try { return readMethod . invoke ( target ) ; } catch ( Throwable t ) { throw new EntityManagerException ( t . getMessage ( ) , t ) ; } } | Returns the value of the field represented by the given metadata . |
14,306 | public static MethodHandle findStaticMethod ( Class < ? > clazz , String methodName , Class < ? > expectedReturnType , Class < ? > ... expectedParameterTypes ) { return findMethod ( clazz , methodName , true , expectedReturnType , expectedParameterTypes ) ; } | Finds and returns a MethodHandle for a public static method . |
14,307 | public static MethodHandle findInstanceMethod ( Class < ? > clazz , String methodName , Class < ? > expectedReturnType , Class < ? > ... expectedParameterTypes ) { return findMethod ( clazz , methodName , false , expectedReturnType , expectedParameterTypes ) ; } | Finds and returns a MethodHandle for a public instance method . |
14,308 | private static MethodHandle findMethod ( Class < ? > clazz , String methodName , boolean staticMethod , Class < ? > expectedReturnType , Class < ? > ... expectedParameterTypes ) { MethodHandle methodHandle = null ; try { Method method = clazz . getMethod ( methodName , expectedParameterTypes ) ; int modifiers = method . getModifiers ( ) ; Class < ? > returnType = method . getReturnType ( ) ; if ( Modifier . isStatic ( modifiers ) != staticMethod ) { throw new NoSuchMethodException ( ) ; } if ( expectedReturnType != null && ! expectedReturnType . isAssignableFrom ( returnType ) ) { throw new NoSuchMethodException ( ) ; } methodHandle = MethodHandles . publicLookup ( ) . unreflect ( method ) ; } catch ( NoSuchMethodException | SecurityException | IllegalAccessException e ) { } return methodHandle ; } | Finds and returns a method handle for the given criteria . |
14,309 | public void setOptional ( boolean optional ) { if ( field . getType ( ) . isPrimitive ( ) || field . isAnnotationPresent ( Version . class ) || field . isAnnotationPresent ( CreatedTimestamp . class ) || field . isAnnotationPresent ( UpdatedTimestamp . class ) ) { this . optional = false ; } else { this . optional = optional ; } } | Sets whether or not the field represented by this metadata is optional . |
14,310 | private void initializeSecondaryIndexer ( ) { SecondaryIndex secondaryIndexAnnotation = field . getAnnotation ( SecondaryIndex . class ) ; if ( secondaryIndexAnnotation == null ) { return ; } String indexName = secondaryIndexAnnotation . name ( ) ; if ( indexName == null || indexName . trim ( ) . length ( ) == 0 ) { indexName = DEFAULT_SECONDARY_INDEX_PREFIX + mappedName ; } this . secondaryIndexName = indexName ; try { secondaryIndexer = IndexerFactory . getInstance ( ) . getIndexer ( field ) ; } catch ( Exception exp ) { String pattern = "No suitable Indexer found or error occurred while creating the indexer " + "for field %s in class %s" ; String message = String . format ( pattern , field . getName ( ) , field . getDeclaringClass ( ) . getName ( ) ) ; throw new EntityManagerException ( message , exp ) ; } } | Initializes the secondary indexer for this property if any . |
14,311 | public static EntityListenersMetadata introspect ( Class < ? > entityClass ) { EntityListenersIntrospector introspector = new EntityListenersIntrospector ( entityClass ) ; introspector . introspect ( ) ; return introspector . metadata ; } | Returns the metadata of various registered listeners for the given entity class . |
14,312 | private List < Class < ? > > getAllExternalListeners ( ) { Class < ? > clazz = entityClass ; List < Class < ? > > allListeners = new ArrayList < > ( ) ; boolean stop = false ; while ( ! stop ) { EntityListeners entityListenersAnnotation = clazz . getAnnotation ( EntityListeners . class ) ; if ( entityListenersAnnotation != null ) { Class < ? > [ ] listeners = entityListenersAnnotation . value ( ) ; if ( listeners . length > 0 ) { allListeners . addAll ( 0 , Arrays . asList ( listeners ) ) ; } } boolean excludeDefaultListeners = clazz . isAnnotationPresent ( ExcludeDefaultListeners . class ) ; boolean excludeSuperClassListeners = clazz . isAnnotationPresent ( ExcludeSuperclassListeners . class ) ; if ( excludeDefaultListeners ) { metadata . setExcludeDefaultListeners ( true ) ; } if ( excludeSuperClassListeners ) { metadata . setExcludeSuperClassListeners ( true ) ; } clazz = clazz . getSuperclass ( ) ; stop = excludeSuperClassListeners || clazz == null || ! clazz . isAnnotationPresent ( MappedSuperClass . class ) ; } return allListeners ; } | Inspects the entity hierarchy and returns all external listeners . |
14,313 | private void processExternalListener ( Class < ? > listenerClass ) { ExternalListenerMetadata listenerMetadata = ExternalListenerIntrospector . introspect ( listenerClass ) ; Map < CallbackType , Method > callbacks = listenerMetadata . getCallbacks ( ) ; if ( callbacks != null ) { for ( Map . Entry < CallbackType , Method > entry : callbacks . entrySet ( ) ) { validateExternalCallback ( entry . getValue ( ) , entry . getKey ( ) ) ; } } } | Introspects the given listener class and finds all methods that should receive callback event notifications . |
14,314 | private void validateExternalCallback ( Method method , CallbackType callbackType ) { Class < ? > [ ] parameters = method . getParameterTypes ( ) ; if ( ! parameters [ 0 ] . isAssignableFrom ( entityClass ) ) { String message = String . format ( "Method %s in class %s is not valid for entity %s" , method . getName ( ) , method . getDeclaringClass ( ) . getName ( ) , entityClass . getName ( ) ) ; throw new EntityManagerException ( message ) ; } CallbackMetadata callbackMetadata = new CallbackMetadata ( EntityListenerType . EXTERNAL , callbackType , method ) ; metadata . put ( callbackType , callbackMetadata ) ; } | Validates and registers the given callback method . |
14,315 | private void processInternalListeners ( ) { List < Class < ? > > internalListeners = getAllInternalListeners ( ) ; for ( Class < ? > internalListener : internalListeners ) { processInternalListener ( internalListener ) ; } } | Introspects the entity class hierarchy for any internal callback methods and updates the metadata . |
14,316 | private List < Class < ? > > getAllInternalListeners ( ) { Class < ? > clazz = entityClass ; List < Class < ? > > allListeners = new ArrayList < > ( ) ; boolean stop = false ; while ( ! stop ) { allListeners . add ( 0 , clazz ) ; boolean excludeSuperClassListeners = clazz . isAnnotationPresent ( ExcludeSuperclassListeners . class ) ; clazz = clazz . getSuperclass ( ) ; stop = excludeSuperClassListeners || clazz == null || ! clazz . isAnnotationPresent ( MappedSuperClass . class ) ; } return allListeners ; } | Traverses up the entity class hierarchy and returns the list of all classes that may potentially have callback listeners . |
14,317 | private void processInternalListener ( Class < ? > listenerClass ) { InternalListenerMetadata listenerMetadata = InternalListenerIntrospector . introspect ( listenerClass ) ; Map < CallbackType , Method > callbacks = listenerMetadata . getCallbacks ( ) ; if ( callbacks != null ) { for ( Map . Entry < CallbackType , Method > entry : callbacks . entrySet ( ) ) { CallbackType callbackType = entry . getKey ( ) ; Method callbackMethod = entry . getValue ( ) ; CallbackMetadata callbackMetadata = new CallbackMetadata ( EntityListenerType . INTERNAL , callbackType , callbackMethod ) ; metadata . put ( callbackType , callbackMetadata ) ; } } } | Processes the given class and finds any internal callbacks . |
14,318 | public Object getListener ( Class < ? > listenerClass ) { Object listener = listeners . get ( listenerClass ) ; if ( listener == null ) { listener = loadListener ( listenerClass ) ; } return listener ; } | Returns the listener object for the given class . |
14,319 | private Object loadListener ( Class < ? > listenerClass ) { synchronized ( listenerClass ) { Object listener = listeners . get ( listenerClass ) ; if ( listener == null ) { listener = IntrospectionUtils . instantiateObject ( listenerClass ) ; listeners . put ( listenerClass , listener ) ; } return listener ; } } | Instantiates the listener of given type and caches it . |
14,320 | public static EmbeddableMetadata introspect ( Class < ? > embeddableClass ) { EmbeddableIntrospector introspector = new EmbeddableIntrospector ( embeddableClass ) ; introspector . introspect ( ) ; return introspector . metadata ; } | Introspects the given Embeddable class and returns the metadata . |
14,321 | private void introspect ( ) { if ( ! embeddableClass . isAnnotationPresent ( Embeddable . class ) ) { String message = String . format ( "Class %s must have %s annotation" , embeddableClass . getName ( ) , Embeddable . class . getName ( ) ) ; throw new EntityManagerException ( message ) ; } metadata = new EmbeddableMetadata ( embeddableClass ) ; processFields ( ) ; } | Introspects the Embeddable . |
14,322 | private void processFields ( ) { List < Field > fields = IntrospectionUtils . getPersistableFields ( embeddableClass ) ; for ( Field field : fields ) { if ( field . isAnnotationPresent ( Embedded . class ) ) { processEmbeddedField ( field ) ; } else { processSimpleField ( field ) ; } } } | Processes each field in this Embeddable and updates the metadata . |
14,323 | public static EmbeddedMetadata introspect ( EmbeddedField field , EntityMetadata entityMetadata ) { EmbeddedIntrospector introspector = new EmbeddedIntrospector ( field , entityMetadata ) ; introspector . process ( ) ; return introspector . metadata ; } | Introspects the given embedded field and returns its metadata . |
14,324 | private void process ( ) { metadata = new EmbeddedMetadata ( field ) ; if ( ! clazz . isAnnotationPresent ( Embeddable . class ) ) { String message = String . format ( "Class %s must have %s annotation" , clazz . getName ( ) , Embeddable . class . getName ( ) ) ; throw new EntityManagerException ( message ) ; } Embedded embeddedAnnotation = field . getField ( ) . getAnnotation ( Embedded . class ) ; String mappedName = embeddedAnnotation . name ( ) ; if ( mappedName == null || mappedName . trim ( ) . length ( ) == 0 ) { mappedName = field . getName ( ) ; } metadata . setMappedName ( mappedName ) ; metadata . setIndexed ( embeddedAnnotation . indexed ( ) ) ; metadata . setOptional ( embeddedAnnotation . optional ( ) ) ; if ( field . getField ( ) . isAnnotationPresent ( Imploded . class ) ) { metadata . setStorageStrategy ( StorageStrategy . IMPLODED ) ; } processFields ( ) ; } | Worker class for introspection . |
14,325 | private void processFields ( ) { List < Field > children = IntrospectionUtils . getPersistableFields ( clazz ) ; for ( Field child : children ) { if ( child . isAnnotationPresent ( Embedded . class ) ) { processEmbeddedField ( child ) ; } else { processSimpleField ( child ) ; } } } | Processes each field in this embedded object and updates the metadata . |
14,326 | private void processPropertyOverride ( PropertyMetadata propertyMetadata ) { String qualifiedName = field . getQualifiedName ( ) + "." + propertyMetadata . getField ( ) . getName ( ) ; Property override = entityMetadata . getPropertyOverride ( qualifiedName ) ; if ( override != null ) { String mappedName = override . name ( ) ; if ( mappedName != null && mappedName . trim ( ) . length ( ) > 0 ) { propertyMetadata . setMappedName ( mappedName ) ; } propertyMetadata . setIndexed ( override . indexed ( ) ) ; propertyMetadata . setOptional ( override . optional ( ) ) ; } } | Processes the override if any for the given property . |
14,327 | private void validateIntent ( ) { if ( entityMetadata . isProjectedEntity ( ) && ! intent . isValidOnProjectedEntities ( ) ) { String message = String . format ( "Operation %s is not allowed for ProjectedEntity %s" , intent , entity . getClass ( ) . getName ( ) ) ; throw new EntityManagerException ( message ) ; } } | Validates if the Intent is legal for the entity being marshalled . |
14,328 | private BaseEntity < ? > marshal ( ) { marshalKey ( ) ; if ( key instanceof Key ) { entityBuilder = Entity . newBuilder ( ( Key ) key ) ; } else { entityBuilder = FullEntity . newBuilder ( key ) ; } marshalFields ( ) ; marshalAutoTimestampFields ( ) ; if ( intent == Intent . UPDATE ) { marshalVersionField ( ) ; } marshalEmbeddedFields ( ) ; return entityBuilder . build ( ) ; } | Marshals the given entity and and returns the equivalent Entity needed for the underlying Cloud Datastore API . |
14,329 | public static Key marshalKey ( DefaultEntityManager entityManager , Object entity ) { Marshaller marshaller = new Marshaller ( entityManager , entity , Intent . DELETE ) ; marshaller . marshalKey ( ) ; return ( Key ) marshaller . key ; } | Extracts the key from the given object entity and returns it . The entity must have its ID set . |
14,330 | private void marshalKey ( ) { Key parent = null ; ParentKeyMetadata parentKeyMetadata = entityMetadata . getParentKeyMetadata ( ) ; if ( parentKeyMetadata != null ) { DatastoreKey parentDatastoreKey = ( DatastoreKey ) getFieldValue ( parentKeyMetadata ) ; if ( parentDatastoreKey != null ) { parent = parentDatastoreKey . nativeKey ( ) ; } } IdentifierMetadata identifierMetadata = entityMetadata . getIdentifierMetadata ( ) ; IdClassMetadata idClassMetadata = identifierMetadata . getIdClassMetadata ( ) ; DataType identifierType = identifierMetadata . getDataType ( ) ; Object idValue = getFieldValue ( identifierMetadata ) ; if ( idValue != null && idClassMetadata != null ) { try { idValue = idClassMetadata . getReadMethod ( ) . invoke ( idValue ) ; } catch ( Throwable t ) { throw new EntityManagerException ( t ) ; } } boolean validId = isValidId ( idValue , identifierType ) ; boolean autoGenerateId = identifierMetadata . isAutoGenerated ( ) ; if ( validId ) { if ( identifierType == DataType . STRING ) { createCompleteKey ( parent , ( String ) idValue ) ; } else { createCompleteKey ( parent , ( long ) idValue ) ; } } else { if ( intent . isKeyRequired ( ) ) { throw new EntityManagerException ( String . format ( "Identifier is not set or valid for entity of type %s" , entity . getClass ( ) ) ) ; } if ( ! autoGenerateId ) { String pattern = "Identifier is not set or valid for entity of type %s. Auto generation " + "of ID is explicitly turned off. " ; throw new EntityManagerException ( String . format ( pattern , entity . getClass ( ) ) ) ; } else { if ( identifierType == DataType . STRING ) { createCompleteKey ( parent ) ; } else { createIncompleteKey ( parent ) ; } } } } | Marshals the key . |
14,331 | private static boolean isValidId ( Object idValue , DataType identifierType ) { boolean validId = false ; if ( idValue != null ) { switch ( identifierType ) { case LONG : case LONG_OBJECT : validId = ( long ) idValue != 0 ; break ; case STRING : validId = ( ( String ) idValue ) . trim ( ) . length ( ) > 0 ; break ; default : break ; } } return validId ; } | Checks to see if the given value is a valid identifier for the given ID type . |
14,332 | private void createCompleteKey ( Key parent , long id ) { String kind = entityMetadata . getKind ( ) ; if ( parent == null ) { key = entityManager . newNativeKeyFactory ( ) . setKind ( kind ) . newKey ( id ) ; } else { key = Key . newBuilder ( parent , kind , id ) . build ( ) ; } } | Creates a complete key using the given parameters . |
14,333 | private void createIncompleteKey ( Key parent ) { String kind = entityMetadata . getKind ( ) ; if ( parent == null ) { key = entityManager . newNativeKeyFactory ( ) . setKind ( kind ) . newKey ( ) ; } else { key = IncompleteKey . newBuilder ( parent , kind ) . build ( ) ; } } | Creates an IncompleteKey . |
14,334 | private void marshalFields ( ) { Collection < PropertyMetadata > propertyMetadataCollection = entityMetadata . getPropertyMetadataCollection ( ) ; for ( PropertyMetadata propertyMetadata : propertyMetadataCollection ) { marshalField ( propertyMetadata , entity ) ; } } | Marshals all the fields . |
14,335 | private static void marshalField ( PropertyMetadata propertyMetadata , Object target , BaseEntity . Builder < ? , ? > entityBuilder ) { Object fieldValue = IntrospectionUtils . getFieldValue ( propertyMetadata , target ) ; if ( fieldValue == null && propertyMetadata . isOptional ( ) ) { return ; } ValueBuilder < ? , ? , ? > valueBuilder = propertyMetadata . getMapper ( ) . toDatastore ( fieldValue ) ; if ( valueBuilder . getValueType ( ) != ValueType . LIST ) { valueBuilder . setExcludeFromIndexes ( ! propertyMetadata . isIndexed ( ) ) ; } Value < ? > datastoreValue = valueBuilder . build ( ) ; entityBuilder . set ( propertyMetadata . getMappedName ( ) , datastoreValue ) ; Indexer indexer = propertyMetadata . getSecondaryIndexer ( ) ; if ( indexer != null ) { entityBuilder . set ( propertyMetadata . getSecondaryIndexName ( ) , indexer . index ( datastoreValue ) ) ; } } | Marshals the field with the given property metadata . |
14,336 | private void marshalEmbeddedFields ( ) { for ( EmbeddedMetadata embeddedMetadata : entityMetadata . getEmbeddedMetadataCollection ( ) ) { if ( embeddedMetadata . getStorageStrategy ( ) == StorageStrategy . EXPLODED ) { marshalWithExplodedStrategy ( embeddedMetadata , entity ) ; } else { ValueBuilder < ? , ? , ? > embeddedEntityBuilder = marshalWithImplodedStrategy ( embeddedMetadata , entity ) ; if ( embeddedEntityBuilder != null ) { entityBuilder . set ( embeddedMetadata . getMappedName ( ) , embeddedEntityBuilder . build ( ) ) ; } } } } | Marshals the embedded fields . |
14,337 | private void marshalWithExplodedStrategy ( EmbeddedMetadata embeddedMetadata , Object target ) { try { Object embeddedObject = initializeEmbedded ( embeddedMetadata , target ) ; for ( PropertyMetadata propertyMetadata : embeddedMetadata . getPropertyMetadataCollection ( ) ) { marshalField ( propertyMetadata , embeddedObject ) ; } for ( EmbeddedMetadata embeddedMetadata2 : embeddedMetadata . getEmbeddedMetadataCollection ( ) ) { marshalWithExplodedStrategy ( embeddedMetadata2 , embeddedObject ) ; } } catch ( Throwable t ) { throw new EntityManagerException ( t ) ; } } | Marshals an embedded field represented by the given metadata . |
14,338 | private ValueBuilder < ? , ? , ? > marshalWithImplodedStrategy ( EmbeddedMetadata embeddedMetadata , Object target ) { try { Object embeddedObject = embeddedMetadata . getReadMethod ( ) . invoke ( target ) ; if ( embeddedObject == null ) { if ( embeddedMetadata . isOptional ( ) ) { return null ; } NullValue . Builder nullValueBuilder = NullValue . newBuilder ( ) ; nullValueBuilder . setExcludeFromIndexes ( ! embeddedMetadata . isIndexed ( ) ) ; return nullValueBuilder ; } FullEntity . Builder < IncompleteKey > embeddedEntityBuilder = FullEntity . newBuilder ( ) ; for ( PropertyMetadata propertyMetadata : embeddedMetadata . getPropertyMetadataCollection ( ) ) { marshalField ( propertyMetadata , embeddedObject , embeddedEntityBuilder ) ; } for ( EmbeddedMetadata embeddedMetadata2 : embeddedMetadata . getEmbeddedMetadataCollection ( ) ) { ValueBuilder < ? , ? , ? > embeddedEntityBuilder2 = marshalWithImplodedStrategy ( embeddedMetadata2 , embeddedObject ) ; if ( embeddedEntityBuilder2 != null ) { embeddedEntityBuilder . set ( embeddedMetadata2 . getMappedName ( ) , embeddedEntityBuilder2 . build ( ) ) ; } } EntityValue . Builder valueBuilder = EntityValue . newBuilder ( embeddedEntityBuilder . build ( ) ) ; valueBuilder . setExcludeFromIndexes ( ! embeddedMetadata . isIndexed ( ) ) ; return valueBuilder ; } catch ( Throwable t ) { throw new EntityManagerException ( t ) ; } } | Marshals the embedded field represented by the given metadata . |
14,339 | private void marshalUpdatedTimestamp ( ) { PropertyMetadata updatedTimestampMetadata = entityMetadata . getUpdatedTimestampMetadata ( ) ; if ( updatedTimestampMetadata != null ) { applyAutoTimestamp ( updatedTimestampMetadata , System . currentTimeMillis ( ) ) ; } } | Marshals the updated timestamp field . |
14,340 | private void marshalCreatedAndUpdatedTimestamp ( ) { PropertyMetadata createdTimestampMetadata = entityMetadata . getCreatedTimestampMetadata ( ) ; PropertyMetadata updatedTimestampMetadata = entityMetadata . getUpdatedTimestampMetadata ( ) ; long millis = System . currentTimeMillis ( ) ; if ( createdTimestampMetadata != null ) { applyAutoTimestamp ( createdTimestampMetadata , millis ) ; } if ( updatedTimestampMetadata != null ) { applyAutoTimestamp ( updatedTimestampMetadata , millis ) ; } } | Marshals both created and updated timestamp fields . |
14,341 | private void marshalVersionField ( ) { PropertyMetadata versionMetadata = entityMetadata . getVersionMetadata ( ) ; if ( versionMetadata != null ) { long version = ( long ) IntrospectionUtils . getFieldValue ( versionMetadata , entity ) ; ValueBuilder < ? , ? , ? > valueBuilder = versionMetadata . getMapper ( ) . toDatastore ( version + 1 ) ; valueBuilder . setExcludeFromIndexes ( ! versionMetadata . isIndexed ( ) ) ; entityBuilder . set ( versionMetadata . getMappedName ( ) , valueBuilder . build ( ) ) ; } } | Marshals the version field if it exists . The version will be set to one more than the previous value . |
14,342 | private void initializeMapper ( ) { if ( itemClass == null ) { itemMapper = CatchAllMapper . getInstance ( ) ; } else { try { itemMapper = MapperFactory . getInstance ( ) . getMapper ( itemClass ) ; } catch ( NoSuitableMapperException exp ) { itemMapper = CatchAllMapper . getInstance ( ) ; } } } | Initializes the mapper for items in the List . |
14,343 | public void setJsonCredentialsFile ( String jsonCredentialsPath ) { if ( ! Utility . isNullOrEmpty ( jsonCredentialsPath ) ) { setJsonCredentialsFile ( new File ( jsonCredentialsPath ) ) ; } else { setJsonCredentialsFile ( ( File ) null ) ; } } | Sets the JSON credentials path . |
14,344 | private void initializeMapper ( ) { if ( valueClass == null ) { valueMapper = CatchAllMapper . getInstance ( ) ; } else { try { valueMapper = MapperFactory . getInstance ( ) . getMapper ( valueClass ) ; } catch ( NoSuitableMapperException exp ) { valueMapper = CatchAllMapper . getInstance ( ) ; } } } | Initializes the mapper for values in the Map . |
14,345 | public static InternalListenerMetadata introspect ( Class < ? > listenerClass ) { InternalListenerMetadata cachedMetadata = cache . get ( listenerClass ) ; if ( cachedMetadata != null ) { return cachedMetadata ; } synchronized ( listenerClass ) { cachedMetadata = cache . get ( listenerClass ) ; if ( cachedMetadata != null ) { return cachedMetadata ; } InternalListenerIntrospector introspector = new InternalListenerIntrospector ( listenerClass ) ; introspector . introspect ( ) ; cache . put ( listenerClass , introspector . metadata ) ; return introspector . metadata ; } } | Introspects the given class for any defined listeners and returns the metadata . |
14,346 | private void processMethods ( ) { Method [ ] methods = listenerClass . getDeclaredMethods ( ) ; for ( Method method : methods ) { for ( CallbackType callbackType : CallbackType . values ( ) ) { if ( method . isAnnotationPresent ( callbackType . getAnnotationClass ( ) ) ) { validateMethod ( method , callbackType ) ; metadata . putListener ( callbackType , method ) ; } } } } | Processes the methods in the listener class and updates the metadata for any valid methods found . |
14,347 | private void validateMethod ( Method method , CallbackType callbackType ) { int modifiers = method . getModifiers ( ) ; if ( ! Modifier . isPublic ( modifiers ) ) { String message = String . format ( "Method %s in class %s must be public" , method . getName ( ) , method . getDeclaringClass ( ) . getName ( ) ) ; throw new EntityManagerException ( message ) ; } if ( Modifier . isStatic ( modifiers ) ) { String message = String . format ( "Method %s in class %s must not be static" , method . getName ( ) , method . getDeclaringClass ( ) . getName ( ) ) ; throw new EntityManagerException ( message ) ; } if ( Modifier . isAbstract ( modifiers ) ) { String message = String . format ( "Method %s in class %s must not be abstract" , method . getName ( ) , method . getDeclaringClass ( ) . getName ( ) ) ; throw new EntityManagerException ( message ) ; } Class < ? > [ ] parameters = method . getParameterTypes ( ) ; if ( parameters . length != 0 ) { String pattern = "Method %s in class %s is not a valid %s callback method. Method must not " + "have any parameters. " ; String message = String . format ( pattern , method . getName ( ) , method . getDeclaringClass ( ) . getName ( ) , callbackType ) ; throw new EntityManagerException ( message ) ; } if ( method . getReturnType ( ) != void . class ) { String message = String . format ( "Method %s in class %s must have a return type of %s" , method . getName ( ) , method . getDeclaringClass ( ) . getName ( ) , void . class . getName ( ) ) ; throw new EntityManagerException ( message ) ; } } | Validates the given method to ensure if it is a valid callback method for the given event type . |
14,348 | public < E > E insert ( E entity ) { try { entityManager . executeEntityListeners ( CallbackType . PRE_INSERT , entity ) ; FullEntity < ? > nativeEntity = ( FullEntity < ? > ) Marshaller . marshal ( entityManager , entity , Intent . INSERT ) ; Entity insertedNativeEntity = nativeWriter . add ( nativeEntity ) ; @ SuppressWarnings ( "unchecked" ) E insertedEntity = ( E ) Unmarshaller . unmarshal ( insertedNativeEntity , entity . getClass ( ) ) ; entityManager . executeEntityListeners ( CallbackType . POST_INSERT , insertedEntity ) ; return insertedEntity ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Inserts the given entity into the Cloud Datastore . |
14,349 | @ SuppressWarnings ( "unchecked" ) public < E > List < E > insert ( List < E > entities ) { if ( entities == null || entities . isEmpty ( ) ) { return new ArrayList < > ( ) ; } try { entityManager . executeEntityListeners ( CallbackType . PRE_INSERT , entities ) ; FullEntity < ? > [ ] nativeEntities = toNativeFullEntities ( entities , entityManager , Intent . INSERT ) ; Class < ? > entityClass = entities . get ( 0 ) . getClass ( ) ; List < Entity > insertedNativeEntities = nativeWriter . add ( nativeEntities ) ; List < E > insertedEntities = ( List < E > ) toEntities ( entityClass , insertedNativeEntities ) ; entityManager . executeEntityListeners ( CallbackType . POST_INSERT , insertedEntities ) ; return insertedEntities ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Inserts the given list of entities into the Cloud Datastore . |
14,350 | @ SuppressWarnings ( "unchecked" ) public < E > E update ( E entity ) { try { entityManager . executeEntityListeners ( CallbackType . PRE_UPDATE , entity ) ; Intent intent = ( nativeWriter instanceof Batch ) ? Intent . BATCH_UPDATE : Intent . UPDATE ; Entity nativeEntity = ( Entity ) Marshaller . marshal ( entityManager , entity , intent ) ; nativeWriter . update ( nativeEntity ) ; E updatedEntity = ( E ) Unmarshaller . unmarshal ( nativeEntity , entity . getClass ( ) ) ; entityManager . executeEntityListeners ( CallbackType . POST_UPDATE , updatedEntity ) ; return updatedEntity ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Updates the given entity in the Cloud Datastore . The passed in Entity must have its ID set for the update to work . |
14,351 | @ SuppressWarnings ( "unchecked" ) public < E > List < E > update ( List < E > entities ) { if ( entities == null || entities . isEmpty ( ) ) { return new ArrayList < > ( ) ; } try { Class < E > entityClass = ( Class < E > ) entities . get ( 0 ) . getClass ( ) ; entityManager . executeEntityListeners ( CallbackType . PRE_UPDATE , entities ) ; Intent intent = ( nativeWriter instanceof Batch ) ? Intent . BATCH_UPDATE : Intent . UPDATE ; Entity [ ] nativeEntities = toNativeEntities ( entities , entityManager , intent ) ; nativeWriter . update ( nativeEntities ) ; List < E > updatedEntities = toEntities ( entityClass , nativeEntities ) ; entityManager . executeEntityListeners ( CallbackType . POST_UPDATE , updatedEntities ) ; return updatedEntities ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Updates the given list of entities in the Cloud Datastore . |
14,352 | public < E > E updateWithOptimisticLock ( E entity ) { PropertyMetadata versionMetadata = EntityIntrospector . getVersionMetadata ( entity ) ; if ( versionMetadata == null ) { return update ( entity ) ; } else { return updateWithOptimisticLockingInternal ( entity , versionMetadata ) ; } } | Updates the given entity with optimistic locking if the entity is set up to support optimistic locking . Otherwise a normal update is performed . |
14,353 | public < E > List < E > updateWithOptimisticLock ( List < E > entities ) { if ( entities == null || entities . isEmpty ( ) ) { return new ArrayList < > ( ) ; } Class < ? > entityClass = entities . get ( 0 ) . getClass ( ) ; PropertyMetadata versionMetadata = EntityIntrospector . getVersionMetadata ( entityClass ) ; if ( versionMetadata == null ) { return update ( entities ) ; } else { return updateWithOptimisticLockInternal ( entities , versionMetadata ) ; } } | Updates the given list of entities using optimistic locking feature if the entities are set up to support optimistic locking . Otherwise a normal update is performed . |
14,354 | @ SuppressWarnings ( "unchecked" ) protected < E > E updateWithOptimisticLockingInternal ( E entity , PropertyMetadata versionMetadata ) { Transaction transaction = null ; try { entityManager . executeEntityListeners ( CallbackType . PRE_UPDATE , entity ) ; Entity nativeEntity = ( Entity ) Marshaller . marshal ( entityManager , entity , Intent . UPDATE ) ; transaction = datastore . newTransaction ( ) ; Entity storedNativeEntity = transaction . get ( nativeEntity . getKey ( ) ) ; if ( storedNativeEntity == null ) { throw new OptimisticLockException ( String . format ( "Entity does not exist: %s" , nativeEntity . getKey ( ) ) ) ; } String versionPropertyName = versionMetadata . getMappedName ( ) ; long version = nativeEntity . getLong ( versionPropertyName ) - 1 ; long storedVersion = storedNativeEntity . getLong ( versionPropertyName ) ; if ( version != storedVersion ) { throw new OptimisticLockException ( String . format ( "Expecting version %d, but found %d" , version , storedVersion ) ) ; } transaction . update ( nativeEntity ) ; transaction . commit ( ) ; E updatedEntity = ( E ) Unmarshaller . unmarshal ( nativeEntity , entity . getClass ( ) ) ; entityManager . executeEntityListeners ( CallbackType . POST_UPDATE , updatedEntity ) ; return updatedEntity ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } finally { rollbackIfActive ( transaction ) ; } } | Worker method for updating the given entity with optimistic locking . |
14,355 | @ SuppressWarnings ( "unchecked" ) protected < E > List < E > updateWithOptimisticLockInternal ( List < E > entities , PropertyMetadata versionMetadata ) { Transaction transaction = null ; try { entityManager . executeEntityListeners ( CallbackType . PRE_UPDATE , entities ) ; Entity [ ] nativeEntities = toNativeEntities ( entities , entityManager , Intent . UPDATE ) ; Key [ ] nativeKeys = new Key [ nativeEntities . length ] ; for ( int i = 0 ; i < nativeEntities . length ; i ++ ) { nativeKeys [ i ] = nativeEntities [ i ] . getKey ( ) ; } transaction = datastore . newTransaction ( ) ; List < Entity > storedNativeEntities = transaction . fetch ( nativeKeys ) ; String versionPropertyName = versionMetadata . getMappedName ( ) ; for ( int i = 0 ; i < nativeEntities . length ; i ++ ) { long version = nativeEntities [ i ] . getLong ( versionPropertyName ) - 1 ; Entity storedNativeEntity = storedNativeEntities . get ( i ) ; if ( storedNativeEntity == null ) { throw new OptimisticLockException ( String . format ( "Entity does not exist: %s" , nativeKeys [ i ] ) ) ; } long storedVersion = storedNativeEntities . get ( i ) . getLong ( versionPropertyName ) ; if ( version != storedVersion ) { throw new OptimisticLockException ( String . format ( "Expecting version %d, but found %d" , version , storedVersion ) ) ; } } transaction . update ( nativeEntities ) ; transaction . commit ( ) ; List < E > updatedEntities = ( List < E > ) toEntities ( entities . get ( 0 ) . getClass ( ) , nativeEntities ) ; entityManager . executeEntityListeners ( CallbackType . POST_UPDATE , updatedEntities ) ; return updatedEntities ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } finally { rollbackIfActive ( transaction ) ; } } | Internal worker method for updating the entities using optimistic locking . |
14,356 | public < E > E upsert ( E entity ) { try { entityManager . executeEntityListeners ( CallbackType . PRE_UPSERT , entity ) ; FullEntity < ? > nativeEntity = ( FullEntity < ? > ) Marshaller . marshal ( entityManager , entity , Intent . UPSERT ) ; Entity upsertedNativeEntity = nativeWriter . put ( nativeEntity ) ; @ SuppressWarnings ( "unchecked" ) E upsertedEntity = ( E ) Unmarshaller . unmarshal ( upsertedNativeEntity , entity . getClass ( ) ) ; entityManager . executeEntityListeners ( CallbackType . POST_UPSERT , upsertedEntity ) ; return upsertedEntity ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Updates or inserts the given entity in the Cloud Datastore . If the entity does not have an ID it may be generated . |
14,357 | @ SuppressWarnings ( "unchecked" ) public < E > List < E > upsert ( List < E > entities ) { if ( entities == null || entities . isEmpty ( ) ) { return new ArrayList < > ( ) ; } try { entityManager . executeEntityListeners ( CallbackType . PRE_UPSERT , entities ) ; FullEntity < ? > [ ] nativeEntities = toNativeFullEntities ( entities , entityManager , Intent . UPSERT ) ; Class < ? > entityClass = entities . get ( 0 ) . getClass ( ) ; List < Entity > upsertedNativeEntities = nativeWriter . put ( nativeEntities ) ; List < E > upsertedEntities = ( List < E > ) toEntities ( entityClass , upsertedNativeEntities ) ; entityManager . executeEntityListeners ( CallbackType . POST_UPSERT , upsertedEntities ) ; return upsertedEntities ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Updates or inserts the given list of entities in the Cloud Datastore . If the entities do not have a valid ID IDs may be generated . |
14,358 | public void delete ( Object entity ) { try { entityManager . executeEntityListeners ( CallbackType . PRE_DELETE , entity ) ; Key nativeKey = Marshaller . marshalKey ( entityManager , entity ) ; nativeWriter . delete ( nativeKey ) ; entityManager . executeEntityListeners ( CallbackType . POST_DELETE , entity ) ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Deletes the given entity from the Cloud Datastore . |
14,359 | public void delete ( List < ? > entities ) { try { entityManager . executeEntityListeners ( CallbackType . PRE_DELETE , entities ) ; Key [ ] nativeKeys = new Key [ entities . size ( ) ] ; for ( int i = 0 ; i < entities . size ( ) ; i ++ ) { nativeKeys [ i ] = Marshaller . marshalKey ( entityManager , entities . get ( i ) ) ; } nativeWriter . delete ( nativeKeys ) ; entityManager . executeEntityListeners ( CallbackType . POST_DELETE , entities ) ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Deletes the given entities from the Cloud Datastore . |
14,360 | public < E > void delete ( Class < E > entityClass , DatastoreKey parentKey , long id ) { try { EntityMetadata entityMetadata = EntityIntrospector . introspect ( entityClass ) ; Key nativeKey = Key . newBuilder ( parentKey . nativeKey ( ) , entityMetadata . getKind ( ) , id ) . build ( ) ; nativeWriter . delete ( nativeKey ) ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Deletes the entity with the given ID and parent key . |
14,361 | public void deleteByKey ( DatastoreKey key ) { try { nativeWriter . delete ( key . nativeKey ( ) ) ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Deletes an entity given its key . |
14,362 | public void deleteByKey ( List < DatastoreKey > keys ) { try { Key [ ] nativeKeys = new Key [ keys . size ( ) ] ; for ( int i = 0 ; i < keys . size ( ) ; i ++ ) { nativeKeys [ i ] = keys . get ( i ) . nativeKey ( ) ; } nativeWriter . delete ( nativeKeys ) ; } catch ( DatastoreException exp ) { throw DatastoreUtils . wrap ( exp ) ; } } | Deletes the entities having the given keys . |
14,363 | public void setNamedBindings ( Map < String , Object > namedBindings ) { if ( namedBindings == null ) { throw new IllegalArgumentException ( "namedBindings cannot be null" ) ; } this . namedBindings = namedBindings ; } | Sets the named bindings that are needed for any named parameters in the GQL query . |
14,364 | public void addPositionalBindings ( Object first , Object ... others ) { positionalBindings . add ( first ) ; positionalBindings . addAll ( Arrays . asList ( others ) ) ; } | Adds the positional bindings that are needed for any positional parameters in the GQL query . |
14,365 | private void computeQualifiedName ( ) { if ( parent != null ) { qualifiedName = parent . qualifiedName + "." + field . getName ( ) ; } else { qualifiedName = field . getName ( ) ; } } | Computes and sets the qualified name of this field . |
14,366 | public void put ( CallbackType callbackType , CallbackMetadata callbackMetadata ) { if ( callbacks == null ) { callbacks = new EnumMap < > ( CallbackType . class ) ; } List < CallbackMetadata > callbackMetadataList = callbacks . get ( callbackType ) ; if ( callbackMetadataList == null ) { callbackMetadataList = new ArrayList < > ( ) ; callbacks . put ( callbackType , callbackMetadataList ) ; } callbackMetadataList . add ( callbackMetadata ) ; } | Adds the given CallbackEventMetadata . |
14,367 | public List < CallbackMetadata > getCallbacks ( CallbackType callbackType ) { return callbacks == null ? null : callbacks . get ( callbackType ) ; } | Returns the callbacks for the given callback type . |
14,368 | private static ConstructorMetadata loadMetadata ( Class < ? > clazz ) { synchronized ( clazz ) { ConstructorMetadata metadata = cache . get ( clazz ) ; if ( metadata == null ) { ConstructorIntrospector introspector = new ConstructorIntrospector ( clazz ) ; introspector . process ( ) ; metadata = introspector . metadataBuilder . build ( ) ; cache . put ( clazz , metadata ) ; } return metadata ; } } | Loads the metadata if it does not already exist in the cache . |
14,369 | private void process ( ) { metadataBuilder = ConstructorMetadata . newBuilder ( ) . setClazz ( clazz ) ; MethodHandle mh = IntrospectionUtils . findDefaultConstructor ( clazz ) ; if ( mh != null ) { metadataBuilder . setConstructionStrategy ( ConstructorMetadata . ConstructionStrategy . CLASSIC ) . setConstructorMethodHandle ( mh ) ; return ; } mh = findNewBuilderMethod ( clazz ) ; if ( mh == null ) { final String pattern = "Class %s requires a public no-arg constructor or a public static " + "method that returns a corresponding Builder instance. The name of the static method " + "can either be %s or %s" ; String error = String . format ( pattern , clazz . getName ( ) , METHOD_NAME_NEW_BUILDER , METHOD_NAME_BUILDER ) ; throw new UnsupportedConstructionStrategyException ( error ) ; } Class < ? > builderClass = mh . type ( ) . returnType ( ) ; metadataBuilder . setConstructionStrategy ( ConstructorMetadata . ConstructionStrategy . BUILDER ) . setConstructorMethodHandle ( mh ) . setBuilderClass ( builderClass ) ; MethodHandle buildMethodHandle = IntrospectionUtils . findInstanceMethod ( builderClass , METHOD_NAME_BUILD , clazz ) ; if ( buildMethodHandle == null ) { String pattern = "Class %s requires a public instance method, %s, with a return type of %s" ; throw new EntityManagerException ( String . format ( pattern , builderClass . getName ( ) , METHOD_NAME_BUILD , clazz ) ) ; } metadataBuilder . setBuildMethodHandle ( buildMethodHandle ) ; } | Introspects the class and builds the metadata . |
14,370 | public static MethodHandle findNewBuilderMethod ( Class < ? > clazz ) { MethodHandle mh = null ; for ( String methodName : NEW_BUILDER_METHOD_NAMES ) { mh = IntrospectionUtils . findStaticMethod ( clazz , methodName , Object . class ) ; if ( mh != null ) { break ; } } return mh ; } | Finds and returns a method handle for new instance of a Builder class . |
14,371 | public static < T > T unmarshal ( Entity nativeEntity , Class < T > entityClass ) { return unmarshalBaseEntity ( nativeEntity , entityClass ) ; } | Unmarshals the given native Entity into an object of given type entityClass . |
14,372 | public static < T > T unmarshal ( ProjectionEntity nativeEntity , Class < T > entityClass ) { return unmarshalBaseEntity ( nativeEntity , entityClass ) ; } | Unmarshals the given native ProjectionEntity into an object of given type entityClass . |
14,373 | @ SuppressWarnings ( "unchecked" ) private < T > T unmarshal ( ) { try { instantiateEntity ( ) ; unmarshalIdentifier ( ) ; unmarshalKeyAndParentKey ( ) ; unmarshalProperties ( ) ; unmarshalEmbeddedFields ( ) ; ConstructorMetadata constructorMetadata = entityMetadata . getConstructorMetadata ( ) ; if ( constructorMetadata . isBuilderConstructionStrategy ( ) ) { entity = constructorMetadata . getBuildMethodHandle ( ) . invoke ( entity ) ; } return ( T ) entity ; } catch ( EntityManagerException exp ) { throw exp ; } catch ( Throwable t ) { throw new EntityManagerException ( t . getMessage ( ) , t ) ; } } | Unmarshals the given Datastore Entity and returns the equivalent Entity POJO . |
14,374 | private static < T > T unmarshalBaseEntity ( BaseEntity < ? > nativeEntity , Class < T > entityClass ) { if ( nativeEntity == null ) { return null ; } Unmarshaller unmarshaller = new Unmarshaller ( nativeEntity , entityClass ) ; return unmarshaller . unmarshal ( ) ; } | Unmarshals the given BaseEntity and returns the equivalent model object . |
14,375 | private void unmarshalIdentifier ( ) throws Throwable { IdentifierMetadata identifierMetadata = entityMetadata . getIdentifierMetadata ( ) ; Object id = ( ( Key ) nativeEntity . getKey ( ) ) . getNameOrId ( ) ; IdClassMetadata idClassMetadata = identifierMetadata . getIdClassMetadata ( ) ; if ( idClassMetadata != null ) { Object wrappedId = idClassMetadata . getConstructor ( ) . invoke ( id ) ; id = wrappedId ; } MethodHandle writeMethod = identifierMetadata . getWriteMethod ( ) ; writeMethod . invoke ( entity , id ) ; } | Unamrshals the identifier . |
14,376 | private void unmarshalKeyAndParentKey ( ) throws Throwable { KeyMetadata keyMetadata = entityMetadata . getKeyMetadata ( ) ; if ( keyMetadata != null ) { MethodHandle writeMethod = keyMetadata . getWriteMethod ( ) ; Key entityKey = ( Key ) nativeEntity . getKey ( ) ; writeMethod . invoke ( entity , new DefaultDatastoreKey ( entityKey ) ) ; } ParentKeyMetadata parentKeyMetadata = entityMetadata . getParentKeyMetadata ( ) ; if ( parentKeyMetadata != null ) { MethodHandle writeMethod = parentKeyMetadata . getWriteMethod ( ) ; Key parentKey = nativeEntity . getKey ( ) . getParent ( ) ; if ( parentKey != null ) { writeMethod . invoke ( entity , new DefaultDatastoreKey ( parentKey ) ) ; } } } | Unamrshals the entity s key and parent key . |
14,377 | private void unmarshalProperties ( ) throws Throwable { Collection < PropertyMetadata > propertyMetadataCollection = entityMetadata . getPropertyMetadataCollection ( ) ; for ( PropertyMetadata propertyMetadata : propertyMetadataCollection ) { unmarshalProperty ( propertyMetadata , entity ) ; } } | Unmarshal all the properties . |
14,378 | private void unmarshalEmbeddedFields ( ) throws Throwable { for ( EmbeddedMetadata embeddedMetadata : entityMetadata . getEmbeddedMetadataCollection ( ) ) { if ( embeddedMetadata . getStorageStrategy ( ) == StorageStrategy . EXPLODED ) { unmarshalWithExplodedStrategy ( embeddedMetadata , entity ) ; } else { unmarshalWithImplodedStrategy ( embeddedMetadata , entity , nativeEntity ) ; } } } | Unmarshals the embedded fields of this entity . |
14,379 | private void unmarshalWithExplodedStrategy ( EmbeddedMetadata embeddedMetadata , Object target ) throws Throwable { Object embeddedObject = initializeEmbedded ( embeddedMetadata , target ) ; for ( PropertyMetadata propertyMetadata : embeddedMetadata . getPropertyMetadataCollection ( ) ) { unmarshalProperty ( propertyMetadata , embeddedObject ) ; } for ( EmbeddedMetadata embeddedMetadata2 : embeddedMetadata . getEmbeddedMetadataCollection ( ) ) { unmarshalWithExplodedStrategy ( embeddedMetadata2 , embeddedObject ) ; } ConstructorMetadata constructorMetadata = embeddedMetadata . getConstructorMetadata ( ) ; if ( constructorMetadata . isBuilderConstructionStrategy ( ) ) { embeddedObject = constructorMetadata . getBuildMethodHandle ( ) . invoke ( embeddedObject ) ; } embeddedMetadata . getWriteMethod ( ) . invoke ( target , embeddedObject ) ; } | Unmarshals the embedded field represented by the given embedded metadata . |
14,380 | private static void unmarshalWithImplodedStrategy ( EmbeddedMetadata embeddedMetadata , Object target , BaseEntity < ? > nativeEntity ) throws Throwable { Object embeddedObject = null ; ConstructorMetadata constructorMetadata = embeddedMetadata . getConstructorMetadata ( ) ; FullEntity < ? > nativeEmbeddedEntity = null ; String propertyName = embeddedMetadata . getMappedName ( ) ; if ( nativeEntity . contains ( propertyName ) ) { Value < ? > nativeValue = nativeEntity . getValue ( propertyName ) ; if ( nativeValue instanceof NullValue ) { embeddedMetadata . getWriteMethod ( ) . invoke ( target , embeddedObject ) ; } else { nativeEmbeddedEntity = ( ( EntityValue ) nativeValue ) . get ( ) ; embeddedObject = constructorMetadata . getConstructorMethodHandle ( ) . invoke ( ) ; } } if ( embeddedObject == null ) { return ; } for ( PropertyMetadata propertyMetadata : embeddedMetadata . getPropertyMetadataCollection ( ) ) { unmarshalProperty ( propertyMetadata , embeddedObject , nativeEmbeddedEntity ) ; } for ( EmbeddedMetadata embeddedMetadata2 : embeddedMetadata . getEmbeddedMetadataCollection ( ) ) { unmarshalWithImplodedStrategy ( embeddedMetadata2 , embeddedObject , nativeEmbeddedEntity ) ; } if ( constructorMetadata . isBuilderConstructionStrategy ( ) ) { embeddedObject = constructorMetadata . getBuildMethodHandle ( ) . invoke ( embeddedObject ) ; } embeddedMetadata . getWriteMethod ( ) . invoke ( target , embeddedObject ) ; } | Unmarshals the embedded field represented by the given metadata . |
14,381 | private void unmarshalProperty ( PropertyMetadata propertyMetadata , Object target ) throws Throwable { unmarshalProperty ( propertyMetadata , target , nativeEntity ) ; } | Unmarshals the property represented by the given property metadata and updates the target object with the property value . |
14,382 | public void setIdentifierMetadata ( IdentifierMetadata identifierMetadata ) { if ( this . identifierMetadata != null ) { String format = "Class %s has at least two fields, %s and %s, marked with %s annotation. " + "Only one field can be marked as an identifier. " ; String message = String . format ( format , entityClass . getName ( ) , this . identifierMetadata . getName ( ) , identifierMetadata . getName ( ) , Identifier . class . getName ( ) ) ; throw new EntityManagerException ( message ) ; } this . identifierMetadata = identifierMetadata ; } | Sets the metadata of the identifier . An exception will be thrown if there is an IdentifierMetadata already set . |
14,383 | public void setKeyMetadata ( KeyMetadata keyMetadata ) { if ( this . keyMetadata != null ) { String format = "Class %s has two fields, %s and %s marked with %s annotation. Only one " + "field can be marked as Key. " ; String message = String . format ( format , entityClass . getName ( ) , this . keyMetadata . getName ( ) , keyMetadata . getName ( ) , Key . class . getName ( ) ) ; throw new EntityManagerException ( message ) ; } this . keyMetadata = keyMetadata ; } | Sets the metadata of the Key field . |
14,384 | public void setParentKetMetadata ( ParentKeyMetadata parentKeyMetadata ) { if ( this . parentKeyMetadata != null ) { String format = "Class %s has two fields, %s and %s marked with %s annotation. Only one " + "field can be marked as ParentKey. " ; String message = String . format ( format , entityClass . getName ( ) , this . parentKeyMetadata . getName ( ) , parentKeyMetadata . getName ( ) , ParentKey . class . getName ( ) ) ; throw new EntityManagerException ( message ) ; } this . parentKeyMetadata = parentKeyMetadata ; } | Sets the metadata about the parent key . |
14,385 | public void setVersionMetadata ( PropertyMetadata versionMetadata ) { if ( this . versionMetadata != null ) { throwDuplicateAnnotationException ( entityClass , Version . class , this . versionMetadata , versionMetadata ) ; } this . versionMetadata = versionMetadata ; } | Sets the metadata of the field that is used for optimistic locking . |
14,386 | public void setCreatedTimestampMetadata ( PropertyMetadata createdTimestampMetadata ) { if ( this . createdTimestampMetadata != null ) { throwDuplicateAnnotationException ( entityClass , CreatedTimestamp . class , this . createdTimestampMetadata , createdTimestampMetadata ) ; } this . createdTimestampMetadata = createdTimestampMetadata ; } | Sets the created timestamp metadata to the given value . |
14,387 | public void updateMasterPropertyMetadataMap ( String mappedName , String qualifiedName ) { String old = masterPropertyMetadataMap . put ( mappedName , qualifiedName ) ; if ( old != null ) { String message = "Duplicate property %s in entity %s. Check fields %s and %s" ; throw new EntityManagerException ( String . format ( message , mappedName , entityClass . getName ( ) , old , qualifiedName ) ) ; } } | Updates the master property metadata map with the given property metadata . |
14,388 | private void ensureUniqueProperties ( EmbeddedMetadata embeddedMetadata , StorageStrategy storageStrategy ) { if ( embeddedMetadata . getStorageStrategy ( ) == StorageStrategy . EXPLODED ) { for ( PropertyMetadata propertyMetadata : embeddedMetadata . getPropertyMetadataCollection ( ) ) { updateMasterPropertyMetadataMap ( propertyMetadata . getMappedName ( ) , embeddedMetadata . getField ( ) . getQualifiedName ( ) + "." + propertyMetadata . getName ( ) ) ; } for ( EmbeddedMetadata embeddedMetadata2 : embeddedMetadata . getEmbeddedMetadataCollection ( ) ) { ensureUniqueProperties ( embeddedMetadata2 , storageStrategy ) ; } } else { updateMasterPropertyMetadataMap ( embeddedMetadata . getMappedName ( ) , embeddedMetadata . getField ( ) . getQualifiedName ( ) ) ; } } | Validates the embedded field represented by the given metadata to ensure there are no duplicate property names defined across the entity . |
14,389 | private static void throwDuplicateAnnotationException ( Class < ? > entityClass , Class < ? extends Annotation > annotationClass , PropertyMetadata metadata1 , PropertyMetadata metadata2 ) { String format = "Class %s has at least two fields, %s and %s, with an annotation of %s. " + "A given entity can have at most one field with this annotation. " ; String message = String . format ( format , entityClass . getName ( ) , metadata1 . getName ( ) , metadata2 . getName ( ) , annotationClass . getName ( ) ) ; throw new EntityManagerException ( message ) ; } | Raises an exception with a detailed message reporting that the entity has more than one field that has a specific annotation . |
14,390 | public static ExternalListenerMetadata introspect ( Class < ? > listenerClass ) { ExternalListenerMetadata cachedMetadata = cache . get ( listenerClass ) ; if ( cachedMetadata != null ) { return cachedMetadata ; } synchronized ( listenerClass ) { cachedMetadata = cache . get ( listenerClass ) ; if ( cachedMetadata != null ) { return cachedMetadata ; } ExternalListenerIntrospector introspector = new ExternalListenerIntrospector ( listenerClass ) ; introspector . introspect ( ) ; cache . put ( listenerClass , introspector . metadata ) ; return introspector . metadata ; } } | Introspects the given entity listener class and returns its metadata . |
14,391 | private void introspect ( ) { if ( ! listenerClass . isAnnotationPresent ( EntityListener . class ) ) { String message = String . format ( "Class %s must have %s annotation to be used as an EntityListener" , listenerClass . getName ( ) , EntityListener . class . getName ( ) ) ; throw new EntityManagerException ( message ) ; } metadata = new ExternalListenerMetadata ( listenerClass ) ; processMethods ( ) ; } | Introspects the listener class and creates the metadata . |
14,392 | public Mapper getMapper ( Field field ) { PropertyMapper propertyMapperAnnotation = field . getAnnotation ( PropertyMapper . class ) ; if ( propertyMapperAnnotation != null ) { return createCustomMapper ( field , propertyMapperAnnotation ) ; } Class < ? > fieldType = field . getType ( ) ; if ( fieldType . equals ( BigDecimal . class ) ) { Decimal decimalAnnotation = field . getAnnotation ( Decimal . class ) ; if ( decimalAnnotation != null ) { return new DecimalMapper ( decimalAnnotation . precision ( ) , decimalAnnotation . scale ( ) ) ; } } if ( List . class . isAssignableFrom ( fieldType ) || Set . class . isAssignableFrom ( fieldType ) ) { return CollectionMapperFactory . getInstance ( ) . getMapper ( field ) ; } return getMapper ( field . getGenericType ( ) ) ; } | Returns the mapper for the given field . If the field has a custom mapper a new instance of the specified mapper will be created and returned . Otherwise one of the built - in mappers will be returned based on the field type . |
14,393 | public Mapper getMapper ( Type type ) { Mapper mapper = cache . get ( type ) ; if ( mapper == null ) { mapper = createMapper ( type ) ; } return mapper ; } | Returns a mapper for the given type . If a mapper that can handle given type exists in the cache it will be returned . Otherwise a new mapper will be created . |
14,394 | public void setDefaultMapper ( Type type , Mapper mapper ) { if ( mapper == null ) { throw new IllegalArgumentException ( "mapper cannot be null" ) ; } lock . lock ( ) ; try { cache . put ( type , mapper ) ; } finally { lock . unlock ( ) ; } } | Sets or registers the given mapper for the given type . This method must be called before performing any persistence operations preferably during application startup . Entities that were introspected before calling this method will NOT use the new mapper . |
14,395 | private Mapper createMapper ( Type type ) { lock . lock ( ) ; try { Mapper mapper = cache . get ( type ) ; if ( mapper != null ) { return mapper ; } if ( type instanceof Class ) { mapper = createMapper ( ( Class < ? > ) type ) ; } else if ( type instanceof ParameterizedType ) { mapper = createMapper ( ( ParameterizedType ) type ) ; } else { throw new IllegalArgumentException ( String . format ( "Type %s is neither a Class nor ParameterizedType" , type ) ) ; } cache . put ( type , mapper ) ; return mapper ; } finally { lock . unlock ( ) ; } } | Creates a new mapper for the given type . |
14,396 | private Mapper createMapper ( Class < ? > clazz ) { Mapper mapper ; if ( Enum . class . isAssignableFrom ( clazz ) ) { mapper = new EnumMapper ( clazz ) ; } else if ( Map . class . isAssignableFrom ( clazz ) ) { mapper = new MapMapper ( clazz ) ; } else if ( clazz . isAnnotationPresent ( Embeddable . class ) ) { mapper = new EmbeddedObjectMapper ( clazz ) ; } else { throw new NoSuitableMapperException ( String . format ( "No mapper found for class %s" , clazz . getName ( ) ) ) ; } return mapper ; } | Creates a mapper for the given class . |
14,397 | private void createDefaultMappers ( ) { BooleanMapper booleanMapper = new BooleanMapper ( ) ; CharMapper charMapper = new CharMapper ( ) ; ShortMapper shortMapper = new ShortMapper ( ) ; IntegerMapper integerMapper = new IntegerMapper ( ) ; LongMapper longMapper = new LongMapper ( ) ; FloatMapper floatMapper = new FloatMapper ( ) ; DoubleMapper doubleMapper = new DoubleMapper ( ) ; cache . put ( boolean . class , booleanMapper ) ; cache . put ( Boolean . class , booleanMapper ) ; cache . put ( char . class , charMapper ) ; cache . put ( Character . class , charMapper ) ; cache . put ( short . class , shortMapper ) ; cache . put ( Short . class , shortMapper ) ; cache . put ( int . class , integerMapper ) ; cache . put ( Integer . class , integerMapper ) ; cache . put ( long . class , longMapper ) ; cache . put ( Long . class , longMapper ) ; cache . put ( float . class , floatMapper ) ; cache . put ( Float . class , floatMapper ) ; cache . put ( double . class , doubleMapper ) ; cache . put ( Double . class , doubleMapper ) ; cache . put ( String . class , new StringMapper ( ) ) ; cache . put ( BigDecimal . class , new BigDecimalMapper ( ) ) ; cache . put ( byte [ ] . class , new ByteArrayMapper ( ) ) ; cache . put ( char [ ] . class , new CharArrayMapper ( ) ) ; cache . put ( Date . class , new DateMapper ( ) ) ; cache . put ( Calendar . class , new CalendarMapper ( ) ) ; cache . put ( GeoLocation . class , new GeoLocationMapper ( ) ) ; cache . put ( DatastoreKey . class , new KeyMapper ( ) ) ; cache . put ( LocalDate . class , new LocalDateMapper ( ) ) ; cache . put ( LocalTime . class , new LocalTimeMapper ( ) ) ; cache . put ( LocalDateTime . class , new LocalDateTimeMapper ( ) ) ; cache . put ( OffsetDateTime . class , new OffsetDateTimeMapper ( ) ) ; cache . put ( ZonedDateTime . class , new ZonedDateTimeMapper ( ) ) ; } | Creates and assigns default Mappers various common types . |
14,398 | private Mapper createCustomMapper ( Field field , PropertyMapper propertyMapperAnnotation ) { Class < ? extends Mapper > mapperClass = propertyMapperAnnotation . value ( ) ; Constructor < ? extends Mapper > constructor = IntrospectionUtils . getConstructor ( mapperClass , Field . class ) ; if ( constructor != null ) { try { return constructor . newInstance ( field ) ; } catch ( Exception exp ) { throw new EntityManagerException ( exp ) ; } } throw new EntityManagerException ( String . format ( "Mapper class %s must have a public constructor with a parameter type of %s" , mapperClass . getName ( ) , Field . class . getName ( ) ) ) ; } | Creates and returns a custom mapper for the given field . |
14,399 | public void putListener ( CallbackType callbackType , Method method ) { if ( callbacks == null ) { callbacks = new EnumMap < > ( CallbackType . class ) ; } Method oldMethod = callbacks . put ( callbackType , method ) ; if ( oldMethod != null ) { String format = "Class %s has at least two methods, %s and %s, with annotation of %s. " + "At most one method is allowed for a given callback type. " ; String message = String . format ( format , listenerClass . getName ( ) , oldMethod . getName ( ) , method . getName ( ) , callbackType . getAnnotationClass ( ) . getName ( ) ) ; throw new EntityManagerException ( message ) ; } } | Registers the given method as the callback method for the given event type . |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.