Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependency Injection: Diagnostic for dependency class with no @Inject and no-arg constructor #191

Closed
Tracked by #16
KidoVin01 opened this issue Oct 20, 2021 · 2 comments
Labels
diagnostic Diagnostic item project-info-needed Cannot be implemented until there is logic to get more information from the Java project

Comments

@KidoVin01
Copy link
Contributor

KidoVin01 commented Oct 20, 2021

Which Java classes are managed beans?

  • It has an appropriate constructor - either:
    • the class has a constructor with no parameters, or
    • the class declares a constructor annotated @Inject.

Injection using the bean constructor

When the container instantiates a managed bean with a constructor annotated @Inject, the container calls this constructor, passing an injectable reference to each parameter.

Diagnostics

  1. Identify @Inject field/method/constructor
  2. Check if dependency class has a constructor declared
  3. If constructor has no @Inject annotating, deliver two different diagnostics
    • at the class declaration: indicating no default no-arg constructor found
    • at the existing constructor: indicating constructor needs to be annotated with @Inject (implemented in [diagnostic] Declaring Managed Bean #108, however it is delivering an error rather than a warning.)

Note: The diagnostic should be a WARNING if there are alternative managed beans, else it should be an ERROR.

Quick Fix

Example

The following is a faulty code snippet example:

@ApplicationScoped
public class Greeting {
  
        // No explicit no-arg constructor        

	public Greeting (String name) {}	

	public String greet(String name) {
        return "GreetingA, " + name;
    }
}
@WebServlet(name = "diUnCon", urlPatterns = { "/diUnCon" })
public class GreetingServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
        
    @Inject
    private Greeting greeting;

    @Override
	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html;charset=UTF-8");
		res.getWriter().println(greeting.greet("Bob"));
	}
}

Note: The existing constructor cannot be annotated with @Inject because the parameters must be injectable as well according to the spec


Related to #153

@kathrynkodama
Copy link
Contributor

kathrynkodama commented Oct 21, 2021

Example link doesn't seem to work for me. Is this item for both a snippet and diagnostics? For the snippet, this might overlap with capabilities the Java language server offers. ie. I can generate a non-arg constructor with ctl-space:

public ServletDiagnosticsCollector() {
        // TODO Auto-generated constructor stub
    }

@KidoVin01
Copy link
Contributor Author

KidoVin01 commented Oct 21, 2021

Example link doesn't seem to work for me

Updated the example link.


For the snippet, this might overlap with capabilities the Java language server offers. ie. I can generate a non-arg constructor with ctl-space

Would the Jakarta LP be able to suggest those implemented capabilities in the diagnostics?

@kathrynkodama kathrynkodama added the diagnostic Diagnostic item label Oct 22, 2021
@KidoVin01 KidoVin01 changed the title Dependency Injection: Add diagnostics to suggest non-arg constructor for dependency class Dependency Injection: Diagnostic for dependency class with no @Inject and no-arg constructor Nov 5, 2021
@kathrynkodama kathrynkodama added the project-info-needed Cannot be implemented until there is logic to get more information from the Java project label Nov 19, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
diagnostic Diagnostic item project-info-needed Cannot be implemented until there is logic to get more information from the Java project
Projects
None yet
Development

No branches or pull requests

2 participants