Create abstract method implementation using sun-codemodel?

149 views Asked by At

I am trying to create the following using sun-codemodel.

JavaRDD<Activitydaily> activityBatchDailyRDDs = 
    activityBatchDailyFunction.cassandraTable("test", "tbl")
        .map(new Function<CassandraRow, Activitydaily>() {
            public Activitydaily call(CassandraRow v1) throws Exception {
                return new Activitydaily();
            }
         });

Following is my code. But it is in complete. I don't know how to complete it.

JClass cassandraConnector = cm.directClass("com.datastax.spark.connector.cql.CassandraConnector");
JExpression functionExpression = JExpr._new(
    function.narrow(cassandraRow, activityBatchDailyRealTimeBean));

JExpression javaRDDAssignmentExpression = JExpr
    .invoke(JExpr.ref(className + "Function"), "cassandraTable")
        .arg(schema).arg(table);
javaRDDAssignmentExpression = javaRDDAssignmentExpression.invoke("map")
    .arg(functionExpression);
executerBlock.decl(javaRDD.narrow(activityBatchDailyRealTimeBean),
    className + "RDD", javaRDDAssignmentExpression);

The below is my current output.

JavaRDD<Activitydaily> ActivitydailyRDD = 
    ActivitydailyFunction.cassandraTable("demo", "activitydaily")
        .map(new Function<CassandraRow, Activitydaily>());
1

There are 1 answers

1
John Ericksen On

The real question here is: how does one define an anonymous inner class.

To Define an anonymouse inner class, you simply using the anonymousClass() method on the Jcm instance:

JDefinedClass anonymousFunctionClass = cm.anonymousClass(function.narrow(cassandraRow, activityBatchDailyRealTimeBean));
JMethod callMethod = anonymousFunctionClass.method(JMod.PUBLIC, Activitydaily.class, "call");
callMethod._throws(Exception.class);
JVar v1Param = callMethod.param(CassandraRow.class, "v1");
callMethod.body()._return(JExpr._new(cm.ref(Activitydaily.class)));

Then use it in your JExpr._new() call:

JClass cassandraConnector = cm.directClass("com.datastax.spark.connector.cql.CassandraConnector");

// Anonymous inner class declaration:
JDefinedClass anonymousFunctionClass = cm.anonymousClass(function.narrow(cassandraRow, activityBatchDailyRealTimeBean));
JMethod callMethod = anonymousFunctionClass.method(JMod.PUBLIC, Activitydaily.class, "call");
callMethod._throws(Exception.class);
JVar v1Param = callMethod.param(CassandraRow.class, "v1");
callMethod.body()._return(JExpr._new(cm.ref(Activitydaily.class)));

JExpression functionExpression = JExpr._new(anonymousFunctionClass);

JExpression javaRDDAssignmentExpression = JExpr
    .invoke(JExpr.ref(className + "Function"), "cassandraTable")
        .arg(schema).arg(table);
javaRDDAssignmentExpression = javaRDDAssignmentExpression.invoke("map")
    .arg(functionExpression);
executerBlock.decl(javaRDD.narrow(activityBatchDailyRealTimeBean),
        className + "RDD", javaRDDAssignmentExpression);

The result of this looks like the following:

activityBatchDailyFunction.cassandraTable("test", "tbl")
    .map(new Function<CassandraRow,Activity_daily>() {
        public Activity_daily call(CassandraRow v1) {
            return new Activity_daily();
        }
    }
);