Javassit: How to remove a superclass - bytecode

How do I remove a super class?
from
Foo extends Bar{
}
to
Foo{
}

Related

Can I change a method's return type present in the child class, when both the methods present in the Parent and Child classes have same parameters?

class Vehicle {
public void show() {
System.out.println("Parent");
}
}
class Car extends Vehicle{
public int show() {
System.out.println("Child");
return 3;
}
}
When I am trying to override Car class' method in child class, it is showing error but when I specify a parameter in the method, the error goes away.

Spring Boot: #Value returns null

I am new to springmvc, so the question may sounds dumb.
I have similar issue to this Spring Boot: #Value returns always null. Example that i have:
System Properties
<sysproperty key="bar" value="bar_value" />
Bean Configuration
#Configuration
public class Foo {
#Value("${bar}")
private String bar;
#Bean(name = "getBar")
public String getBar() {
return bar;
}
}
Access Bean (index.jsp)
String bar = webApplicationContext.getBean("getBar", String.class); // returns null instead of "bar_value"
However, if i change the Bean Configuration to
#Configuration
public class Foo {
#Value("${bar}")
#Bean(name = "getBar")
public String getBar(String bar) {
return bar;
}
}
and then accessing it, the returned value is correct, what could be the reason of this? I know that things are controlled by the framework so i don't have much visibility about the instantiation process of the app, help is well appreciated.
Please make use of Setter Injection. Hope it will resolve your issue.
public class Foo {
private String bar;
#Autowired
public setBar(#Value("${bar}") String bar) {
this.bar = bar;
}
// standard getter
#Bean(name = "getBar")
public String getBar() {
return bar;
}
}

MyHow to inject bean using constructor and call method in springboot

#Service
abstract class A {
protected MyObj obj;
protected A(MyObj obj) {
this.obj = obj;
}
public abstract XYZ getTrade();
}
#Service
public class B extends A {
B(MyObj obj) {
super(obj);
}
public XYZ getTrade() {}
}
#Service
public class C extends A {
C(MyObj obj) {
super(obj);
}
public XYZ getTrade() {}
}
#Controller
public class MyController {
#GemMapping("/test")
public void test() {
MyObj obj = new MyObj();
if(condition 1) {
//call getTrade() method of class B
}
if(condition 2) {
//call getTrade() method of class C
}
}
}
MyObj is a user-defined POJO that is not managed bean.
I have the above classes. now I have to call getTrade() method based on some condition in Controller. Could you please help/advise?
this answer is based on the information provided by the question.
to inject beans you could do:
#Controller
public class MyController {
private final B b;
private final C c;
public MyController (B b, C c) {
this.b = b;
this.c = c;
}
.........
}
in this case, beans must have to have one no-args constructor. so, In your case what you can do is have a no-args constructor in each service. then set your myObj by setter. that will do the trick.
If you want to achieve polyformism on your sevices you need to add a Qualifier in your classes B and C. So the question now became how to inject a service dynamically on runtime, and this is answered here:
How to dynamically inject a service using a runtime "qualifier" variable?

How to only retrieve the nested document mongodb and spring boot

I have the following document and nested document.
class Foo {
public String name;
public String description;
public LocalDateTime someDate;
}
#Document
class Bar {
#Id
public String id;
public String details;
public List<Foo> foos;
}
My custom query looks as follows:
Query query = new Query(
Criteria.where("foo.name").is("Some name").and("id").is("myId")
);
List<Bar> resultList = operations.find(query, Bar.class);
I want to retrieve the the single Foo document that matches "Some name". And not the Bar document with the Foo list. How would I do this?
Thanks
If your Foo class doesn't have a collection in database and it's embedded in Bar as you described in your question. You will need to create a custom repository to get what you are looking for.
interface BarRepository extends BarCustomRepository, MongoRepository<Bar, String> {
}
interface BarCustomRepository {
Foo findFooByName(String fooName);
}
class BarRepositoryImpl implements BarCustomRepository {
#Autiwired
private MongoTemplate mongoTemplate;
#Override
Foo findFooByName(String fooName) {
Bar bar = mongoTemplate.findOne(new Query ( Criteria.where("foos.name").is(fooname)), Bar.class);
Foo foo = bar.getFooByName(fooName);// implement this method in your Bar class. // to Get a foo by fooName from foos list
return foo;
}
}
I hope that helps.

dependency injection: conditional

This is an extremely simplified example of something I'm already doing in Grails:
// this can be a service or normal class
public abstract class Person {
public final String introduceSelf() {
return "Hi, I'm " + getFullName()
}
protected abstract String getFullName()
}
// Service
class AlexService extends Person {
protected String getFullName() {
return "Alex Goodman"
}
}
// Service
class BobService extends Person {
protected String getFullName() {
return "Bob Goodman"
}
}
// Service
class CarlService extends Person {
protected String getFullName() {
return "Carl Goodman"
}
}
// Controller
class IntroduceController {
def alex
def bob
def carl
def index() {
if(params.person == "a")
render alex.introduceSelf()
if(params.person == "b")
render bob.introduceSelf()
if(params.person == "c")
render carl.introduceSelf()
}
}
I'm looking for more object oriented way of doing it, something like:
// Controller
class IntroduceController {
def person
def index() {
// inject a proper person in a more object oriented way
render person.introduceSelf()
}
}
Can you suggest how to achieve this in a more object-oriented/dynamic way?
You can write a person bean definition in grails-app/conf/spring/resources.groovy file. For example:
beans = {
person(BobService)
}
For more information, check this: http://grails.org/doc/latest/guide/spring.html#springdslAdditional
class IntroduceController {
def grailsApplication
def index() {
def person = grailsApplication.mainContext.getBean( params.person )
person.doSomething()
}
}
You must make sure, that your services' names correspond to params.person value, params.person = 'bob' would work, params.person = 'b' would not

Resources