SageTV Community  

Go Back   SageTV Community > SageTV Development and Customizations > SageTV Studio
Forum Rules FAQs Community Downloads Today's Posts Search

Notices

SageTV Studio Discussion related to the SageTV Studio application produced by SageTV. Questions, issues, problems, suggestions, etc. relating to the Studio software application should be posted here.

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-02-2010, 05:28 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Passing Parameters from the STV to Java

I've recently been grappling with passing parameters of various types to some custom java code and was wondering how others deal with the issue.

Suppose I have written a method that looks like this:

Code:
public static boolean SomeMethod(boolean b, int i, String s) {}
From the STV I can invoke the method like this:

Code:
result = package_class_SomeMethod(true, 5, "Str")
But if I do this it will not work because GetProperty returns strings (I think - correct me if I'm wrong):

Code:
result = package_class_SomeMethod(GetProperty(), GetProperty(), GetProperty())
I realize that if I have a method that has just a few parameters I can overload it to deal with various types but what if I have a method that takes many parameters?

How do you deal with the fact that the parameters can be various types? Or am I missing something more basic? Is there a convention that I can use to anticipate what type the STV will pass to the java code?

If I have this in the STV:

Code:
Var = 5
result = SomeMethod(Var)
will the STV pass an int, Integer or String?

what about:

Code:
Var = true
resullt = SomeMethod(Var)
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #2  
Old 06-02-2010, 05:45 AM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
Tom, the best thing to do in your case is coerce your GetProperty resuts into the type that you want.

Code:
SomMethod(java_lang_Boolean_parseBoolean(GetProperty()), java_lang_Integer_parseInt(GetProperty()), GetProperty())
there are cases in which you want your method to accept just an Object and then you can do type checking within your code, but where possible I'd use strongly typed methods.

Code:
SomeMethod(Object myarg) {
    if (myarg isntanceof Integer) {
    } else if (myarg instanceof Boolean) {
    } else {
    }
}
But, personally, I'd stay away from that kind of code, if at all possible.
Reply With Quote
  #3  
Old 06-02-2010, 05:56 AM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
I would agree with Sean to stay away from that because if you pass an object you don't check for things can get hairy a bit.

I would use overrides.
Code:
 public static boolean GetProperty(String Name,boolean Default){
 String Value = java.lang.String.valueOf(Default);
 String SValue = currprop.GetProperty(Name,Value);
 return java.lang.Boolean.parseBoolean(SValue);}

public static void SetProperty(String Name,boolean Value){
  currprop.setProperty(Name, java.lang.String.valueOf(Value));}
Then you can set up ones for integers,doubles,arrays whatever you want to store and get from a property and you are not limited to strings. Also it keeps you stv coding much simplier and only requires on code in java and not several parsing within studio.

Last edited by PLUCKYHD; 06-02-2010 at 05:58 AM.
Reply With Quote
  #4  
Old 06-02-2010, 06:27 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Got it, thanks.

The steep learning curve continues....
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #5  
Old 06-02-2010, 04:32 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Got it working. First problem was that I did not declare the method as static.

Next I used parseInt() in studio to convert the String to an integer.

I could not get parseBoolean() to work so I gave up and changed the method to accept a String and converted the String to Boolean in the method. Not ideal so I'll put this on the "things to fix" list. (ArrayList<ThingsToFix> thingsToFix = new ArrayList<ThingsToFix>()
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #6  
Old 06-02-2010, 06:17 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Quote:
Originally Posted by tmiranda View Post
Got it working. First problem was that I did not declare the method as static.

Next I used parseInt() in studio to convert the String to an integer.

I could not get parseBoolean() to work so I gave up and changed the method to accept a String and converted the String to Boolean in the method. Not ideal so I'll put this on the "things to fix" list. (ArrayList<ThingsToFix> thingsToFix = new ArrayList<ThingsToFix>()
Boolean.parseBoolean("True") or Boolean.parseBoolean("False") should work I use it allot inside java but dont use it inside sage I believe inside sage it would be

java_lang_Boolean_parseBoolean("True")
Reply With Quote
  #7  
Old 06-03-2010, 02:02 PM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
I tried:

Code:
var = GetProperty()
...
res = MyJava(java_lang_Boolean_parseBolean(var))
but that generates an unknown method error in studio. (The method expects a boolean.)
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #8  
Old 06-03-2010, 02:35 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
It's going to be hard for us to tell you exactly what you're doing wrong without seeing your actual code. However I can say that the following code works verbatim in Expression Evaluator:

Code:
java_lang_Boolean_toString(java_lang_Boolean_parseBoolean(GetProperty("someproperty", "true")))
So try replacing parts of that with the corresponding parts from your code to try to pinpoint where yours is going wrong.
__________________
-- Greg
Reply With Quote
  #9  
Old 06-03-2010, 02:58 PM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
I still maintain it would be just easier for you to make overrides in java for the different types you need. Then no conversion is needed within studio. You just have to define a get and set method to parse each type you want to store in a properties file.
Reply With Quote
  #10  
Old 06-03-2010, 04:22 PM
Fuzzy's Avatar
Fuzzy Fuzzy is offline
SageTVaholic
 
Join Date: Sep 2005
Location: Jurupa Valley, CA
Posts: 9,957
Quote:
Originally Posted by tmiranda View Post
I tried:

Code:
var = GetProperty()
...
res = MyJava(java_lang_Boolean_parseBolean(var))
but that generates an unknown method error in studio. (The method expects a boolean.)
If that was the actual code you were trying, the problem is a type... java_lang_Boolean_parseBoolean(var)
__________________
Buy Fuzzy a beer! (Fuzzy likes beer)

unRAID Server: i7-6700, 32GB RAM, Dual 128GB SSD cache and 13TB pool, with SageTVv9, openDCT, Logitech Media Server and Plex Media Server each in Dockers.
Sources: HRHR Prime with Charter CableCard. HDHR-US for OTA.
Primary Client: HD-300 through XBoxOne in Living Room, Samsung HLT-6189S
Other Clients: Mi Box in Master Bedroom, HD-200 in kids room
Reply With Quote
  #11  
Old 06-03-2010, 04:48 PM
GKusnick's Avatar
GKusnick GKusnick is offline
SageTVaholic
 
Join Date: Dec 2005
Posts: 5,083
Quote:
Originally Posted by Fuzzy View Post
If that was the actual code you were trying, the problem is a type... java_lang_Boolean_parseBoolean(var)
Well, that and the fact that he passed no arguments to GetProperty, and that MyJava is not a valid method name. That's why we need to see the real code before nitpicking it at that level.

And you meant typo, not type.
__________________
-- Greg
Reply With Quote
  #12  
Old 06-03-2010, 06:15 PM
Fuzzy's Avatar
Fuzzy Fuzzy is offline
SageTVaholic
 
Join Date: Sep 2005
Location: Jurupa Valley, CA
Posts: 9,957
I'm actually kinda brimming with pride that i mistyped typo... That level of fail is something to be marveled, in my opinion...
__________________
Buy Fuzzy a beer! (Fuzzy likes beer)

unRAID Server: i7-6700, 32GB RAM, Dual 128GB SSD cache and 13TB pool, with SageTVv9, openDCT, Logitech Media Server and Plex Media Server each in Dockers.
Sources: HRHR Prime with Charter CableCard. HDHR-US for OTA.
Primary Client: HD-300 through XBoxOne in Living Room, Samsung HLT-6189S
Other Clients: Mi Box in Master Bedroom, HD-200 in kids room
Reply With Quote
  #13  
Old 06-04-2010, 08:06 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
The code is very straight forward.

Studio:

DefaultSetting = false
BoolVal = GetProperty("propname", DefaultSetting)
Result = MyJavaMethod(java_lang_Boolean_parseBoolean(BoolVal))


Java:

static public boolean MyJavaFunction(boolean val) {
}



This is a bit of a simplification since the actual Java method takes more parameters of differing types. I narrowed it down to the boolean value as the culprit causing the problem.

I'll try to post the actual code over the weekend. I'm probably doing something wrong without knowing it.
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #14  
Old 06-04-2010, 08:43 AM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
Quote:
Originally Posted by tmiranda View Post
The code is very straight forward.

Studio:

DefaultSetting = false
BoolVal = GetProperty("propname", DefaultSetting)
Result = MyJavaMethod(java_lang_Boolean_parseBoolean(BoolVal))


Java:

static public boolean MyJavaFunction(boolean val) {
}



This is a bit of a simplification since the actual Java method takes more parameters of differing types. I narrowed it down to the boolean value as the culprit causing the problem.

I'll try to post the actual code over the weekend. I'm probably doing something wrong without knowing it.
Tom, the problem with you simply typing what you did in studio is that it can lead to questions that are not relevant. For example, I'm not sure if DefaultSetting=false is valid, if if it should be DefaultSetting="false".

Also, if your java method has other args, then those other args may be reason for your method not found exception, and it may have nothing to do with the parseBoolean stuff.

Screenshots and Code fragments will always be better than, "here's what I think I have"

keep in mind that a No Such Method error could mean that you've mistyped the method name... or it could mean that the argument types that you are passing do not match those for the method name that was found.

Sometimes, to trouble shoot this, you can change your method to some debug method like,

Code:
MyMethod(Object o1, Object o2, Object o3) {
    if (o1!=null) {
       System.out.println("O1: " + o1.getClass().netName());
    }
    // repeat for o2 and o3
}
This since this method takes all "Object" args, it should be found, and it will print out the "types" that you are sending from studio to the java code.

Next, make sure you don't have methods like,
MyMethod(String s1, String s2)
MyMethod(String s1, Boolean s2)

In java, those are fine, and the compiler will tell you if you pass null in the second arg, then it's an error. But from studio, if the second arg was null, then there would be no way to tell which method to invoke, and it would likely result in a No Such Method error.
Reply With Quote
  #15  
Old 06-04-2010, 10:22 AM
tmiranda's Avatar
tmiranda tmiranda is offline
SageTVaholic
 
Join Date: Jul 2005
Location: Central Florida, USA
Posts: 5,851
Sean,

I'm at work and can't post the code. When I get home I will do that.

I do know enough to check the spelling

I like the idea of using getClass(), that will tell me for sure what type I'm getting.

And so you don't think I am a total mush-brain, I did figure out how to serialize obejcts, write them to disk, read them back and de-serialize them. I'm actually making good progress considering that I am coming from ZERO Java and OO knowledge.

I am working on several classes that do some pretty nifty things (well, at least I think they are nifty):

class Podcast - represents podcasts on the web. Some methods:
- getEpisodeList()
- addAsFavorite()
- hasUnrecordedEpisodes()
- removeAsFavorite()

class Episode - represents episodes for podcasts. Some methods:
- download()
- delete()
- getMediaFile()
- isDownloaded()

Each Podcast can have zero or more Episodes.

There are lots more methods but I'm at work at the moment so can't list them all. The idea is to make all of the classes and methods necessary to define favorite podcasts, download them, manage them in the Sage database and display them in Studio. I'm learning by leaps and bounds and, as expected, am running into lots of little "gotchas" that experienced Java programmers know about All in all it's a lot of fun.
__________________

Sage Server: 8th gen Intel based system w/32GB RAM running Ubuntu Linux, HDHomeRun Prime with cable card for recording. Runs headless. Accessed via RD when necessary. Four HD-300 Extenders.
Reply With Quote
  #16  
Old 06-04-2010, 10:48 AM
stuckless's Avatar
stuckless stuckless is offline
SageTVaholic
 
Join Date: Oct 2007
Location: London, Ontario, Canada
Posts: 9,713
Quote:
Originally Posted by tmiranda View Post
Sean,

I'm at work and can't post the code. When I get home I will do that.

I do know enough to check the spelling

I like the idea of using getClass(), that will tell me for sure what type I'm getting.

And so you don't think I am a total mush-brain, I did figure out how to serialize obejcts, write them to disk, read them back and de-serialize them. I'm actually making good progress considering that I am coming from ZERO Java and OO knowledge.

I am working on several classes that do some pretty nifty things (well, at least I think they are nifty):

class Podcast - represents podcasts on the web. Some methods:
- getEpisodeList()
- addAsFavorite()
- hasUnrecordedEpisodes()
- removeAsFavorite()

class Episode - represents episodes for podcasts. Some methods:
- download()
- delete()
- getMediaFile()
- isDownloaded()

Each Podcast can have zero or more Episodes.

There are lots more methods but I'm at work at the moment so can't list them all. The idea is to make all of the classes and methods necessary to define favorite podcasts, download them, manage them in the Sage database and display them in Studio. I'm learning by leaps and bounds and, as expected, am running into lots of little "gotchas" that experienced Java programmers know about All in all it's a lot of fun.
That all sounds very good, Tom. I think the fact that you have programming experience is good... otherwise learning java would be even that much more painful.

I'm currently teaching my 11 year son how to program in Java. It's been fun yet painful teaching him about 'data types' 'variables' 'conditions' etc....
Reply With Quote
  #17  
Old 06-04-2010, 11:00 AM
PLUCKYHD PLUCKYHD is offline
SageTVaholic
 
Join Date: Dec 2007
Posts: 6,257
Quote:
Originally Posted by stuckless View Post
That all sounds very good, Tom. I think the fact that you have programming experience is good... otherwise learning java would be even that much more painful.

I'm currently teaching my 11 year son how to program in Java. It's been fun yet painful teaching him about 'data types' 'variables' 'conditions' etc....
If I lived close I would love to listen in on those lessons

Back to subject.

Still not sure you need to use .getclass() to determine the class as the overrides will do this for you as long as one exist for what you are passing. (if it doesn't make it .

basically like this (if you understand this and I am being repetitive please let me know just trying to suggest easiest way I think to do it)

Code:
\\ set property as string 
public static void SetProperty(String Name,String Value){
currprop.setProperty(Name, Value);}

\\set property as Long
public static void SetProperty(String Name,Long Value){
CurrProp.setProperty(Name, Value.toString());}

\\set property as Boolean
public static void SetProperty(String Name,boolean Value){
CurrProp.setProperty(Name, java.lang.String.valueOf(Value));}

\\Set property as Int with a max and min I do this so I can predetermine a max or min value for a certain property but the min and max is not necessary
 public static void SetProperty(String Name,int Value,int min,int max){
    if (Value>max){
    Value = max;}
    if(Value<min){
    Value=min;}
CuuProper.setProperty(Name,java.lang.String.valueOf(Value));}
Basically when setting the properties you always got to convert to a string then when getting the property you convert back to the form you are wanting.
Code:
\\convert back from string to integer.
    public static int GetProperty(String Name,int Default){
        String Value = java.lang.String.valueOf(Default);
        String SValue = GetProperty(Name,Value);
     
        return java.lang.Integer.parseInt(SValue);
     }

\\convert back from string to double
     public static double GetProperty(String Name,double Default){
        String Value = java.lang.String.valueOf(Default);
        String SValue = GetProperty(Name,Value);
        return java.lang.Double.parseDouble(SValue);
     }
\\convert back from string to Long

     public static Long GetProperty(String Name,Long Default){
        String Value = java.lang.String.valueOf(Default);
        String SValue = GetProperty(Name,Value);
        return java.lang.Long.parseLong(SValue);
     }
\\Convert back from string to Boolean

     public static boolean GetProperty(String Name,boolean Default){
      String Value = java.lang.String.valueOf(Default);
      String SValue = GetProperty(Name,Value);

      return java.lang.Boolean.parseBoolean(SValue);

     }
So in studio you would just call
package_class_GetProperty("PropName",true)
to set as a boolean and the same call
package_class_GetProperty("PropName",1)
would set it as a integer

the important part comes in in the get method as the default value will determine how it is parsed and returned to you as the object time in the value.

hope that helps and I just typed that code really quick so apologies for errors.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
STV Import: Java Heap Monitor tmiranda SageTV Customizations 26 02-25-2018 05:56 AM
How Does STV Determine Java Installation? jtors SageTV Software 9 11-13-2009 05:03 PM
java contructor parameters jphipps SageTV Studio 5 06-09-2009 04:02 AM
Java.Lang error after changing STV SteveP SageTV Customizations 2 01-27-2006 03:05 PM
Java Question for STV Developers mightyt SageTV Customizations 10 09-29-2004 07:35 AM


All times are GMT -6. The time now is 06:08 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2023, vBulletin Solutions Inc.
Copyright 2003-2005 SageTV, LLC. All rights reserved.