diff --git a/extensions/panache/mongodb-panache/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/PanacheMongoEntityEnhancer.java b/extensions/panache/mongodb-panache/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/PanacheMongoEntityEnhancer.java index 4b31bb09b52a8..27d5b0c216769 100644 --- a/extensions/panache/mongodb-panache/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/PanacheMongoEntityEnhancer.java +++ b/extensions/panache/mongodb-panache/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/PanacheMongoEntityEnhancer.java @@ -72,39 +72,6 @@ protected void generateAccessorGetField(MethodVisitor mv, EntityField field) { @Override public void visitEnd() { - // Bridge for mongoCollection - MethodVisitor mv = super.visitMethod( - Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE | Opcodes.ACC_STATIC, - "mongoCollection", - "()Lcom/mongodb/client/MongoCollection;", - null, - null); - mv.visitCode(); - mv.visitLdcInsn(thisClass); - mv.visitMethodInsn(Opcodes.INVOKESTATIC, - getPanacheOperationsBinaryName(), - "getMongoCollection", - "(Ljava/lang/Class;)Lcom/mongodb/client/MongoCollection;", false); - mv.visitInsn(Opcodes.ARETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - - // Bridge for mongoDatabase - mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE | Opcodes.ACC_STATIC, - "mongoDatabase", - "()Lcom/mongodb/client/MongoDatabase;", - null, - null); - mv.visitCode(); - mv.visitLdcInsn(thisClass); - mv.visitMethodInsn(Opcodes.INVOKESTATIC, - getPanacheOperationsBinaryName(), - "getMongoDatabase", - "(Ljava/lang/Class;)Lcom/mongodb/client/MongoDatabase;", false); - mv.visitInsn(Opcodes.ARETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - super.visitEnd(); } } diff --git a/extensions/panache/mongodb-panache/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/PanacheMongoRepositoryEnhancer.java b/extensions/panache/mongodb-panache/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/PanacheMongoRepositoryEnhancer.java index f1a07dbde2283..988a942d7420b 100644 --- a/extensions/panache/mongodb-panache/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/PanacheMongoRepositoryEnhancer.java +++ b/extensions/panache/mongodb-panache/deployment/src/main/java/io/quarkus/mongodb/panache/deployment/PanacheMongoRepositoryEnhancer.java @@ -68,38 +68,6 @@ public void visitEnd() { mv.visitMaxs(0, 0); mv.visitEnd(); - // Bridge for mongoCollection - mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE, - "mongoCollection", - "()Lcom/mongodb/client/MongoCollection;", - null, - null); - mv.visitCode(); - mv.visitLdcInsn(entityType); - mv.visitMethodInsn(Opcodes.INVOKESTATIC, - getPanacheOperationsBinaryName(), - "getMongoCollection", - "(Ljava/lang/Class;)Lcom/mongodb/client/MongoCollection;", false); - mv.visitInsn(Opcodes.ARETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - - // Bridge for mongoDatabase - mv = super.visitMethod(Opcodes.ACC_PUBLIC | Opcodes.ACC_SYNTHETIC | Opcodes.ACC_BRIDGE, - "mongoDatabase", - "()Lcom/mongodb/client/MongoDatabase;", - null, - null); - mv.visitCode(); - mv.visitLdcInsn(entityType); - mv.visitMethodInsn(Opcodes.INVOKESTATIC, - getPanacheOperationsBinaryName(), - "getMongoDatabase", - "(Ljava/lang/Class;)Lcom/mongodb/client/MongoDatabase;", false); - mv.visitInsn(Opcodes.ARETURN); - mv.visitMaxs(0, 0); - mv.visitEnd(); - super.visitEnd(); } diff --git a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoEntityBase.java b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoEntityBase.java index ff190d2bd4066..bf0c17cd3d23e 100644 --- a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoEntityBase.java +++ b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoEntityBase.java @@ -869,6 +869,7 @@ public static void persist(Object firstEntity, Object... entities) { /** * Allow to access the underlying Mongo Collection. */ + @GenerateBridge public static MongoCollection mongoCollection() { throw MongoOperations.implementationInjectionMissing(); } @@ -876,6 +877,7 @@ public static MongoCollection mongoCollection() { /** * Allow to access the underlying Mongo Database. */ + @GenerateBridge public static MongoDatabase mongoDatabase() { throw MongoOperations.implementationInjectionMissing(); } diff --git a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoRepositoryBase.java b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoRepositoryBase.java index a20c0ad427574..50693a79da525 100644 --- a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoRepositoryBase.java +++ b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/PanacheMongoRepositoryBase.java @@ -871,6 +871,7 @@ public default void persist(Entity firstEntity, @SuppressWarnings("unchecked") E /** * Allow to access the underlying Mongo Collection */ + @GenerateBridge public default MongoCollection mongoCollection() { throw MongoOperations.implementationInjectionMissing(); } @@ -878,6 +879,7 @@ public default MongoCollection mongoCollection() { /** * Allow to access the underlying Mongo Database. */ + @GenerateBridge public default MongoDatabase mongoDatabase() { throw MongoOperations.implementationInjectionMissing(); } diff --git a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java index 2617f95d02094..bd12253154444 100644 --- a/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java +++ b/extensions/panache/mongodb-panache/runtime/src/main/java/io/quarkus/mongodb/panache/runtime/MongoOperations.java @@ -39,7 +39,7 @@ public class MongoOperations { // Instance methods public static void insert(Object entity) { - MongoCollection collection = getMongoCollection(entity); + MongoCollection collection = mongoCollection(entity); insert(collection, entity); } @@ -53,13 +53,13 @@ public static void insert(Iterable entities) { if (objects.size() > 0) { // get the first entity to be able to retrieve the collection with it Object firstEntity = objects.get(0); - MongoCollection collection = getMongoCollection(firstEntity); + MongoCollection collection = mongoCollection(firstEntity); insert(collection, objects); } } public static void insert(Object firstEntity, Object... entities) { - MongoCollection collection = getMongoCollection(firstEntity); + MongoCollection collection = mongoCollection(firstEntity); if (entities == null || entities.length == 0) { insert(collection, firstEntity); } else { @@ -75,13 +75,13 @@ public static void insert(Stream entities) { if (objects.size() > 0) { // get the first entity to be able to retrieve the collection with it Object firstEntity = objects.get(0); - MongoCollection collection = getMongoCollection(firstEntity); + MongoCollection collection = mongoCollection(firstEntity); insert(collection, objects); } } public static void update(Object entity) { - MongoCollection collection = getMongoCollection(entity); + MongoCollection collection = mongoCollection(entity); update(collection, entity); } @@ -95,13 +95,13 @@ public static void update(Iterable entities) { if (objects.size() > 0) { // get the first entity to be able to retrieve the collection with it Object firstEntity = objects.get(0); - MongoCollection collection = getMongoCollection(firstEntity); + MongoCollection collection = mongoCollection(firstEntity); update(collection, objects); } } public static void update(Object firstEntity, Object... entities) { - MongoCollection collection = getMongoCollection(firstEntity); + MongoCollection collection = mongoCollection(firstEntity); if (entities == null || entities.length == 0) { insert(collection, firstEntity); } else { @@ -117,13 +117,13 @@ public static void update(Stream entities) { if (objects.size() > 0) { // get the first entity to be able to retrieve the collection with it Object firstEntity = objects.get(0); - MongoCollection collection = getMongoCollection(firstEntity); + MongoCollection collection = mongoCollection(firstEntity); update(collection, objects); } } public static void persist(Object entity) { - MongoCollection collection = getMongoCollection(entity); + MongoCollection collection = mongoCollection(entity); persist(collection, entity); } @@ -137,13 +137,13 @@ public static void persist(Iterable entities) { if (objects.size() > 0) { // get the first entity to be able to retrieve the collection with it Object firstEntity = objects.get(0); - MongoCollection collection = getMongoCollection(firstEntity); + MongoCollection collection = mongoCollection(firstEntity); persist(collection, objects); } } public static void persist(Object firstEntity, Object... entities) { - MongoCollection collection = getMongoCollection(firstEntity); + MongoCollection collection = mongoCollection(firstEntity); if (entities == null || entities.length == 0) { persist(collection, firstEntity); } else { @@ -159,31 +159,31 @@ public static void persist(Stream entities) { if (objects.size() > 0) { // get the first entity to be able to retrieve the collection with it Object firstEntity = objects.get(0); - MongoCollection collection = getMongoCollection(firstEntity); + MongoCollection collection = mongoCollection(firstEntity); persist(collection, objects); } } public static void delete(Object entity) { - MongoCollection collection = getMongoCollection(entity); + MongoCollection collection = mongoCollection(entity); BsonDocument document = getBsonDocument(collection, entity); BsonValue id = document.get(ID); BsonDocument query = new BsonDocument().append(ID, id); collection.deleteOne(query); } - public static MongoCollection getMongoCollection(Class entityClass) { + public static MongoCollection mongoCollection(Class entityClass) { MongoEntity mongoEntity = entityClass.getAnnotation(MongoEntity.class); - MongoDatabase database = getMongoDatabase(mongoEntity); + MongoDatabase database = mongoDatabase(mongoEntity); if (mongoEntity != null && !mongoEntity.collection().isEmpty()) { return database.getCollection(mongoEntity.collection(), entityClass); } return database.getCollection(entityClass.getSimpleName(), entityClass); } - public static MongoDatabase getMongoDatabase(Class entityClass) { + public static MongoDatabase mongoDatabase(Class entityClass) { MongoEntity mongoEntity = entityClass.getAnnotation(MongoEntity.class); - return getMongoDatabase(mongoEntity); + return mongoDatabase(mongoEntity); } // @@ -259,12 +259,12 @@ private static BsonDocument getBsonDocument(MongoCollection collection, Object e return document; } - private static MongoCollection getMongoCollection(Object entity) { + private static MongoCollection mongoCollection(Object entity) { Class entityClass = entity.getClass(); - return getMongoCollection(entityClass); + return mongoCollection(entityClass); } - private static MongoDatabase getMongoDatabase(MongoEntity entity) { + private static MongoDatabase mongoDatabase(MongoEntity entity) { MongoClient mongoClient = Arc.container().instance(MongoClient.class).get(); if (entity != null && !entity.database().isEmpty()) { return mongoClient.getDatabase(entity.database()); @@ -278,7 +278,7 @@ private static MongoDatabase getMongoDatabase(MongoEntity entity) { // Queries public static Object findById(Class entityClass, Object id) { - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return collection.find(new Document(ID, id)).first(); } @@ -291,7 +291,7 @@ public static PanacheQuery find(Class entityClass, String query, Sort sort String bindQuery = bindQuery(entityClass, query, params); Document docQuery = Document.parse(bindQuery); Document docSort = sortToDocument(sort); - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return new PanacheQueryImpl(collection, entityClass, docQuery, docSort); } @@ -344,7 +344,7 @@ public static PanacheQuery find(Class entityClass, String query, Sort sort String bindQuery = bindQuery(entityClass, query, params); Document docQuery = Document.parse(bindQuery); Document docSort = sortToDocument(sort); - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return new PanacheQueryImpl(collection, entityClass, docQuery, docSort); } @@ -358,13 +358,13 @@ public static PanacheQuery find(Class entityClass, String query, Sort sort @SuppressWarnings("rawtypes") public static PanacheQuery find(Class entityClass, Document query, Sort sort) { - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); Document sortDoc = sortToDocument(sort); return new PanacheQueryImpl(collection, entityClass, query, sortDoc); } public static PanacheQuery find(Class entityClass, Document query, Document sort) { - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return new PanacheQueryImpl(collection, entityClass, query, sort); } @@ -442,13 +442,13 @@ public static Stream stream(Class entityClass, Document query, Document so @SuppressWarnings("rawtypes") public static PanacheQuery findAll(Class entityClass) { - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return new PanacheQueryImpl(collection, entityClass, null, null); } @SuppressWarnings("rawtypes") public static PanacheQuery findAll(Class entityClass, Sort sort) { - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); Document sortDoc = sortToDocument(sort); return new PanacheQueryImpl(collection, entityClass, null, sortDoc); } @@ -482,21 +482,21 @@ public static Stream streamAll(Class entityClass, Sort sort) { } public static long count(Class entityClass) { - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return collection.countDocuments(); } public static long count(Class entityClass, String query, Object... params) { String bindQuery = bindQuery(entityClass, query, params); Document docQuery = Document.parse(bindQuery); - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return collection.countDocuments(docQuery); } public static long count(Class entityClass, String query, Map params) { String bindQuery = bindQuery(entityClass, query, params); Document docQuery = Document.parse(bindQuery); - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return collection.countDocuments(docQuery); } @@ -506,26 +506,26 @@ public static long count(Class entityClass, String query, Parameters params) //specific Mongo query public static long count(Class entityClass, Document query) { - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return collection.countDocuments(query); } public static long deleteAll(Class entityClass) { - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return collection.deleteMany(new Document()).getDeletedCount(); } public static long delete(Class entityClass, String query, Object... params) { String bindQuery = bindQuery(entityClass, query, params); Document docQuery = Document.parse(bindQuery); - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return collection.deleteMany(docQuery).getDeletedCount(); } public static long delete(Class entityClass, String query, Map params) { String bindQuery = bindQuery(entityClass, query, params); Document docQuery = Document.parse(bindQuery); - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return collection.deleteMany(docQuery).getDeletedCount(); } @@ -535,7 +535,7 @@ public static long delete(Class entityClass, String query, Parameters params) //specific Mongo query public static long delete(Class entityClass, Document query) { - MongoCollection collection = getMongoCollection(entityClass); + MongoCollection collection = mongoCollection(entityClass); return collection.deleteMany(query).getDeletedCount(); }