Pass a class as argument to another class Java

112 Views Asked by At

I am using a data class written in POJO in package myApp. I have another Package UtilLibrary which I have imported as a jar file to the package myApp.

I have developed a method in UtilLibrary by hardcoding the dataclass now I need to remove the same, for which I am looking for solutons to pass the data class as argument. Below is a sample code, any help is really appreciated.

import com.package.dataclasses.TestTable;

public class Sample {
    public String putBatchRecords(DynamoDbEnhancedClient enhancedClient, Object tableObj) {
        try {
            DynamoDbTable<TestTable> objTableMappedClass = enhancedClient.table("Test_Table", TableSchema.fromBean(TestTable.class));
            BatchWriteItemEnhancedRequest batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder()
                    .writeBatches(
                            WriteBatch.builder(TestTable.class)
                                    .mappedTableResource(objTableMappedClass)
                                    .addPutItem(builder -> builder.item((TestTable) tableObj))
                                    .build()
                    )
                    .build();
            enhancedClient.batchWriteItem(batchWriteItemEnhancedRequest);
            }
        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "Batch write successful";
    }
}

In the above code, i need to remove the import statement - hence pass the TestTable as object to putBatchRecords. Removing the statement shows below errors.

enter image description here

1

There are 1 best solutions below

4
On

You can change your code like this: Replace the class name with the fully qualified class name. Then you do not need to import that class.

public class Sample {
    public String putBatchRecords(DynamoDbEnhancedClient enhancedClient, Object tableObj) {
        try {
            DynamoDbTable<com.package.dataclasses.TestTable> objTableMappedClass = enhancedClient.table("Test_Table", TableSchema.fromBean(com.package.dataclasses.TestTable.class));
            BatchWriteItemEnhancedRequest batchWriteItemEnhancedRequest = BatchWriteItemEnhancedRequest.builder()
                    .writeBatches(
                            WriteBatch.builder(com.package.dataclasses.TestTable.class)
                                    .mappedTableResource(objTableMappedClass)
                                    .addPutItem(builder -> builder.item((com.package.dataclasses.TestTable) tableObj))
                                    .build()
                    )
                    .build();
            enhancedClient.batchWriteItem(batchWriteItemEnhancedRequest);
            }
        } catch (DynamoDbException e) {
            System.err.println(e.getMessage());
            System.exit(1);
        }
        return "Batch write successful";
    }
}

But it is not clear why you need to remove that import. It does not make your code behave differently at runtime, and it does not make it easier to read. So removing one such import would only be meaningful if you want to remove a name collision with another TestTable class.

I strongly believe you do not want to hardcode against that one class name. Use a common base class, or an interface instead.