Dukascopy
 
 
Wiki JStore Search Login

Attention! Read the forum rules carefully before posting a topic.

    Try to find an answer in Wiki before asking a question.
    Submit programming questions in this forum only.
    Off topics are strictly forbidden.

Any topics which do not satisfy these rules will be deleted.

How get File object from Configurable annotation?
 Post subject: How get File object from Configurable annotation? Post rating: 0   New post Posted: Thu 26 May, 2011, 16:34 

User rating: 0
Joined: Thu 19 May, 2011, 16:00
Posts: 37
Location: UkraineUkraine
Hello!

I want get File object annotated as Configurable. To do this I wrote this code:

        for(Field field : strategy.getClass().getFields()) {
            for(Annotation annotation : field.getAnnotations()) {
                if(annotation.annotationType().equals(Configurable.class)) {
                    Configurable conf = ((Configurable) annotation);
                    if(conf.value().equals(LOG_FILE_KEY)) {
                        context.getConsole().getOut().println(conf);
                    }
                }
            }
        }

LOG_FILE_KEY - constant name. If we found it in annotations, logging will be forward to this file. If not - just ignore such feature.
Problem is that Configurable not provide any methods to get access to File itself.


 
 Post subject: Re: How get File object from Configurable annotation? Post rating: 0   New post Posted: Fri 27 May, 2011, 08:44 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Unless you trying to retrieve a @Configurable annotation from another class file, you can use a similar approach to this:
@RequiresFullAccess
public class LoggingToFileStrategy implements IStrategy {
   
   @Configurable("LogFile")
   public File file = new File("C:\\temp\\log.txt");
   
   private IConsole console;
   
   @Override
   public void onStart(IContext context) throws JFException {
      this.console = context.getConsole();

      console.getOut().println("Start, filename: " + this.file.getAbsolutePath());
      String msg = "test message";
      writeToFile(msg);
   }
   
   private void writeToFile(String message) {
      try {
         FileWriter fstream = new FileWriter(this.file);
         fstream.write(message);
         fstream.close();
      } catch (Exception e) {
         console.getOut().println("Error: " + e.getMessage());
      }
   }


 
 Post subject: Re: How get File object from Configurable annotation? Post rating: 0   New post Posted: Fri 27 May, 2011, 13:37 

User rating: 0
Joined: Thu 19 May, 2011, 16:00
Posts: 37
Location: UkraineUkraine
I am trying to retrieve File object from another class. Doing this from IStrategy is simple:)


 
 Post subject: Re: How get File object from Configurable annotation? Post rating: 0   New post Posted: Tue 31 May, 2011, 11:08 
User avatar

User rating:
Joined: Fri 31 Aug, 2007, 09:17
Posts: 6139
Consider casting the field value:
public File getLogFile() {
   File file = new File("");
   IStrategy strategy = new EmptyStrategy();
   for (Field field : strategy.getClass().getFields()) {
      for (Annotation annotation : field.getAnnotations()) {
         if (annotation.annotationType().equals(Configurable.class)) {
            Configurable conf = ((Configurable) annotation);
            if (conf.value().equals(LOG_FILE_KEY)) {
               try {
                  return (File) field.get(strategy);
               } catch (Exception e) {
                  console.getOut().println(e.getMessage());
               }
            }
         }
      }
   }
   return file;
}


 

Jump to:  

  © 1998-2025 Dukascopy® Bank SA
On-line Currency forex trading with Swiss Forex Broker - ECN Forex Brokerage,
Managed Forex Accounts, introducing forex brokers, Currency Forex Data Feed and News
Currency Forex Trading Platform provided on-line by Dukascopy.com