Method '*' has a name conflict with automatic generated getter/setter methods for attribute '*' in class '*'. Please use a different method name - umple

I have the following Umple model
class SomeClass{
att1;
Boolean setAtt1(String aAtt1){
//Do something
}
}
I need to override the setter method of att1. When I try to do it as in the model above, I get a warning. What is the appropriate way to extend setter or getter methods in such a case?

Setter and getter methods are auto generated, and should not be redefined. Alternatively, what you can do is to use the aspect-orientation features to add before or after as below
class SomeClass{
att1;
before setAtt1{
//Do something before
}
after setAtt1{
//Do something after
}
}

Related

How to do Spring Controller method specific serialization with Jackson?

I have two different serializers for String fields. I want to use either of them conditionally based on an annotation present on the calling Controller method. I'm looking at different ways of doing this via Jackson (eg. annotationIntrospector, JsonView etc). However, I do not see anywhere I can use method annotation during serialization. I can probably check if I can follow something similar to how Jackson implements JsonViews but haven't got to a solution yet.
Here is the use case.
// Dto
public class MyDto {
#Masked //Mask the fields with an option to avoid masking based controller method annotation.
private final String stringField;
// getters, setters.
}
// controller.
// default behavior is to serialize masked.
#ResponseBody
public MyDto getMaskedDto() {
// return dto with masked value.
return this.someService.getDto();
}
// Controller
#IgnoreMasking // Do not mask the dto if method is annotated with #IgnoreMasking.
#ResponseBody
public MyDto getDtoSkipMasking() {
// return dto without masking String field value.
return this.someService.getDto();
}
You could extend Jackon's StdSerializer and override the serialize method.
So something like this:
Create a new CustomSerializer class extending StdSerializer
Override the serialize method
In the overridden method, check for the existence of the object being serialised for the existence of your custom annotation (ie IgnoreMasking). You can do this via reflection
Do your processing
Register your custom serializer into Jackson's ObjectMapper configuration as a new SimpleModule

SpringMVC Custom Getter Method

Is there a way to specify a custom Getter method in SpringMVC binding, rather than the default PropertyDescriptors?
I feel like I'm venturing into outer space with this question, sounds so simple and yet no Google results. I'm doing this because my model has a Boolean has a getter named isSomething() and SpringMVC will only check that for primitive booleans. For the class Boolean it'll complain that a getter is not found, it requires getSomething().
I should be able to just specify custom getters/setters if I need those, isn't that the case?
Since I know people will suggest custom Property Editors etc., I put a breakpoint in these methods, and it doesn't even get here -- the problem happens somewhere up the chain, with PropertyDescriptors:
binder.registerCustomEditor(Boolean.class, new PropertyEditorSupport() {
#Override
public void setValue(Object value) {
// TODO Auto-generated method stub
super.setValue(value);
}
#Override
public Object getValue() {
// TODO Auto-generated method stub
return super.getValue();
}
});
I wanted to play with these custom editors, but the code never gets here. The error is thrown before that.
Invalid property 'test' of bean class [Model]: Bean property 'test' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
How can I achieve this fairly common scenario of custom getters/setters?

How can I create "static" method for enum in Kotlin?

Kotlin already have number of "static" methods for enum class, like values and valueOf
For example I have enum
public enum class CircleType {
FIRST
SECOND
THIRD
}
How can I add static method such as random(): CircleType? Extension functions seems not for this case.
Just like with any other class, you can define a class object in an enum class:
enum class CircleType {
FIRST,
SECOND,
THIRD;
companion object {
fun random(): CircleType = FIRST // http://dilbert.com/strip/2001-10-25
}
}
Then you'll be able to call this function as CircleType.random().
EDIT: Note the commas between the enum constant entries, and the closing semicolon before the companion object. Both are now mandatory.

Add a Model Attribute using ControllerAdvice

I have marked a class with #ControllerAdvice
Added a method
#ModelAttribute
public void setSourceAppId(Model model)
{
model.addAttribute("myattribute","1234");
}
But this method is not getting called at all.
I have not yet used modelattribute with a setter method so i cannot tell that thats a wrong approach, but changing it to a getter will do the job. Also, you dont need to add it to the model yourself. Modelattribute is handling exactly that.
#ModelAttribute("myattribute")
public int getSourceAppId()
{
return 1234;
}

StructureMap Beginner | Property Injection

Part of this question was already asked here : structuremap Property Injection but the answer was never given.
With StructureMap, is it possible to do Property Injection such that
class SomeController : Controller
{
public IService Service
{
get;
set;
}
}
gets injected properly? I am a
StructureMap supports setter/property injection. So you could do the following:
public class SomeController : Controller
{
[SetterProperty]
public IService Service { get; set; }
}
and then:
ObjectFactory.Initialize(x =>
{
x.For<IService>()
.Use<ServiceImpl>();
});
or if you don't like the idea of cluttering your controllers with StructureMap specific attributes you could configure it like this:
ObjectFactory.Initialize(x =>
{
x.For<IService>()
.Use<ServiceImpl>();
x.ForConcreteType<SomeController>()
.Configure
.Setter<IService>(c => c.Service)
.IsTheDefault();
});
Also note that property injection is suitable in scenarios where the presence of this property is not compulsory for the correct functioning of the controller. For example think of a logger. If the consumer of the controller doesn't inject any specific implementation of a logger into the property the controller still works it's just that it doesn't log. In your case you are using a service and I would use constructor injection if your controller actions depend on this service. So the question you should ask yourself is: will my controller crash when I call some its action if this property is null? If the answer to this question is yes then I would recommend constructor injection. Also when you use a constructor injection you force the consumer of this controller to specify an implementation because he cannot obtain an instance of the controller without passing a proper service in the constructor.
To inject dependencies for all properties of a certain type, use the SetAllProperties method as part of the initialization of your ObjectFactory:
ObjectFactory.Initialize(x =>
{
x.SetAllProperties(x => x.OfType<IService>());
});
It is also possible to define policies for setter injection, see this post.

Resources