How do I manage dynamic narrowing casts without instanceof?

Hi all,
I need to extract arbitrary Objects from a given Collection and cast them to their original types. The problem is: checking with instanceof won't work for me, because the number of possible types is practically unlimited. Can anybody help?
Thanks in advance,
Wolfgang

I don't think you have to do the cast in the set method. The following code works for me:
[Main is a simple class with attributes <String a>, <int b> and <Main c>, and has getters for them]
Note that the code is not nice, it's just a quick-n-dirty test case I wrote in a few minutes.
Note also that I use Objects in the set method, rather then a String, an Integer and a Main.
      Class mainClass = Class.forName("dynamicinstantiation.Main");
      Constructor ctr = mainClass.getConstructor(new Class[0]);
      // object of class main, not known to the client
      Object main = ctr.newInstance(new Object[0]);
      // fieldvalues to set
      Object a = "String";
      Object b = new Integer(100);
      Object c = new Main();
      // get fields
      Field fldA = main.getClass().getDeclaredField("a");
      Field fldB = main.getClass().getDeclaredField("b");
      Field fldC = main.getClass().getDeclaredField("c");
      fldA.set(main, a);
      fldB.set(main, b);
      fldC.set(main, c);
      // Cast main to Main class, so we;re able to access it's methods
      Main m = (Main)main;
      System.out.println(m.getA());
      System.out.println(m.getB());
      System.out.println(m.getC().getA());
      System.out.println(m.getC().getB());

Similar Messages

  • How can I manage six email accounts WITHOUT them all appearing (twice) in the Folders column?

    How can I manage six email accounts WITHOUT them all appearing (twice) in the Folders column?
    All I need is ONE INBOX (like Windows Mail used to do) ...
    I can see in the 'Account' column of each message which email account it's using.
    Then, I either junk or read the message, followed by delete or file to one of my created folders.
    As it is, half my page is depth is taken up by 12 lines of Account names !!!

    That still leaves SIX lines of Account names down the side. All I want is ONE INBOX ... (I can see which account each message belongs to in the 'Account' column...)

  • How to revert managed client to unmanaged without server

    The scenario is that a group of 10.6 client macs were managed by an OS X server, and that server no longer exists. How can the clients be reverted back to an unmanaged state without using a server.
    I've tried manually erasing the '/Library/Managed Preferences' and '/Library/Preferences/DirectoryService' contents but they are restored from somewhere, I assume some hidden system cache (because I can't find copies of these files anywhere, and a hidden locker makes sense). I've also tried changing some of these preferences using the 'defaults' command, hoping that'd propagate back to the cache, but they still get restored. I've also cleaned out the global com.apple.loginwindow MCX preferences, to no avail.
    I have perused the local directory with 'dscl' and found nothing obvious, but it wouldn't surprise me if there was binary data in there supporting the automatic recovery.
    From observation, I'd guess there might be a process run from 'launchd' periodically that restores the values, because I've seen them come back while I was working at the command line.
    There are only a few clients so it's not a problem if I have to do it manually or even in single-user mode, but I'd really, really like to avoid having to wipe and reinstall because there's a great deal of user state on them. Everything's backed up with good TM backups going back a long way, but I'm afraid of restoring the server binding if I have to reinstall and recover, and I'd really like to avoid that hassle, too.
    I'm a very seasoned Unix geek and have no fear of working in system directories from the command line as root, but so far I haven't been able to find whatever it takes.
    So, who knows how to actually do this?
    Thanks.

    Antonio Rocco wrote:
    Hi
    sudo rm -R -v /Library/Preferences/DirectoryService
    sudo rm -R -v /Library/Preferences/edu.mit.kerberos
    sudo rm -R -v /Library/Managed\ Preferences
    sudo rm -R -v /Library/Caches/*
    Restart the mac.
    This is the approach I have used as well. However if the client Mac is on the same network as a server managing that client then the server will re-apply the managed settings after a reboot.
    I know the original poster said the server was no longer present, but is there another server that might be applying settings? Perhaps an Open Directory replica?
    It would not apply in this case because the original poster said the clients are running Mac OS X 10.6, but much older versions of Mac OS X client also kept a copy of managed preferences in NetInfo and you needed to use NetInfo Manager to delete them. Mac OS X 10.6 does not use NetInfo.

  • How do I manage video and music without a mac?

    With iOS5 and iCloud solving almost all of my problems, I decided to replace all my computing needs for an iPad 2. I will keep my iMac solely to manage my music and video needs, since I have a huge library in an external drive. So I started wondering: is it possible to access the media on my hard drive from the iPad without using a computer of any kind? Would the USB adapter allow this? Or maybe a networked drive?

    You might try putting your harddrive on your wifi and see if there's some sort of file management app that would allow you to access it.
    Some people have reported limited success accessing a powered harddrive via the camera kit (which has an SD and USB dongle you can put into your iPad)
    that said, accessing it and getting it into iTunes might be two separate battles. (to the music folder and video folders to be able to play it)
    Perhaps if you put iTunes on the harddrive and the music/videos in iTunes then try to access it via home sharing????
    Sorry I don't have a direct answer, I'm just trying to brain storm.

  • How do I Manage media for QTSS without QTSS Publisher?

    Mac OS X Server Server Administration For Version 10.5 Leopard
    p. 49 says:
    “Media Streaming Management
    QuickTime Streaming and Broadcasting Administration provides instructions for
    administering QuickTime Streaming Server (QTSS) using Server Admin.
    QuickTime Streaming and Broadcasting Administration also describes QTSS Publisher, an
    easy-to-use application for managing media and preparing it for streaming or
    progressive download.”
    But the manual QuickTime Streaming and Broadcasting Administration For Version 10.5 Leopard
    Has no reference whatsoever to “QTSS Publisher” or “Publish”
    And the Migrating from Mac OS X Server v10.3 manual
    Says on p. 69:
    QTSS Publisher Files and Folders
    QTSS Publisher has been removed from Mac OS X Server v10.5. However, files created
    using QTSS Publisher on v10.4 should continue to work on v10.5.
    What gives?
    How do I migrate and then upload and manage my QTSS files now? The manuals are vague!

    Hi Tom,
    I did this last night (literally). Here is what you need to do. I am working under the premise that your current server is a Tiger server. If this is not the case, then you may have to adapt the Tiger server locations I am providing to your version of server.
    If you have not tweaked Apple's configuration in any way, your current streaming media files are located in /Library/QuickTimeStreaming/Movies on the Tiger server. You will need to copy these files to the same location on the Leopard server. Next, you will need to set the permissions and ownership of the files you just copied. If you execute the +ls -l+ command from /Library/QuickTimeStreaming/Movies, you will find that the permissions for the stock sample files are -rw-rw-r--, and that the owner:group is +_qtss admin+. So to change your files to match these parameters, execute (as sudo) the chmod command +chmod 644 <filename>+ as well as the chown command +chown _qtss:admin <filename>+. Now when you execute +ls -l+, you should find that the owner, group, and permissions for all the files are the same.
    Once that is done, the rest is fairly straightforward per Apple's documentation. One thing I will say, as this burned me last night, is that if you change IPs, and you are also using the Web server component, things will get a bit funky. The files themselves will be fine, but QTSS will become unbound from whatever IP it was set to originally, and Web server will prevent it from starting. You'll have to re-bind QTSS to the appropriate IP (if your server is multihomed), re-enable streaming on port 80 if necessary, start QTSS, then start the Web server.
    Good luck! Hopefully through my suffering you will suffer less.

  • How to get images dynamically from database without file paths in the table field

    I have a MS-Access database. I am working with ASP.NET. In the database there is product table in which I have "CodeNo" as a field which is a text field, and the product codes like "SM-R-2035". I also have another field "Image" which is also a text field and which have a file path in it corresponding to the particular product Image (e.g. Images\Products\SM-R-2035.jpg). So far every thing is ok. I have to update this site very frequently and lots of images are added each and every time. Its a tedious work to type the paths and file name every time and it also take a lot of time.
    What I am asking is : Is it possible to get images from a specific folder at runtime which is referenced by the "code no" itself and not the file path from the database. (Say at run time the "code no" is referenced from the database and the corresponding image is loaded dynamically from the specified folder). In other word I want to avoid the tedious work of typing.
    Can any one help with this issue. Any other simple suggestions are welcome.

    All you need to do is simple concatenation to obtain the path for the image file.  You didn't mention whether you are using VB.Net, C# or some other language to do your coding.
    If the code in your database is SM-R-2035, the file name is SM-R-2035.jpg and the path to the images foilder is Images\Products\SM-R-2035.jpg, Conceptually here is what you need to do:
    dim code_var
    dim path_var
    code_var = the code you obtain from your relevant field in the database
    path_var = "Images\Products\" & code_var & ".jpg"
    Now path_var is what you would call to obtain the image from your images folder.

  • Dynamic class cast and instanceof

    I am looking for a way to dynamically test an object for being of a specific class. The instanceof operator can only be used with literally named classes I understand. So you cannot have something like boolean checkClass( Object o, Class c ) {
       return o instanceof c;
    } Is there any way to do something like this by use of the reflection API? The point is that instanceof only works statically, the reference type being tested for being specified at compile time. The question is whether it can be done with a reference type being specified at run-time, ie dynamically.

    There is a collection of parameters of various types, which is passed to some client module.
    There should be a, say 'checkParam' method for the client, which checks for existence of a certain parameter by its name, and also specified which class the parameter should have. So it should be called like
    checkParam( params, "paramName", String.class )I wanted to avoid hard-coded types, but have the class specified as an argument to this method. I feel like there should be some way to accomplish that. What swingfreak intended may be a way.

  • Creating Dynamic Cascading Prompts Without Business View manager?

    How can we create dynamic cascading prompts without Business View Manager??

    Creae a parameter in Crystal Report Designer. I suggest you read the help on how to also.

  • How do i manage multiple users and devices with one apple id without everything showing up on every device?

    how do i manage multiple users and devices with one apple id without everything showing up on every device?

    How to use multiple iPhone, iPad, or iPod devices with one computer

  • How can I manage my music without erasing all of my songs?

    For reference I've had iPods since about 2005 and I've been through this many times. My harddrive in my computer recently crashed and I had to replace it. Now all of my songs are gone (other than the ones I got on iTunes obviously). About half of my music (near 500 songs) are off of old CD's that I have since thrown away since I figured I had them saved on my iPhone and my laptop. Reference again: I had this happen when I had an iPod touch also with another computer. With my iPod touch, I was free to manage my music manually without erasing all my songs that were on my iPod. Now why can't I seem to do this now with my iPhone. Please help! I can't loose 500 songs! That's $500 that I DON'T HAVE to spend on music that I've already bought! P.S. I just need to know how to manually manage my music without erasing all my music!

    is this the one you're looking for?
    Managing content manually on iPhone, iPad, and iPod

  • I have my ipod shuffle plugged into a diffrent computer associated with it for ittunes, but i cant figure out how to maually manage my music without erasing all the songs already on my ipod shuffle... so how do i do that?

    I have my ipod shuffle plugged into a diffrent computer associated with it for ittunes, but i cant figure out how to maually manage my music without erasing all the songs already on my ipod shuffle... so how do i do that? I've done it before with my old ipod before it broke. I really need help with this!

    I have my ipod shuffle plugged into a diffrent computer associated with it for ittunes, but i cant figure out how to maually manage my music without erasing all the songs already on my ipod shuffle... so how do i do that? I've done it before with my old ipod before it broke. I really need help with this!

  • How do I manage open files in Server 2012 without resorting to 3rd party application?

    How do I manage open files in Server 2012 without resorting to 3rd party application?
    I cannot find the Mange open files in FSRM.
    Thanks

    Wouldn't it make sense to place it in the server manager / shares section?
    I mean, second to creating or finding a share, isn't this what most users would be after?
    For like 20 years we've been trained to right click to find stuff Microsoft is hiding, and right click don't do nothin' here.  You gotta go hunting under tools.   Or, forget the new half-baked interface with half of what you need and go old school,
    direct to Start - Computer..Right Click.. whoops... that doesn't exist anymore.....
    Drool.........drool.............Find that hiding, blinking start button in a RDP session, click start after three trys,  and TYPE IN "Computer Management" .  Right Click, Pin it to your desktop, pin it to the start screen, pin it to the taskbar, send
    it to OneNote, and upload it to skydrive  so you can find it again, then get back to what you were doing....to find the cottin' pickin open files and kick your users off so you can have your way with the server.

  • How do i manually manage my new iphone without erasing everything?

    OK, so i got a new iphone 5 for christmas. i already have an ipod touch so through icloud i restored everything to my new phone. then i went on to my computer to add my cds since you cant do that through icloud, and when i tried i couldnt. then when i tried to set it to manual manage it said you can only sync to one computer at a time, and gave me two options: erase and sync or cancel. i understand this happening if it was a new computer and i previously synced my phone  to another computer, but for me thats not the case, this is the first computer ive used with my new phone. if syncing were to erase only my pictures, songs and videos i wouldnt mind just deleting everything but it will delete all my apps and app info, and there are things i need. is there anyway i can manually manage my new phone without erasing everything????

    You don't manually manage with iCloud, you manually manage with iTunes on your computer. So, if you want to manually manage, get all of the content in your iTunes library on your computer first.
    You can re-download iTunes purchased content:
    http://support.apple.com/kb/HT2519

  • How do people manage QoS Policies in large network without using QPM

    We are using QPM to manage QoS polices however we are looking at decommissioning CiscoWorks. How are people managing with their QoS settings in large environments?

    I have no idea about the modem and bridge mode (I don't do networking -- hopefully Bob Timmons, Tesserax, or one of the other networking gurus will drop in and address that).
    But . . . you should be able to back up to the TC as long as it's on your network and recognized by your Macs.  I think being in bridge mode means it will be rather slow, but it should work.  Until/unless we hear otherwise, you might want to see #Q1 in Using Time Machine with a Time Capsule.

  • How to get the Dynamic UI component value from JSFF page to any managedbean

    HI ,
    We have list of bean objects in jSF page we are iterating the list of bean using the forEach loop and displaying the value into Input type text (UI component) value filed .
    If we try to get the UI component value in Managed bean we are not getting the dynamic values .
    The below piece of code used to retrieve the dynamic values from the JSF page doesn't have any form :
    UIComponent component = null;
    FacesContext facesContext = FacesContext.getCurrentInstance();
    if (facesContext != null) {
    UIComponent root = facesContext.getViewRoot();
    component = findComponent(root, componentId);
    then component type casting to the based on UI component which we trying to access and getting the value as " NULL " ..Please let me know how to get the dynamic values form the JSF ?
    Please let me know if any other approach or any changes required on above ?
    Thanks

    Hi,
    the root problem is this
    <h:inputText id="it3" value="#{familyList.ctn}" />
    <tr:commandButton text="Save" id="cb3"Note how each row writes to the same managed bean property, thus showing the same data. Instead your managed bean should expose a HashMap property that you then apply values to using a key/value pair. The key could be the ID of the field, which then you also should dynamically define e.g. cb<rowIndx>. The command button could then have a f:attribute assigned that has the row HahMap key as a value. This way you truly create value instances for the object
    Frank

Maybe you are looking for

  • RSBBS program or TA jump in R/3 not working

    hi gurus, I have a problem in RSBBS configuration. I have a query in BW and i want to link it to a program in R/3 I created the program in R/3. my program has 5 parameters. This program was set upin a transaction . By this way i can see the value of

  • Icon size on Top Sites page

    Every screen shot i've seen show the Top Sites screen showing either 3 rows of 5 or 3 rows of 4. Mine shows 4 rows of 6 making 24. That's way too many for me but i just cannot work out how to alter either individual size or amount of them. Any Ideas?

  • Editing 5dmark footage on macbook pro

    heya , my problem is i have 12 GB of 5dmark II HD footage. theyre all .mov files. i edited some material from a sony xdcam with premiere without problems. but the the 5dmark footage is stuttering in the review window where i can set the in and out po

  • Yet another audio post

    Much like everyone else here, I'm having the no audio output device is installed problem.  I am simply going mad now... Here's a bit of a background. The laptop is a dv6815nr that came with Vista Business and everything worked fine until I upgraded (

  • ITunes payment

    I get the message ur payment method has been declined please enter a new one. pls help me !! thank