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

Add Annotations to MethodExpression / MethodInfo #154

Closed
tandraschko opened this issue Apr 14, 2021 · 22 comments
Closed

Add Annotations to MethodExpression / MethodInfo #154

tandraschko opened this issue Apr 14, 2021 · 22 comments
Labels
Component: API Affects the API and (by implication) the spec and all implementations Jakarta EE 10 new feature

Comments

@tandraschko
Copy link

I'm currently working on a prototype for jakartaee/faces#1554

i wonder if we could add it, so we can easily access Anntotation[] from MethodExpression / MethodInfo?

@markt-asf
Copy link
Contributor

markt-asf commented Apr 14, 2021

getAnnotations() or getDeclaredAnnotations() ? I'm a little hesitant as we don't expose similar objects (e.g. Method) in the API. Could a similar AnnotationInfo object meet your needs?

@tandraschko
Copy link
Author

Why dont we need expose the Method itself? Would be much better as introduce AnnotationInfo.

Otherwise im OK with AnnotationInfo too.

@tandraschko
Copy link
Author

We may also allow to access annotations of Method params

@markt-asf markt-asf added Component: API Affects the API and (by implication) the spec and all implementations Jakarta EE 10 new feature labels Apr 14, 2021
@tandraschko
Copy link
Author

Any thoughts @markt-asf ?

@markt-asf
Copy link
Contributor

It looks to me like it was a conscious design decision not to expose Method instances directly. While I can't find any discussion about this in the archives, I suspect that was due to security concerns. I'm much happier erring on the side of caution and continuing that approach. Looking at how best to expose the annotation info is on my TODO list for this month.

@markt-asf
Copy link
Contributor

I've spent a bit of time looking at this and my current thinking is something along these lines added to MethodExpression:

public abstract Object getAnnotationElementValue(ELContext context, Class<? extends Annotation> type, String elementName);

I plan to continue to pursue a solution along these lines. My current thinking is to implement this in Tomcat to confirm an implementation is possible and, assuming that it is, then add the new abstract method to the API. It would be very helpful if you could confirm if this approach would meet your requirement.

@tandraschko
Copy link
Author

Do you think we should make such abstraction over reflection API?
Also this doesnt cover multiple same annotations

@markt-asf
Copy link
Contributor

The abstraction approach rather than providing raw access is consistent with how the EL API has approached similar requirements.

Handling multiple instances of the same annotation would need a slightly different approach. Still on MethodExpression something like:
public abstract AnnotationInfo[] getAnnotationInfo(ELContext context)
where

public class AnnotationInfo {
    Class getType();
    Map<String,Object> getElements();
}

That pushes some of the work onto the client. We could add a Predicate field to the getAnnotationInfo() to reduce the work the client needs to do whilst retaining the option to return info for all annotations.

I have the basics of this coded (but untested) in Tomcat so at this point I'm reasonably confident this is implementable.

@tandraschko
Copy link
Author

tandraschko commented Sep 7, 2021

+1 for AnnotationInfo

@arjantijms
Copy link
Contributor

arjantijms commented Sep 8, 2021 via email

@markt-asf
Copy link
Contributor

The EL implementations will have (or can easily get) the method reference but the only way for EL clients to get it is via reflection.

With the deprecation of security manager, the concept of trusted and untrusted code looks to be going away in the JRE but we haven't reached a conclusion yet about what that means for Jakarta EE. We could expose the Method object now but that assumes that the conclusion of the Jakarta EE discussion about removal of the security manager will reach the same answer as the JRE and I don't want to make that assumption just yet.

I much more comfortable continuing the existing approach of not directly exposing (or requiring permission for client code to use) classes in the java.lang.reflect package for EL 5.0 and revisiting those aspects of the entire EL API once we have a Jakarta EE decision on how to handle the deprecation of the security manager.

@arjantijms
Copy link
Contributor

arjantijms commented Sep 8, 2021 via email

@markt-asf
Copy link
Contributor

I've done a little digging and Annotation looks to be safe to expose directly. There is a comment in the Javadoc for Method.getDeclaredAnnotations() that the caller is free to modify the returned array. That simplifies things and provides what looks to be a reasonable compromise for now. Something like: Annotation[] MethodExpression.getAnnotations(ELContext)

I tend to agree with you that the answer for the security manager will be complete removal. I can think of a few replacements that could be implemented for Jakarta EE (still complex but not as complex as security manager) but I'm not sure how much demand there will be for them. For Tomcat, users using the security manager are a very small minority.

@arjantijms
Copy link
Contributor

arjantijms commented Sep 8, 2021 via email

@markt-asf
Copy link
Contributor

I'll expose getAnnotations() and getDeclaredAnnotations() as you can be sure if we only expose one, there will be users that want the other.

@markt-asf
Copy link
Contributor

Actually, cancel that. They return the same thing for methods.

@arjantijms
Copy link
Contributor

arjantijms commented Sep 8, 2021 via email

@markt-asf
Copy link
Contributor

Sounds like a plan. I'll get started. Should have something shortly.

@markt-asf
Copy link
Contributor

A few questions:

  • base for literal method expressions? null?
  • actualParameters for literal method expressions Object[0]?

@markt-asf
Copy link
Contributor

Implementation committed. I have an implementation ready in Tomcat 10.1.x as well if folks want to experiment with it.

@tandraschko
Copy link
Author

Thanks, the code looks good!
I will give it a try in MyFaces next week

@arjantijms
Copy link
Contributor

Thanks @markt-asf for other people looking at this issue, the code was added in this commit:

apache/tomcat@fda4faa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: API Affects the API and (by implication) the spec and all implementations Jakarta EE 10 new feature
Projects
None yet
Development

No branches or pull requests

3 participants