I have the following implementation:
private SomeWritter someWritter(String someArgument) {
SomeWritter.Builder builder = SomeWritter.builder(someArguments);
builder = builder.addColumn("colName1", TargetClass::getArg1)
builder = builder.addColumn("colName2", TargetClass::getArg2)
return builder.build();
}
private Builder<T> addColumn(String colName, ToDoubleFunction<T> getter){
//implementation goes here
}
my issue is that I need to iterate over the addColumns call, something among these lines:
private void SomeWritter(String someArgument) {
SomeWritter.Builder builder = SomeWritter.builder(someArguments);
for (Field field : getFilteredFieldsFromClass(TargetClass.class)) {
builder = builder.addColumn(field.getName(), [SOMEHOW GET THE REF TO GETTER HERE])
}
return builder.build();
}
in order to get the refference to the getter, I tryed to do
TargetClass.class.getMethod("getArg1", ...);
this works, but I have a Method, not a ToDoubleFunction.
I need to somehow get that ToDoDoubleFunction, programatically, I want to do the same that the TargetClass:: does, dinamically, not harcoded. any ideas ?
import java.lang.reflect.Field;
public class Main {
static class Example{
double arg1;
int arg2;
}
interface Foo<T>{
double toDouble(T example);
}
public static void addColumn(Foo<Example> foo){
//do nothing
}
public static void main(String[] args) {
final var example = new Example();
for(Field field: Example.class.getDeclaredFields()){
addColumn(example1 -> {
try {
return (double) field.get(example1);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
});
}
}
}
That code iterates over all fields of the Example class and uses the reflection inside a lambda.
Side Note. Intellij can replace method references with lambda when you click alt+enter when cursor is on them (Windows).
Related
So i'm trying to develop a fluent interface for some DSL in Java and am running into a problem. The interface consists of builder pattern classes that each construct part of the DSL. The problem is that a specific builder sometimes needs to transfer control to a different builder which at some point returns to the 'parent' builder. For example, there is a 'SequenceBuilder' that creates a list of statements but it need sometimes transfer control to an 'IfBuilder' that is used to create an 'if' statement. When the IfBuilder is finished, it needs to return to the SequenceBuilder. Now there are some builders that are not always called by the same type of other builder and therefore need to be able to return builders of a different datatype. The example program below demonstrates this:
package com.example.fluent;
public class Test {
public class Type1 {
public Type1 test1() {
System.out.println("test1");
return this;
}
public Type3 gotype3() {
System.out.println("gotype3");
return new Type3<Type1>(this);
}
public void endtype1() {
System.out.println("endtype1");
}
}
public class Type2 {
public Type2 test2() {
System.out.println("test2");
return this;
}
public Type3 gotype3() {
System.out.println("gotype3");
return new Type3<Type2>(this);
}
public void endtype2() {
System.out.println("endtype2");
}
}
public class Type3<T> {
private T parent;
public Type3(T parent) {
this.parent = parent;
}
public Type3 test3() {
System.out.println("test3");
return this;
}
public T endtype3() {
System.out.println("endtype3");
return parent;
}
}
public static void main(String[] args) {
new Test().run();
}
private void run() {
new Type1()
.test1()
.gotype3()
.test3()
.endtype3()
.test1()
.endtype1();
}
}
You can see in the .run() method that i start by creating a new instance of the Type1 class, which follows the builder pattern. At some point i'm calling the .goType3() method which transfers control to the Type3 builder. Because it has to return control at some point to Type1 again, a reference to that builder is passed via constructor to Type3. When it's time to return to Type1, the method .endtype3() is called. And here lies the problem. I'm using generics to (try) to return the datatype of Type1 but instead it's converted to an Object type. That datatype obviously does not have the methods that Type1 has and therefore the pattern is broken.
Question: is there some other way to return the proper datatype to the parent builder?
You're not using generics as much as you want to. You're using return type Type3. You need to use Type3<Type1>, Type3<Type2> and Type3<T> instead.
Thanks to Rob Spoor i got my code finally working, though i didn't understand it at first. The solution lies in changing the code in the following ways:
package com.example.fluent;
public class Test {
public class Type1 {
public Type1 test1() {
System.out.println("test1");
return this;
}
public Type3<Type1> gotype3() {
System.out.println("gotype3");
return new Type3<Type1>(this);
}
public void endtype1() {
System.out.println("endtype1");
}
}
public class Type2 {
public Type2 test2() {
System.out.println("test2");
return this;
}
public Type3<Type2> gotype3() {
System.out.println("gotype3");
return new Type3<Type2>(this);
}
public void endtype2() {
System.out.println("endtype2");
}
}
public class Type3<T> {
private T parent;
public Type3(T parent) {
this.parent = parent;
}
public Type3<T> test3() {
System.out.println("test3");
return this;
}
public T endtype3() {
System.out.println("endtype3");
return parent;
}
}
public static void main(String[] args) {
new Test().run();
}
private void run() {
// new Type1().test1().test1().endtype1();
new Type1().test1().gotype3().test3().endtype3().test1().endtype1();
// new Type2().test2().gotype3().test3().endtype3().test2().endtype2();
}
}
See the changes in the return types of the .gotype3() methods in the Type1 and Type2 classes, as well as the return type of the .test3() method. Now everything is working fine.
I have method that is called by another service and it just change one of the field for some rows in database. Method looks like this:
void errorOrders() {
List<Orders> orders = OrderDAO.getErrorOrders(); //select only fields with status 'error'
orders.forEach(order -> order.setStatus(OrderStatus.NEW);
//some logging etc.
}
Is there any way to unit test this method? Can I inject myself inside this method and check if orders status was changed?
Cheers!
I would recommend you refactor your class to make your code testable. Ideally you would inject the dependency that represents the OrderDAO:
class ErrorChecker {
private final OrderDAO orderDAO;
public ErrorChecker(OrderDAO orderDAO) {
this.orderDAO = orderDAO;
}
public void errorOrders() {
List<Orders> orders = orderDAO.getErrorOrders();
orders.forEach(order -> order.setStatus(OrderStatus.NEW);
}
}
Then your test code would look like:
#Test
void testErrorOrders() {
Order order1 = mock(Order.class);
Order order2 = mock(Order.class);
OrderDAO orderDAO = mock(OrderDAO.class);
when(orderDAO.getErrorOrders()).thenReturn(List.of(order1, order2));
ErrorChecker errorChecker = new ErrorChecker(orderDAO);
errorChecker.errorOrders();
verify(order1).setState(OrderStatus.NEW);
verify(order2).setState(OrderStatus.NEW);
}
There are ways to mock static methods but I would recommend refactoring to inject the dependencies as it has many other benefits beside testability.
If you need to leave the method as static then you can still mock it (in v3.4+ of Mockito):
#Test
void testErrorOrders() {
try (MockedStatic mocked = mockStatic(OrderDAO.class)) {
mocked.when(OrderDAO.getErrorOrders()).thenReturn(List.of(order1, order2));
ErrorChecker errorChecker = new ErrorChecker(orderDAO);
errorChecker.errorOrders();
mocked.verify(order1).setState(OrderStatus.NEW);
}
}
#ismail and #khelwood already provided good answers.
If you mock the Object, you can control/see what happens to it
If you change an Object, where you can access the state via public methods, use those
If you change an Object whose state you cannot access with normal code, use Java Reflections to look at member variables.
If you set up Objects, that pass their data to streams and other output, you can put some additional streams etc in between. Use inheritance and reflection if necessary
Simple example of using Reflection on a shielded class:
package stackoverflow.simplefieldaccess;
public class ShieldedClass {
private int mStatus;
public ShieldedClass() {
mStatus = 666;
}
public void setStatus(final int pStatus) {
mStatus = pStatus; // usually with ints be careful and do checks here, but for the sake of simplicity we leave that out
}
#Override public String toString() {
return getClass().getSimpleName() + "[status:" + mStatus + "]";
}
}
Code to access it via reflection in a few ways:
package stackoverflow.simplefieldaccess;
import java.lang.reflect.Field;
import jc.lib.lang.reflect.JcFieldAccess;
public class SimpleFieldAccess {
public static void main(final String[] args) throws NoSuchFieldException, SecurityException {
final ShieldedClass so = new ShieldedClass();
System.out.println("Object.status before change: " + so);
so.setStatus(667);
System.out.println("Object.status after change: " + so);
System.out.println();
System.out.println("Accessing Object.status via Reflection...");
final Class<? extends ShieldedClass> cls = so.getClass();
final Field fieldToChance = cls.getDeclaredField("mStatus");
{
System.out.println("\nBad read access");
try { // will result in java.lang.IllegalAccessException
System.out.println("\tReading Object.status fiels via Reflection: " + fieldToChance.getInt(so));
throw new IllegalStateException("UNEXOECTED ERROR!");
} catch (final java.lang.IllegalAccessException e) {
System.out.println("\tAs expected: IllegalAccessException");
}
}
{
System.out.println("\nBad write access");
try { // will result in java.lang.IllegalAccessException
fieldToChance.set(so, Integer.valueOf(1337));
System.out.println("\tObject.status after change: " + so);
} catch (final java.lang.IllegalAccessException e) {
System.out.println("\tAs expected: IllegalAccessException");
}
}
{
System.out.println("\nGood manual read and write access");
final boolean isFieldOriginallyAccessible = fieldToChance.isAccessible();
try { // will result in java.lang.IllegalAccessException
if (!isFieldOriginallyAccessible) fieldToChance.setAccessible(true);
System.out.println("\tReading Object.status field via Reflection: " + fieldToChance.getInt(so));
fieldToChance.set(so, Integer.valueOf(4321));
System.out.println("\tObject.status after change: " + so);
} catch (final java.lang.IllegalAccessException e) {
e.printStackTrace();
} finally {
if (!isFieldOriginallyAccessible) fieldToChance.setAccessible(false);
}
}
{
System.out.println("\nGood automated read and write access");
try (JcFieldAccess fa = new JcFieldAccess(fieldToChance)) { // will result in java.lang.IllegalAccessException
System.out.println("\tReading Object.status field via Reflection: " + fieldToChance.getInt(so));
fieldToChance.set(so, Integer.valueOf(123));
System.out.println("\tObject.status after change: " + so);
} catch (final java.lang.IllegalAccessException e) {
e.printStackTrace();
}
}
}
}
For reflections, when I want to access fields, I use my homebrew class that makes it easier to get access to the field and afterwards restore it to normal (last example above uses this):
package jc.lib.lang.reflect;
import java.io.Closeable;
import java.lang.reflect.AccessibleObject;
public class JcFieldAccess implements Closeable {
private final AccessibleObject mField;
private final boolean mIsAccessible;
public JcFieldAccess(final AccessibleObject pField) {
mField = pField;
mIsAccessible = mField.isAccessible();
if (!mIsAccessible) mField.setAccessible(true);
}
#Override public void close() {
if (mIsAccessible) return;
if (mField != null) mField.setAccessible(false);
}
}
The trick with this util class is that when used in a try-resource block, its close() method will get called automatically, whether the block fails or not. It's the same as having the close() or in this case setAccessible(false) call in the finally block, with some extra checks.
Let the class be:
class HandleErrorOrders {
private OrderDAO orderDAO;
HandleErrorOrders(final OrderDAO orderDAO) {
this.orderDAO = orderDAO;
}
public void errorOrders() {
List<Orders> orders = OrderDAO.getErrorOrders(); //select only fields with status 'error'
orders.forEach(order -> order.setStatus(OrderStatus.NEW);
//some logging etc.
}
}
You need to use assert methods to check end state.
To test, write something like:
class HandleErrorOrdersTest {
#Mock
private OrderDAO orderDAO;
#InjectMocks
private HandleErrorOrders handleErrorOrders;
#Test
void testErrorOrders() {
Order order1 = mock(Order.class);
Order order2 = mock(Order.class);
when(orderDAO.getErrorOrders()).thenReturn(List.of(order1, order2));
ErrorChecker errorChecker = new ErrorChecker(orderDAO);
errorChecker.errorOrders();
//asset checks
Assert.assertEquals(OrderStatus.NEW, order1.getStatus());
Assert.assertEquals(OrderStatus.NEW, order2.getStatus());
//verification checks
Mockito.verify(orderDAO).getErrorOrders();
}
}
I would like to know if exists such thing like java parser (like parser xml). I mean from a String like this
String javaToParse = "public class x{void foo(){...} void baar(){...}}"
I could for example get the body (in string format) from foo or the body of all methods etc..
I have some like that
public class Some {
#PostConstruct
public void init1() {
//some to do
}
#PostConstruct
public void init2() {
//some to do2
}
#PostConstruct
public void init3() {
//some to do3
}
//..more..
}
Here there are one or more #PostConstruct
This class is autogenerated and I cannot modified it manually.
I would like to iterate all #PostConstruct methods and put all his bodies into only one #Postcontruct method and export to file and get this:
public class Some {
#PostConstruct
public void init() {
//some to do
//some to do2
//some to do3
}
}
I see that it's possible to do this getting that file as String and operate manually with fors and search manually but maybe there are librearies to do it.
EDIT:
Resolved with JavaParser
If somebody has similar problem here my solution:
public static void createUniquePostContruct(File InputFile,File outputFile) throws FileNotFoundException {
//get class to modified
FileInputStream in = new FileInputStream(ficheroEntradaJava);
// parse the file
CompilationUnit cu = JavaParser.parse(in);
//store methods with #PostContruct
List<MethodDeclaration> methodsPost = new ArrayList<>();
//iterate over all class' methods
cu.accept(new VoidVisitorAdapter<Void>() {
#Override
public void visit(MethodDeclaration method, Void arg) {
//if this method has #PostConstruct
if (method.getAnnotationByName("PostConstruct").isPresent()) {
methodsPost .add(method);
}
}
}, null);
//delete all methods with #PostConstruct
methodsPost.forEach((method) -> method.remove());
//add a unique #PostConstruct method using body of anothers #PostConstruct methods
MethodDeclaration uniqueMethodPostConstruct = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC), new VoidType(), "init");
uniqueMethodPostConstruct.addAnnotation("PostConstruct");
BlockStmt bodyUniqueMethodPostConstruct= new BlockStmt();
metodosPost.forEach(method-> {
method.getBody().get().getStatements().forEach(statement -> {
bodyUniqueMethodPostConstruct.addStatement(statement);
});
});
metodoUnicoPostConstruct.setBody(bodyUniqueMethodPostConstruct);
//get main class and put our method
Optional<ClassOrInterfaceDeclaration> clazz = cu.getClassByName(ficheroEntradaJava.getName().split("\\.")[0]);
clazz.get().addMember(uniqueMethodPostConstruct);
System.out.println(cu.toString());
//write to file
try (PrintWriter out = new PrintWriter(outputFile)) {
out.println(cu.toString());
} catch (FileNotFoundException ex) {
Logger.getLogger(ParserMetodos.class.getName()).log(Level.SEVERE, null, ex);
}
}
public class MyXML {
private MessageParser messageParser;
private String valueA;
private String valueB;
private String valueC;
public MyXML (MessageParser messageParser) {
this.messageParser=messageParser;
}
public void build() {
try {
setValueA();
setValueB();
setValueC();
} catch (Exception e) {
e.printStackTrace();
}
}
private void setValueA() {
valueA = messageParser.getArrtibuteUsingXPath("SomeXPath1...");
}
private void setValueB() {
valueB = messageParser.getArrtibuteUsingXPath("SomeXPath2...");
}
private void setValueC() {
valueC = messageParser.getArrtibuteUsingXPath("SomeXPath...");
}
public String getValueA() {
return valueA;
}
public String getValueB() {
return valueB;
}
public String getValueC() {
return valueC;
}
}
So I need to use Mockito to test the builder method. Im fairly new to Mockito could someone give me some example code as to how I might write a test for the builder method?
If you want to suggest any ways I might change the design of the class or make it easier to test let me know.
To test build() you can try :
#RunWith(MockitoJUnitRunner.class)
public class YourTest {
#Mock
private private MessageParser messageParserMock;
// this one you need to test
private MyXML myXML;
#Test
public void test() {
myXML = new MyXML(messageParserMock);
// I believe something like this should work
Mockito.doAnswer(/* check mockito Answer to figure out how */)
.when(messageParserMock).getArrtibuteUsingXPath(anyString());
// you should do this for all your 3 getArrtibuteUsingXPath because setValueA(), setValueB(), setValueC() are called that one and then call build and verify results
myXML.build(); // for instance
assertEquals("something you return as Answer", myXML.getValueA());
}
}
The resource https://static.javadoc.io/org.mockito/mockito-core/2.8.9/org/mockito/Mockito.html#stubbing_with_exceptions might be useful - it describes how to stub void methods call.
I have a android application, but it is not relevant.
I have a class called "Front controller" which will receive some message
through it's constructor. The message, for brievity, could be an integer.
I want somewhere else to create a new controller which will execute
a method based on the integer defined above
public class OtherController {
#MessageId("100")
public void doSomething(){
//execute this code
}
#MessageId("101")
public void doSomethingElse(){
//code
}
}
The front controller could be something like this:
public class FrontController {
private int id;
public FrontController(int id){
this.id=id;
executeProperControllerMethodBasedOnId();
}
public void executeProperControllerMethodBasedOnId(){
//code here
}
public int getId(){
return id;
}
}
So, if the Front Controller will receive the integer 100, it
will execute the method annotated with #MessageId(100). The
front controller don't know exactly the class where this method
is.
The problem which I found is that I need to register somehow
each controller class. I Spring I had #Component or #Controller
for autoloading. After each controllers are register, I need to
call the properly annotated method.
How to achieve this task? In Spring MVC, I had this system
implemented, used to match the HTTP routes. How could I implement
this in a plain java project?
Any suggestions?
Thanks to Google Reflections (hope you can integrate this in your android project.)
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections-maven</artifactId>
<version>0.9.8</version>
</dependency>
For optimisation I've added the requirement to also annotate the class with MessageType annotation and the classes should be in the same package (org.conffusion in my example):
#Retention(RetentionPolicy.RUNTIME)
#Target(ElementType.TYPE)
public #interface MessageType {
}
The OtherController looks like:
#MessageType
public class OtherController {
#MessageId(id=101)
public void method1()
{
System.out.println("executing method1");
}
#MessageId(id=102)
public void method2()
{
System.out.println("executing method2");
}
}
The implementation will look like:
public void executeProperControllerMethodBasedOnId() {
Set<Class<?>> classes = new org.reflections.Reflections("org.conffusion")
.getTypesAnnotatedWith(MessageType.class);
System.out.println("found classes " + classes.size());
for (Class<?> c : classes) {
for (Method m : c.getMethods()) {
try {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
Object o = c.newInstance();
if (mid.id() == id)
m.invoke(o);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Maybe you can optimise and build a static hashmap containing already scanned message ids.
You need to implement some of the work by yourself using reflection, I would recommend to prepare message handlers on initial phase in regards to performance. Also you possibly want to think about Singleton/Per Request controllers. Some of the ways to implement the solution:
interface MessageProcessor {
void execute() throws Exception;
}
/* Holds single instance and method to invoke */
class SingletonProcessor implements MessageProcessor {
private final Object instance;
private final Method method;
SingletonProcessor(Object instance, Method method) {
this.instance = instance;
this.method = method;
}
public void execute() throws Exception {
method.invoke(instance);
}
}
/* Create instance and invoke the method on execute */
class PerRequestProcessor implements MessageProcessor {
private final Class clazz;
private final Method method;
PerRequestProcessor(Class clazz, Method method) {
this.clazz = clazz;
this.method = method;
}
public void execute() throws Exception {
Object instance = clazz.newInstance();
method.invoke(instance);
}
}
/* Dummy controllers */
class PerRequestController {
#MessageId(1)
public void handleMessage1(){System.out.println(this + " - Message1");}
}
class SingletonController {
#MessageId(2)
public void handleMessage2(){System.out.println(this + " - Message2");}
}
class FrontController {
private static final Map<Integer, MessageProcessor> processors = new HashMap<Integer, MessageProcessor>();
static {
try {
// register your controllers
// also you can scan for annotated controllers as suggested by Conffusion
registerPerRequestController(PerRequestController.class);
registerSingletonController(SingletonController.class);
} catch (Exception e) {
throw new ExceptionInInitializerError();
}
}
private static void registerPerRequestController(Class aClass) {
for (Method m : aClass.getMethods()) {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
processors.put(mid.value(), new PerRequestProcessor(aClass, m));
}
}
}
private static void registerSingletonController(Class aClass) throws Exception {
for (Method m : aClass.getMethods()) {
if (m.isAnnotationPresent(MessageId.class)) {
MessageId mid = m.getAnnotation(MessageId.class);
Object instance = aClass.newInstance();
processors.put(mid.value(), new SingletonProcessor(instance, m));
}
}
}
/* To process the message you just need to look up processor and execute */
public void processMessage(int id) throws Exception {
if (processors.containsKey(id)) {
processors.get(id).execute();
} else {
System.err.print("Processor not found for message " + id);
}
}
}