You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@beehive.apache.org by Mitch Upton <mu...@bea.com> on 2005/08/19 22:19:36 UTC

Can method parameters be merged with annotations upon access at runtime?

 

All,

 

   I'm developing a control that could benefit from accepting values
from method parameters at runtime. In particular, I'd like to have a way
to specify an annotation attribute to take its runtime value from a
named method parameter. The DBMS control does something similar by
having parameter markers in the sql attribute like this 'select * from
TABLE where COLUMN1 = {param1} and COLUMN2 = {param2}'.

 

   I can see how this would work with String attributes, as you can just
parse them, find the markers, and then get the method parameter named in
the marker (via ControlBeanContext). However, has anyone thought of a
way to do this with non-String types? In this case, you can't come up
with some proprietary 'marker' syntax, and it has to be valid Java
syntax.

 

   At the very least I guess what you could do is to make everything a
String, and then always parse looking for parameter markers (even when
the annotation is logically a non-String value, like an int).

 

   Any help/ideas appreciated.

 

Thanks,

 

Mitch Upton


Re: Can method parameters be merged with annotations upon access at runtime?

Posted by Kyle Marvin <ky...@gmail.com>.
One last bit (since you asked about runtime).  Once the above
compile-time checks pass, you can then easily access the value of the
annotation at runtime using:

@ControlImplementation
public class MitchControlImpl {

    @ControlBeanContext context;

     // Extensible.invoke()
     public Object invoke(Method method, Object [] args)  {

          String paramName = 
                context.getMethodPropertySet(MitchAnnot.class).intParamName();
          int paramValue = (int)context.getParameterValue(meth,
paramName, args);
     }
}

On 8/19/05, Kyle Marvin <ky...@gmail.com> wrote:
> On 8/19/05, Mitch Upton <mu...@bea.com> wrote:
> >
> >   I can see how this would work with String attributes, as you can just
> > parse them, find the markers, and then get the method parameter named in
> > the marker (via ControlBeanContext). However, has anyone thought of a
> > way to do this with non-String types? In this case, you can't come up
> > with some proprietary 'marker' syntax, and it has to be valid Java
> > syntax.
> >
> 
> Hi Mitch,
> 
> If your question is whether you could define an annotation like this:
> 
> public @interface MitchAnnot {
>    int intParam();
> }
> 
> and then use it like this:
> 
> @MitchAnnot(intParam={param1})
> public mitchAnnotatedMethod(int param1)  { ... }
> 
> then I think the short answer is that this is not possible.
> 
> The problem here is that the Java compiler wants to do compile-time
> validation of @MitchAnnot usage against the declaration, including all
> of the annotation members.  Because this is a syntactic check, it will
> happen before any APT processor handling of MitchAnnot (which exists
> for semantic checks and/or codegen) and the above will fail to
> compile.
> 
> What you can do is something like this:
> 
> public @interface MitchAnnot {
>    String intParamName();
> }
> 
> and then:
> 
> @MitchAnnot(intParamName="param1")
> public mitchAnnotatedMethod(int param1)  { ... }
> 
> This satisfies the Java syntax for MitchAnnot and then an APT
> processor associated with MitchAnnot could do additional checking.
> For example, it could verify that the type of parameter referenced by
> the intParamName member ("param1") is actually an integer and generate
> appropriate errors if the types don't line up with the semantic
> contract.
> 
> Hope this helps!
> 
> -- Kyle
>

Re: Can method parameters be merged with annotations upon access at runtime?

Posted by Kyle Marvin <ky...@gmail.com>.
On 8/19/05, Mitch Upton <mu...@bea.com> wrote:
> 
>   I can see how this would work with String attributes, as you can just
> parse them, find the markers, and then get the method parameter named in
> the marker (via ControlBeanContext). However, has anyone thought of a
> way to do this with non-String types? In this case, you can't come up
> with some proprietary 'marker' syntax, and it has to be valid Java
> syntax.
> 

Hi Mitch,

If your question is whether you could define an annotation like this:

public @interface MitchAnnot {
    int intParam(); 
}

and then use it like this:

@MitchAnnot(intParam={param1})
public mitchAnnotatedMethod(int param1)  { ... }

then I think the short answer is that this is not possible.

The problem here is that the Java compiler wants to do compile-time
validation of @MitchAnnot usage against the declaration, including all
of the annotation members.  Because this is a syntactic check, it will
happen before any APT processor handling of MitchAnnot (which exists
for semantic checks and/or codegen) and the above will fail to
compile.

What you can do is something like this:

public @interface MitchAnnot {
    String intParamName(); 
}

and then:

@MitchAnnot(intParamName="param1")
public mitchAnnotatedMethod(int param1)  { ... }

This satisfies the Java syntax for MitchAnnot and then an APT
processor associated with MitchAnnot could do additional checking. 
For example, it could verify that the type of parameter referenced by
the intParamName member ("param1") is actually an integer and generate
appropriate errors if the types don't line up with the semantic
contract.

Hope this helps!

-- Kyle