Parent instance = new Child();
instance.method();
When we call method, JVM will find child method directly or find parent class firstly, find child, find method, or some other sequence?
If I may add a more comprehensive example:
class Parent {
public void print() {
System.out.println("Hello from Parent");
}
}
class Child extends Parent {
#Override
public void print() {
System.out.println("Hello from Child");
}
}
public class Main {
public static void main(String[] args) {
Parent parent = new Child();
parent.print();
}
}
This program will print Hello from Child. Although we instantiate the Child via the Parent, Java will look for the "nearest" implementation of print(), starting from the child.
Related
When I save a session with redis,
I'd like to add custom data.
RedisSessionRepository.class
....
#Override
public void save(CustomRedisSessionRepository.RedisSession session) {
if (!session.isNew) {
String key = getSessionKey(session.hasChangedSessionId() ? session.originalSessionId : session.getId());
Boolean sessionExists = this.sessionRedisOperations.hasKey(key);
if (sessionExists == null || !sessionExists) {
throw new IllegalStateException("Session was invalidated");
}
}
session.save();
//I want add code..... (custom data..)
}
So I decided to expand.
public class MyRedisSessionRepository extends RedisSessionRepository {
public MyRedisSessionRepository(RedisOperations<String, Object> sessionRedisOperations) {
super(sessionRedisOperations);
}
#Override
public void save(RedisSessionRepository.RedisSession session) {
super.save(session);
//add custom data...
}
}
But I can't.
The access modifier for RedisSession is 'default'.
public class RedisSessionRepository implements SessionRepository<RedisSessionRepository.RedisSession> {
...
final class RedisSession implements Session {
....
}
..
}
So I can't extend the save method of RedisSessionRepository.
Is there any other way Or is there an expandable class?
I have a simple test class:
public class Test {
public void foo() {
Object a = getClass();
}
}
Playing with the DefinitionsReferenceContributor I expect it to get the reference, but nothing happens. Breakpoint inside getReferencesByElement is not reached.
public class DefinitionsReferenceContributor extends PsiReferenceContributor {
#Override
public void registerReferenceProviders(PsiReferenceRegistrar registrar) {
PsiElementPattern.Capture<PsiMethodCallExpression> psiJavaTokenCapture = psiElement(PsiMethodCallExpression.class);
registrar.registerReferenceProvider(psiJavaTokenCapture, new PsiReferenceProvider() {
#NotNull
#Override
public PsiReference[] getReferencesByElement(#NotNull PsiElement element, #NotNull ProcessingContext context) {
return new PsiReference[0];
}
});
}
}
Changes to plugin.xml:
<depends>com.intellij.modules.platform</depends>
<depends>com.intellij.java</depends>
<extensions defaultExtensionNs="com.intellij">
<psi.referenceContributor implementation="DefinitionsReferenceContributor"/>
</extensions>
What am I doing wrong?
I was able to solve it this way:
PsiElementPattern.Capture<PsiLiteralExpression> psiLiteralExpressionCapture = PlatformPatterns.psiElement(PsiLiteralExpression.class);
registrar.registerReferenceProvider(psiLiteralExpressionCapture, psiReferenceProvider, PsiReferenceRegistrar.HIGHER_PRIORITY);
This fires the
public PsiReference[] getReferencesByElement(PsiElement psiElement,
ProcessingContext processingContext)
Method where i can process the element according to my needs.
I'm new to JOOQ... The following code seems to work in WildFly 22 but I'm not sure if that is the best way to do things. What is the preferred way to inject WF DataSource to JOOQ DAOs (my extended ones)? Is there a way to avoid doing the ".get()." in the service below and just leave #Resource(...) etc. connection related for the MyCompanyDAO to handle internally?
In other words: companyDAO.get().fetchOneById(id) vs. companyDAO.fetchOneById(id)
#Stateless
public class CompanyService extends DefaultCompanyService {
#Inject
private MyCompanyDAO companyDAO;
public Company find(Integer id) {
return companyDAO.get().fetchOneById(id);
}
}
#Stateless
public class MyCompanyDAO extends CompanyDao {
#Inject
private MyConnectionProvider cp;
public CompanyDAO get() { // since cannot use #Resource in dao constructor
this.configuration().set(cp).set(SQLDialect.POSTGRES);
return this;
}
// custom code here
}
public class CompanyDao extends DAOImpl<CompanyRecord, tables.pojos.Company, Integer> {
// jooq generated code here
}
#Stateless
#LocalBean
public class MyConnectionProvider implements ConnectionProvider {
#Resource(lookup = "java:/MyDS")
private DataSource dataSource;
#Override
public Connection acquire() throws DataAccessException {
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new DataAccessException("Could not acquire connection.", e);
}
}
#Override
public void release(Connection connection) throws DataAccessException {
try {
connection.close();
} catch (SQLException e) {
throw new DataAccessException("Could not release connection.", e);
}
}
}
Put initialization logic of MyCompanyDAO inside a #PostConstruct method.
#PostConstruct
public void init() {
this.configuration().set(cp).set(SQLDialect.POSTGRES);
}
This way, you don't need to call get:
#Inject
private MyCompanyDAO companyDAO;
public Company find(Integer id) {
return companyDAO.fetchOneById(id);
}
How about using constructor injection instead? The generated DAO classes offer a constructor that accepts a Configuration precisely for that:
#Stateless
public class MyCompanyDAO extends CompanyDao {
#Inject
public MyCompanyDAO (Configuration configuration) {
super(configuration);
}
}
If for some reason you cannot inject the entire configuration (which I'd recommend), you could still inject the ConnectionProvider:
#Stateless
public class MyCompanyDAO extends CompanyDao {
#Inject
public MyCompanyDAO (MyConnectionProvider cp) {
super(DSL.using(cp, SQLDialect.POSTGRES));
}
}
This is a question about abstraction.
I want to be able to use two completely different GUIs for my application. They are completely different but implements the same interface.
My question is, what will the constructor look like? What type of object goes in the signature?
They do not extend a common parent, so I can't use polymorphism.
controller object wants to be injected with an object which implements Displayable interface.
interface Displayable {
void display();
}
class Display1 implements Displayable {
public void display() {
//Shows something Fancy on the screen
}
}
class Display2 implements Displayable {
public void display() {
//write something to console
}
}
class Main {
public static void main(String[] args) {
// Controller controller = new Controller(new Display1());
Controller controller = new Controller(new Display2());
controller.display();
}
}
class Controller {
????? display;
public Controller(?????? display) {
this.display = display;
}
public void display() {
display.display();
}
}
Android: i have a Handler class defined inside my activity and i get the warning "Handler class should be static or leaks might occur" with the following code:
Handler messageHandler = new Handler() {
// #Override
public void handleMessage(Message msg) {
try {
... accessing variables defined at the activity level
... doing something very important
}
super.handleMessage(msg)
}
}
However, the problem is that my message Handler has references to main activity variables, so i cannot make it static. How in my case can i get rid of that warning (in correct manner)?
Change
Handler messageHandler = new Handler() {
// #Override
public void handleMessage(Message msg) {
try {
... accessing variables defined at the activity level
... doing something very important
}
}
}
To
Handler mIncomingHandler = new Handler(new Handler.Callback() {
#Override
public boolean handleMessage(Message msg) {
}
});
Refercnce:
This Handler class should be static or leaks might occur: IncomingHandler