|
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. |
|
Thread Tools | Search this Thread | Display Modes |
#1
|
||||
|
||||
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) {} Code:
result = package_class_SomeMethod(true, 5, "Str") Code:
result = package_class_SomeMethod(GetProperty(), GetProperty(), GetProperty()) 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) 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. |
#2
|
||||
|
||||
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()) Code:
SomeMethod(Object myarg) { if (myarg isntanceof Integer) { } else if (myarg instanceof Boolean) { } else { } }
__________________
Batch Metadata Tools (User Guides) - SageTV App (Android) - SageTV Plex Channel - My Other Android Apps - sagex-api wrappers - Google+ - Phoenix Renamer Downloads SageTV V9 | Android MiniClient |
#3
|
|||
|
|||
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));} Last edited by PLUCKYHD; 06-02-2010 at 05:58 AM. |
#4
|
||||
|
||||
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. |
#5
|
||||
|
||||
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. |
#6
|
|||
|
|||
Quote:
java_lang_Boolean_parseBoolean("True") |
#7
|
||||
|
||||
I tried:
Code:
var = GetProperty() ... res = MyJava(java_lang_Boolean_parseBolean(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. |
#8
|
||||
|
||||
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")))
__________________
-- Greg |
#9
|
|||
|
|||
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.
|
#10
|
||||
|
||||
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 |
#11
|
||||
|
||||
Quote:
And you meant typo, not type.
__________________
-- Greg |
#12
|
||||
|
||||
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 |
#13
|
||||
|
||||
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. |
#14
|
||||
|
||||
Quote:
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 } 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.
__________________
Batch Metadata Tools (User Guides) - SageTV App (Android) - SageTV Plex Channel - My Other Android Apps - sagex-api wrappers - Google+ - Phoenix Renamer Downloads SageTV V9 | Android MiniClient |
#15
|
||||
|
||||
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. |
#16
|
||||
|
||||
Quote:
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....
__________________
Batch Metadata Tools (User Guides) - SageTV App (Android) - SageTV Plex Channel - My Other Android Apps - sagex-api wrappers - Google+ - Phoenix Renamer Downloads SageTV V9 | Android MiniClient |
#17
|
|||
|
|||
Quote:
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));} 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); } 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. |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|
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 |