1

maybe it's very silly question. Please understand and forgive. I'm trying to find a way to implement annotation that can change method paramter.

Please see the code:

/**
* This method does some scary things in OS file system.
* @param anyFileSystemPath is a file system path :)
*/
public File makeSomeFileSystemStuff(String anyFileSystemPath){
//some code goes here...
}

I would like to create an annotation:

public File makeSomeFileSystemStuff(@IsValid String anyFileSystemPath){
//some code goes here...
}

Where @IsValid annotation validates path (is should be at least with read access and should exist of course). Also I would like to be sure that each path ends with "/". So This annotation should append "/" to paramter value if "/" is not present at the end of line.

How can I do that?

I've tries to goolge like: java annotation change method paramter value, get method paramter value, e.t.c. but didn't find something useful for me :( Please advice.

Capacytron
  • 3,425
  • 6
  • 47
  • 80
  • Why not perform the validation inside the method? Only do something, if it's valid. instead of trying to mess with the method declaration itself. which I have no idea if it's even possible – La bla bla Aug 11 '12 at 18:35
  • 3
    Annotations don't do anything at all by themselves. There shall be an annotation processor and in your case there shall be an invocation interceptor aspect. Are you using an AOP framework such as Spring or AspectJ? If it is not your case the best solution is to validate your parameter inside your method. – Amir Pashazadeh Aug 11 '12 at 18:41
  • @AmirPashazadeh, as I doubt that there will be any better answer than your comment, perhaps you should turn that comment into an answer. Perhaps throw in a few links if you like. – MvG Aug 11 '12 at 20:55
  • @AmirPashazadeh, I've read some review articale about AspectJ. I thougth that it's a way to solve my problem. Can you suggest me some articles with cases which can be close to my problem? I understand that annotations doesn't do something, it's just a declarative approach. I want to implement validation once and declare validation rules for *arguments* I want to validate. It's a complex problem. – Capacytron Aug 11 '12 at 21:22
  • Here is solution. Works fine :) http://stackoverflow.com/questions/4312224/aspectj-overwrite-an-argument-of-a-method – Capacytron Aug 22 '12 at 09:35
  • Please see my comment. http://stackoverflow.com/questions/4312224/aspectj-overwrite-an-argument-of-a-method It worked for me. – Capacytron Nov 12 '12 at 09:35

0 Answers0