Skip to content

Commit

Permalink
Change method signature and refactor function call
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiang Zhong committed Jun 7, 2022
1 parent 1c7bf6c commit 2274df6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public final class GenerateQuery {
*/
private static QueryMessage message = new QueryMessage();

/**
* variable CLASSNAME_SPLIT_PATTERN
*/
private static final String CLASSNAME_SPLIT_PATTERN = "\\$";

/**
* Constructor to have private modifier as it has only static methods
*/
Expand All @@ -69,7 +74,7 @@ private GenerateQuery() {
* @return the proxified object
*/
@SuppressWarnings("unchecked")
public static <T> T createQueryEntity(Class<T> cl) {
public static <T> T createQueryEntity(Class<T> cl) throws InstantiationException, IllegalAccessException {
Class<?> proxied = null;
if (cl.isInterface()) {
LOG.debug("The given class is interface");
Expand All @@ -82,11 +87,7 @@ public static <T> T createQueryEntity(Class<T> cl) {
.load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
}
try {
return (T) proxied.newInstance();
} catch(Exception exception) {
throw new RuntimeException(exception);
}
return (T) proxied.newInstance();
}

/**
Expand All @@ -95,25 +96,9 @@ public static <T> T createQueryEntity(Class<T> cl) {
* @return the proxified object
*/
@SuppressWarnings("unchecked")
public static <T> T createQueryEntity(T entity) {
public static <T> T createQueryEntity(T entity) throws InstantiationException, IllegalAccessException {
Class<?> cl = entity.getClass();
Class<?> proxied = null;
if (cl.isInterface()) {
LOG.debug("The given entity is interface");
} else {
proxied = new ByteBuddy()
.subclass(cl)
.method(ElementMatchers.not(ElementMatchers.isClone().or(ElementMatchers.isFinalizer()).or(ElementMatchers.isEquals()).or(ElementMatchers.isHashCode()).or(ElementMatchers.isToString())))
.intercept(MethodDelegation.to(new MyMethodInterceptor()))
.make()
.load(ClassLoader.getSystemClassLoader(), ClassLoadingStrategy.Default.WRAPPER)
.getLoaded();
}
try {
return (T) proxied.newInstance();
} catch(Exception exception) {
throw new RuntimeException(exception);
}
return (T) createQueryEntity(cl);
}


Expand All @@ -131,7 +116,7 @@ public static <T> T createQueryEntity(T entity) {
return new Path<Object>(currentPath.getPathString().concat(".*"), currentPath.getEntity());
} else {
String name = ret.getClass().getSimpleName();
String[] extracted = name.split("\\$");
String[] extracted = name.split(CLASSNAME_SPLIT_PATTERN);
return new Path<Object>("*", extracted[0]);
}
}
Expand Down Expand Up @@ -240,7 +225,7 @@ public static <T extends IEntity> OptionalSyntax selectCount(T entity) {
resetQueryMessage();
getMessage().setSQL("SELECT");
String name = entity.getClass().getSimpleName();
String extracted[] = name.split("\\$");
String extracted[] = name.split(CLASSNAME_SPLIT_PATTERN);
getMessage().setCount(true);
if (extracted.length == LEN_3) {
getMessage().setEntity(extracted[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public class MyMethodInterceptor {
*/
private static final int NUM_2 = 2;

/**
* variable CLASSNAME_SPLIT_PATTERN
*/
private static final String CLASSNAME_SPLIT_PATTERN = "\\$";

/**
* Constructor MyMethodInterceptor
*
Expand All @@ -61,7 +66,7 @@ public MyMethodInterceptor() {
}

@RuntimeType
public Object intercept(@This Object proxyObject, @Origin Method method, @AllArguments Object[] methodArgs, @SuperMethod(nullIfImpossible = true) Method superMethod) throws FMSException {
public Object intercept(@This Object proxyObject, @Origin Method method, @AllArguments Object[] methodArgs, @SuperMethod(nullIfImpossible = true) Method superMethod) throws FMSException, InstantiationException, IllegalAccessException {

if (GenerateQuery.path.get() == null) {
GenerateQuery.path.set(new Path<Object>(extractPropertyName(method), extractEntity(proxyObject)));
Expand All @@ -79,7 +84,7 @@ public Object intercept(@This Object proxyObject, @Origin Method method, @AllArg
*/
private String extractEntity(Object obj) {
String name = obj.getClass().getSimpleName();
String[] extracted = name.split("\\$");
String[] extracted = name.split(CLASSNAME_SPLIT_PATTERN);
if (extracted.length == NUM_3) {
return extracted[0];
}
Expand Down Expand Up @@ -107,7 +112,7 @@ protected String extractPropertyName(Method method) {
*/
@SuppressWarnings("unchecked")
public <T> T createInstance(Object proxyObject, Method method, Object[] methodArgs, Method superMethod)
throws FMSException {
throws FMSException, InstantiationException, IllegalAccessException {
Object obj = null;
Class<?> type = method.getReturnType();
if (String.class.equals(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void setCalendar() {
}

@Test
public void testQuery_datatypes() {
public void testQuery_datatypes() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(new Data());
String query = select($(data.getStringData()), $(data.getIntData()), $(data.getByteData()), $(data.getShortData()), $(data.getLongData()),
$(data.getFloatData()), $(data.getDoubleData()), $(data.getCalendarData()), $(data.isBooleanData()), $(data.getDateData()),
Expand All @@ -69,7 +69,7 @@ public void testQuery_datatypes() {
}

@Test
public void testQuery_eq() {
public void testQuery_eq() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where($(data.getStringData()).eq("StringValue"), $(data.getIntData()).eq(10),
$(data.getByteData()).eq((byte) 10), $(data.getShortData()).eq((short) 10), $(data.getLongData()).eq((long) 10),
Expand All @@ -82,7 +82,7 @@ public void testQuery_eq() {
}

@Test
public void testQuery_eqUsingInvalidEnum() {
public void testQuery_eqUsingInvalidEnum() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where($(data.getStringData()).eq("StringValue"), $(data.getIntData()).eq(10),
$(data.getByteData()).eq((byte) 10), $(data.getShortData()).eq((short) 10), $(data.getLongData()).eq((long) 10),
Expand All @@ -95,7 +95,7 @@ public void testQuery_eqUsingInvalidEnum() {
}

@Test
public void testQuery_neq() {
public void testQuery_neq() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data))
.where($(data.getStringData()).neq("StringValue"), $(data.getIntData()).neq(10), $(data.getByteData()).neq((byte) 10),
Expand All @@ -108,7 +108,7 @@ public void testQuery_neq() {
}

@Test
public void testQuery_lt() {
public void testQuery_lt() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where($(data.getStringData()).lt("StringValue"), $(data.getIntData()).lt(10),
$(data.getByteData()).lt((byte) 10), $(data.getShortData()).lt((short) 10), $(data.getLongData()).lt((long) 10),
Expand All @@ -120,7 +120,7 @@ public void testQuery_lt() {
}

@Test
public void testQuery_lte() {
public void testQuery_lte() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where($(data.getStringData()).lte("StringValue"), $(data.getIntData()).lte(10),
$(data.getByteData()).lte((byte) 10), $(data.getShortData()).lte((short) 10), $(data.getLongData()).lte((long) 10),
Expand All @@ -132,7 +132,7 @@ public void testQuery_lte() {
}

@Test
public void testQuery_gt() {
public void testQuery_gt() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where($(data.getStringData()).gt("StringValue"), $(data.getIntData()).gt(10),
$(data.getByteData()).gt((byte) 10), $(data.getShortData()).gt((short) 10), $(data.getLongData()).gt((long) 10),
Expand All @@ -144,7 +144,7 @@ public void testQuery_gt() {
}

@Test
public void testQuery_gte() {
public void testQuery_gte() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where($(data.getStringData()).gte("StringValue"), $(data.getIntData()).gte(10),
$(data.getByteData()).gte((byte) 10), $(data.getShortData()).gte((short) 10), $(data.getLongData()).gte((long) 10),
Expand All @@ -156,7 +156,7 @@ public void testQuery_gte() {
}

@Test
public void testQuery_in() {
public void testQuery_in() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where($(data.getStringData()).in(new String[] { "StringValue1", "StringValue2" }),
$(data.getIntData()).in(new Integer[] { 10, 20 }), $(data.getByteData()).in(new Byte[] { 10, 20 }),
Expand All @@ -171,7 +171,7 @@ public void testQuery_in() {
}

@Test
public void testQuery_between() {
public void testQuery_between() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where(
$(data.getStringData()).between("StringValue1", "StringValue2"),
Expand All @@ -190,7 +190,7 @@ public void testQuery_between() {
}

@Test
public void testQuery_like() {
public void testQuery_like() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where($(data.getStringData()).startsWith("StringValue")).generate();
String expectedQuery = "SELECT * FROM Data WHERE StringData LIKE 'StringValue%'";
Expand All @@ -209,7 +209,7 @@ public void testQuery_like() {
}

@Test
public void testQuery_select() {
public void testQuery_select() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data.getSubData())).generate();
String expectedQuery = "SELECT SubData.* FROM Data";
Expand All @@ -218,7 +218,7 @@ public void testQuery_select() {
}

@Test
public void testQuery_orderby() {
public void testQuery_orderby() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).orderByAscending($(data.getStringData()), $(data.getIntData())).generate();
String expectedQuery = "SELECT * FROM Data ORDERBY StringData, IntData ASC";
Expand All @@ -234,7 +234,7 @@ public void testQuery_orderby() {
}

@Test
public void testQuery_not() {
public void testQuery_not() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).where($(data.getIntData()).eq(10).negate()).generate();
String expectedQuery = "SELECT * FROM Data WHERE NOT IntData = '10'";
Expand All @@ -243,7 +243,7 @@ public void testQuery_not() {
}

@Test
public void testQuery_pagination() {
public void testQuery_pagination() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = select($(data)).skip(10).generate();
String expectedQuery = "SELECT * FROM Data STARTPOSITION 11";
Expand All @@ -262,7 +262,7 @@ public void testQuery_pagination() {
}

@Test
public void testQuery_count() {
public void testQuery_count() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);
String query = selectCount(data).generate();
String expectedQuery = "SELECT count(*) FROM Data";
Expand All @@ -271,7 +271,7 @@ public void testQuery_count() {
}

@Test
public void testQuery_calendar() {
public void testQuery_calendar() throws InstantiationException, IllegalAccessException {
Data data = GenerateQuery.createQueryEntity(Data.class);

Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Expand All @@ -292,7 +292,7 @@ public void testQuery_calendar() {
}

@Test
public void testQuery_line() {
public void testQuery_line() throws InstantiationException, IllegalAccessException {
Invoice data = GenerateQuery.createQueryEntity(Invoice.class);
String query = select($(data.getLine())).generate();
String expectedQuery = "SELECT Line.* FROM Invoice";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public void testFindAll() throws FMSException {
}

@Test (enabled = false)
public void testExecuteQuery_get() throws FMSException {
public void testExecuteQuery_get() throws FMSException, InstantiationException, IllegalAccessException {
Customer customer = GenerateQuery.createQueryEntity(Customer.class);
String query = select($(customer.getId()), $(customer.getDisplayName())).where($(customer.getId()).eq(EntityCreator.customer.getId())).generate();

Expand All @@ -197,7 +197,7 @@ public void testExecuteQuery_get() throws FMSException {
}

@Test(enabled = false)
public void testExecuteQuery_post() throws FMSException {
public void testExecuteQuery_post() throws FMSException, InstantiationException, IllegalAccessException {
Customer customer = GenerateQuery.createQueryEntity(Customer.class);
String query = select($(customer.getId()), $(customer.getDisplayName())).where($(customer.getId()).eq(EntityCreator.customer.getId())).generate();
String newQuery = " ";
Expand All @@ -214,7 +214,7 @@ public void testExecuteQuery_post() throws FMSException {
}

@Test(enabled = false)
public void testExecuteQuery_postCompression() throws FMSException {
public void testExecuteQuery_postCompression() throws FMSException, InstantiationException, IllegalAccessException {
Customer customer = GenerateQuery.createQueryEntity(Customer.class);
String query = select($(customer.getId()), $(customer.getDisplayName())).where($(customer.getId()).eq(EntityCreator.customer.getId())).generate();
String newQuery = " ";
Expand Down Expand Up @@ -249,7 +249,7 @@ public void testExecuteQuery_invalidQuery() {
}

@Test(enabled = false)
public void testExecuteBatch() throws FMSException {
public void testExecuteBatch() throws FMSException, InstantiationException, IllegalAccessException {
BatchOperation batchOperation = new BatchOperation();

Customer customer = new Customer();
Expand Down Expand Up @@ -296,7 +296,7 @@ public void testExecuteBatch_Entity() throws FMSException {
}

@Test (enabled = false)
public void testExecuteBatch_Query() throws FMSException {
public void testExecuteBatch_Query() throws FMSException, InstantiationException, IllegalAccessException {
BatchOperation batchOperation = new BatchOperation();

Customer c = GenerateQuery.createQueryEntity(Customer.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public void testPurchaseQuery_JsonResponse() throws FMSException {
}

@Test (enabled = false)
public void testExecuteQuery_get() throws FMSException {
public void testExecuteQuery_get() throws FMSException, InstantiationException, IllegalAccessException {
Customer customerIn = getCustomer();
Customer customer = GenerateQuery.createQueryEntity(Customer.class);
String query = select($(customer.getId()), $(customer.getDisplayName())).where($(customer.getId()).eq(customerIn.getId())).generate();
Expand All @@ -513,7 +513,7 @@ public void testExecuteQuery_get() throws FMSException {
}

@Test(enabled=false)
public void testExecuteQuery_post() throws FMSException {
public void testExecuteQuery_post() throws FMSException, InstantiationException, IllegalAccessException {
Customer customerIn = getCustomer();
Customer customer = GenerateQuery.createQueryEntity(Customer.class);
String query = select($(customer.getId()), $(customer.getDisplayName())).where($(customer.getId()).eq(customerIn.getId())).generate();
Expand Down

0 comments on commit 2274df6

Please sign in to comment.