Skip to content

Commit

Permalink
fix: use @GenerateBridge and avoid manual ASM implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
loicmathieu committed Sep 6, 2019
1 parent 5c4ff38 commit 0faa74e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 100 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -869,13 +869,15 @@ public static void persist(Object firstEntity, Object... entities) {
/**
* Allow to access the underlying Mongo Collection.
*/
@GenerateBridge
public static <T> MongoCollection<T> mongoCollection() {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Allow to access the underlying Mongo Database.
*/
@GenerateBridge
public static MongoDatabase mongoDatabase() {
throw MongoOperations.implementationInjectionMissing();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -871,13 +871,15 @@ public default void persist(Entity firstEntity, @SuppressWarnings("unchecked") E
/**
* Allow to access the underlying Mongo Collection
*/
@GenerateBridge
public default MongoCollection<Entity> mongoCollection() {
throw MongoOperations.implementationInjectionMissing();
}

/**
* Allow to access the underlying Mongo Database.
*/
@GenerateBridge
public default MongoDatabase mongoDatabase() {
throw MongoOperations.implementationInjectionMissing();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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 {
Expand All @@ -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);
}

Expand All @@ -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 {
Expand All @@ -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);
}

Expand All @@ -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 {
Expand All @@ -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);
}

//
Expand Down Expand Up @@ -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());
Expand All @@ -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();
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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<String, 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);
}

Expand All @@ -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<String, 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();
}

Expand All @@ -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();
}

Expand Down

0 comments on commit 0faa74e

Please sign in to comment.