Friday, May 6, 2011

identifying code improvements

We've just gone through a pretty major system rewrite and I have been asked to find and identify areas of the code that have been improved. as a way to justify to the customer that the effort we've spent was worthwhile. Identifying the areas isn't really the hard part but I'm struggling with how to best present this information. Any suggestions on this, or if anyone ahs done something similar in the past would be appreciated.

From stackoverflow
  • It depends on the customer: is the customer technically-minded? Are they internal or external? How much detail do they want? Were you asked to rewrite, and if so by whom and for what reason?

  • In effect, this is a reduction of your technical debt. All of the benefits you would normally receive from that sort of effort will also apply here. Some of the effects will be forward-looking. For example:

    • Reduced defect rate as a result of better, clearer APIs.
    • More rapid development time (and lower costs) because interfaces are easier to test and have fewer integration points.
    • Easier to maintain old code, because the layers they depend on will now be cleaner and freer of cruft.

    Some of them will be immediate, however:

    • Faster build because there's less cruft.
    • Identification of bugs that wouldn't have been found until production because unit testing these areas was too hard.
    • Better separation of layers and concerns.

    The degree to which these sorts of benefits apply will, of course, be specific to your project and its code base.

  • Since it is for non technical people I would suggest Bar and Pie charts. Nothing helps non technical people grasp complex subjects like a big colorful chart.

C++ Iterators Considered Harmful?

At the Boost library conference today, Andrei Alexandrescu author of the book Modern C++ Design and the Loki C++ library, spoke about why iterators are bad, and he had a better solution.

I tried to read the presentation slides, but could not get much out of them. I have these questions for the StackOverflow community:

  1. Are iterators bad?
  2. Is his replacement really better?
  3. Will C++ implementators pick up his ideas?

http://www.boostcon.com/site-media/var/sphene/sphwiki/attachment/2009/05/08/iterators-must-go.pdf

From stackoverflow
  • I agree with him that iterators are mostly inferior to ranges, and I don't know if 'something better' will get picked up.

    "The good is the enemy of the best" is strongly at play here, as it usually is. Iterators are useful and firmly entrenched, so it's hard to know if something better like ranges can supplant them in a reasonable amount of time.

  • I think C++ implementors will have their hands full producing full working support for C++0x, without implementing new, non-standard paradigms.

    Unknown : Aren't ranges standard in some other languages though?
    anon : So what? Lots of things are available in other languages that are not, and probably never will be, avaiolable in C++.
    1. Sometimes
    2. Probably
    3. Not likely, at least not for many years
    Binary Worrier : Almost a haiku, well done twentieth century boy, give us more?
  • First, to answer your questions:

    1. No. In fact, I argued elsewhere that iterators are the most important/fundamental concept of computer science ever. I (unlike Andrei) also think that iterators are intuitive.
    2. Yes, definitely but that shouldn't come as a surprise.
    3. Hmm. Looking at Boost.Range and C++0x – haven't they already?

    Andrei's big contribution here is just to say: drop the concept of iterators altogether, see ranges not just as a convenience wrapper but rather as a core construct. Other languages have already done this (much of Andrei's concepts just echo .NET's LINQ or Python's iterators) but they all only offer output ranges. Andrei argues for different types of ranges, much like the conventional iterator categories.

    In that light, it's odd that he starts by mocking the arbitrariness of these iterator categories.

    I also think that his examples are off, especially his file copying: yes, the iterator variant is a huge improvement over the 1975 code. It reduces a loop with complicated break condition down to one statement. What he's really taking issue with here is just the syntax. Well, excuse me: we're talking about C++ here – of course the syntax is ugly. And yes, using ranges here is an improvement – but only syntactically.

    I also think that Andrei's find implementation is off. What he really defines there is the DropUntil operation (naming is hard!) from LINQ. The find operation should really return either one or zero elements (or an iterator!). Shunning iterators here isn't helpful in my opinion since we might want to modify the value directly instead of copying it. Returning a one-element range here only adds overhead without a benefit. Doing it Andrei's way is bad because then the name of the method is just wrong and misleading.

    That said, I essentially agree with Andrei in almost all points. Iterators, while being my pet concept from computer science, are certainly a big syntactical burden and many ranges (especially infinite generators) can (and should) be implemented conveniently without them.

    James Hopkin : +1: Not sure I agree with everything here, but very interesting to read. Almost as provocative as the great Alexandrescu himself :-)
    wilhelmtell : That's why I love programming more than math. There are opinions and we get to argue. Much more interesting this way. Also: the stream iterators solved the infinite range problem, so it IS possible. Of course, with an extra member variable for the default constructor signaling the end iterator does impose an overhead, as compared to a `while( true )` loop, but how significant is this overhead?
    Konrad Rudolph : @wilhelmtell: You’re right, infinite ranges are absolutely expressible via iterators but the syntax is cumbersome since we have to define some arbitrary “end” iterator whose basic semantics is just to be unequal to all other iterators. That’s quite artificial. I don’t believe that the runtime overhead is significant here – possibly non-existent (no idea really). I just find it weird to call an operation with a start and an end iterator when in reality there’s no end.
  • I think we should use ranges next to iterators, i.e. we should choose the evolution way, not revolution way.

  • C++0x is already making the first steps:

    • rvalue references solve some problems with treating containers as ranges
    • ranges have been added to the core library, including range concepts

    Transitioning to ranges without losing any iterator functionality (think of all the combinations of iterator categories, const-ness and rvalue-ness) is hard, especially if you try to factor in infinite and mutable ranges.

    1. Most of us make a simple use of them in what have become well known idioms, like in for loops to iterate through an std::vector. A developer reads it and knows what's going on. In our everyday coding life, iterators are not good or bad, they're just "what gets the job done".
    2. Probably, yes.
    3. I don't think so.
  • Andrei at times can be a bit provocative. Iterators are a reasonable concept, and quite fundamental in the sense that bits are. But just like most bits in C++ are not bools, but part of larger types,most iterators should be dealt with at a high level. Andrei is right that the proper level to do so is the range object. But not all ranges are properly exposed as iterator ranges, as the istream_iterator sentinel shows. That's just a hack to create an artificial end iterator. I don't think his ideas will be picked up by implementations, though. C++1x will be as relevant as C99.

  • The only argument I can see from that presentation is the inability to define ranges, and the c++0x "Range for statement" proposal seems to eliminate that problem to some extent anyway. maybe it shouldn't be an argument about if iterators should / shouldn't be used at all, but more what situations should / shouldn't they be used for?

  • Like any API or function, if misused can create many problems of identification difficult. Iterators have used in many projects, but always maintaining the necessary care required according to their characteristics. Its use should be preceded by a good understanding of their limitations. Iterators can be very useful if user properly.
    This questions are related :
    Is there any way to check if an iterator is valid?
    Should I prefer iterators over const_iterators?

  • I disagree with both Andrei and Konrad and myself :-)

    The most fundamental concept is an interface not an iterator and that is pretty obvious in any work anyone does today (which is all about cross-library, cross-language, cross-compiler, cross-OS, cross-platform, you cross-name it :-)

    Neither iterator or range (apart from source-level use) offer anything more than a clean and simple, non intrusive or intrusive, non shared or shared, non unique or unique: pointer ! Clean pointer to typed data is simply put universal and you can make data mutable or immutable and many other things. All interface is is just another level of indirection to it while still being friendly to machine and compiler of all sorts, plus far safer, relegating iterators and range usage to an implementation detail.

    To that extent IEnumerable and IQueryable do the half 'right thing' TM but they are clearly inferior in their concepts of iteration and much more to what you can do with STL, retain control and so on and on (but otoh, they have better metadata and hence a better, cleaner model). Point being with interfaces you can build any abstraction you want and satisfy, well probably contraversial but essentially a no-brainer: optimal, and runtime or compile-time neutral data representation and code (heck essential to algorithms and compilers and VMs and what not).

    It is even possible to optimise it for 'dynamic'/component systems down to 'runtime' inlining (screw HotSpot VM:-).. To that extent, the advance to 1975 is minimal as evident by a huge interop industry workload (it's everywhere you look, including this site, its use of proprietary and open tech, etc; in computer science idealism, well, this type of interfacing 'work' should not exist should it)..

    Konrad Rudolph : The point is that you need a *well-defined* interface (or a set thereof). So the question is really: *which* interface to use? The one offered by iterators? Ranges? …
    rama-jka toti : Completely agree.. imho, something that won't ever be technology or single-idea specific is the way forward, domain and model driven tools will have the same tough time there. Plus the world is such it enforces one proprietary solution over another for vendor lock in especially with VMs and their own ideas of integration (market share). But it all still favours K&R idea as minimal and complete, much like templates and concepts in C++ push forward, or indeed metadata of execution environments help another aspect of 'reshape'/reuse. Protocols are simply hard to do and follow.
    rama-jka toti : On a side note, what's worst, it makes tweats like Linus right in C++ being a moving target for almost fundamental abstraction change that impacts client code considerably (or to point of ease-of-breakage). Then again, Linux kernel is in such a state it wouldn't be suitable for anything but OS and down-to-metal building (which is sufficient I guess but still inflexible for future). Not that other OS-es are not copying and reinventing *nix, ie. W6.0, W7 especially, but modularity doesn't come from dependency breaking solely.[someone fix the crappy ASP.NET HTML constraints for this site PLEASE]
    rama-jka toti : Reuse goes beyond module/assembly and OO here and it applies to protocols and mechanisms that are effectively algorithms or algorithm friendly (CLR isn't; ie. numerics ). Sequences are a problem for every protocol with complex, non-primitive types, heck even simple arrays can exhibit similar issues. LINQ won't cut it everywhere as evident with continous streaming attempts too, it is as bloated and heavy as any high-level protocol.
    jalf : I don't think you answer the question. Yes, we need an interface. What should it look like? Iterators define an interface. So do ranges. But "interface" does not.
    rama-jka toti : Sure, no answer.. but what you certainly want to see is change of the interface in the model rathen than source. As it currently stands, any change simply breaks code and idioms. Something many libraries are popular for; all I want is to change it as say underlying hardware might prefer something else. Why should it be a specific interface? So somebody can come and say look that abstraction is now 'outdated' yet again? It is typical of C++ and will be typical of C++ reinventing itself which is what the question is about.
    rama-jka toti : Note that I'm not in favour of iterator or range or IEnumerable or IQueryable.. my argument is for neutrality and none of those give me anyone of it cross-language, cross-compiler, or anything similar. Which is just the sign of the times, you pick yours, someone else picks another one, depending on the phase of the moon. I'd prefer to be able to say I don't care whether it is an iterator or a range, but that might be just me..
  • Isn't Andrei trying to do some hidden marketing for the D language (currently he is working with it)...?

    Andrei states that containers are ok, but iterators are ugly, non-intuitive, error-prone and dangerous, hard to implement (well this last one seems to be rather true...) And what do we have in C++... pointers? Aren't they ugly/.../dangerous? But we happily embraced them and live with them.

    Which one is more intuitive to write:

    for(auto i=foo.begin();i!=foo.end();++i)
        bar(*i);
    

    or

    for (auto r=foo.all(); !foo.empty(); foo.popFront())
            bar(r.front());
    

    Iterators concept can be complemented with ranges and other ideas, but I think that they have their place and won't be replaced.

    Unknown : "Isn't Andrei trying to do some hidden marketing for the D language" I was wondering exactly the same thing.
    rama-jka toti : He is,and he is also behind some of the constructs that got far more refined.Boost is continously expanding and rewriting itself the compile-time-STL way but it does break interfaces and it brings complexity that kills with plenty of bugs.I don't care whether it is C++, D or C#, iterator or not. I just want maintainable code without going into that level of detail, just like I don't want to go into MPL in detail (it's a suicide, something machine translation should be doing on your intent an not a brain picking over syntax to grasp its meaning with usual keyboard-reflex :). They will 'go'.
    1. No, they are not bad, they are very clever idea in fact. However, they are not ideal and there is room for improvements in the concept of iterator.

    2. It solves number of real-life problems with iterators. For instance, it's tedious (also error prone) in many cases to query two separate objects, iterators, from a single containers and then pass them as still two separate objects to an algorithm. Why not to pass a single object around? Even std::pair<iterator, iterator> would make for a crude range which is easier to manipulate - one object, not two. Also, it's a good idea to consider a range is an iterator. That's in fact what Andrei suggests. By the way, some of these problems have been already solved by Boost.Range.

    3. I would expect it happened, but it will not be a revolution, rather evolution.

Is it possible to select all floats on a page with JQuery ?

I am trying to select all elements that have CSS computed style float:left or float:right. I see attribute selectors available in the JQuery documentation, however I am interested in CSS properties not attributes.

Possible use cases for such a feature would be to select say all display:none elements on a page.

From stackoverflow
  • One idea would be to apply the floating style via CSS then you could select on the class name and do a show/hide as necessary.

     <style>
        .right { float: right; }
        .left { float: left; }
     </style>
    
     <div class='left'>...</div>
     <div class='right'>...</div>
    
      $('.left,.right').hide();
    
    Jed Schmidt : $('.left.right') would be empty in this case, since you're looking for elements with both classes. Did you mean $('.left, .right')?
    tvanfosson : The interesting bit is that I had it that way originally and then must have misread the documentation. Yes, the comma needs to be there.
  • This should do the trick without class hacking:

    $("*").filter( function() {
        return /^(left|right)$/.test( $(this).css("float") )
    })
    

    By the way, jQuery already has a nice way to find all display: none elements:

    $(":hidden")
    
  • Creating new selectors is kind of fun, so I did that:

    Usage:

    :hasCssAttr(property, value ...)

    Property is the css property you would like use to compare

    value is the value(s) you would like to match against (you can have more than one)

    $(':hasCssAttr(float, left)').css('float', 'right');
    

    The source Luke:

    $.expr[':'].hasCssAttr = function(objNode, intStackIndex, arrProperties, arrNodeStack) {
      var arrArguments = arrProperties[3].split(','); 
      var cssPropVal = $(objNode).css(arrArguments[0]); // need for speed
      for (var i = 1 ; i < arrArguments.length ; i++)
        if (cssPropVal == arrArguments[ i ].replace(/^\s+|\s+$/g,""))
            return true;      
      return false;
    }
    

    Basically, this selects any ol' css property. I suppose you could eliminate the loop if you only wanted one value, kind of unnecessary. Also, I wonder if it might be more interesting to do this in an eval so you could do numerical comparisons. Anyway. There it is.

    Props to Ben for helping me out.

    KyleFarris : +1 for making your own flexible selector. I always prefer flexible solutions when possible.

Looking for a set of tools/objects to start a website in PHP

Looking for a good set of base objects to start a website up in PHP. I am not looking for links to CakePHP as I am not interested in frameworks. I am looking for a set of objects that would come handy to start off with for new projects...

Objects such as:

  • Loggers
  • MySQL wrapper object
  • etc

Basically a "Start with this", I remember seeing a nice collection of objects and code on Digg, but I can't find it again. There were user objects (for logging in), fsck editor built in, etc...

From stackoverflow
  • MySQL wrapper object

    Use PDO.

    I am not interested in frameworks

    Well, you are. You are not looking for a tightly coupled, full stack framework perhaps.

    You might find one or more of the following useful:

    They are all very loosely coupled frameworks, where you can pick and match as you need it.

    Mike Curry : hmm, I've been looking at Zend... seems it is exactly what I want. I've been against frameworks all this time... and they are exactly what I wanted... in this case, ignorance is NOT bliss, lol. Thanks :)
  • Well, there's a bunch of digg related code found here.

    http://github.com/digg/

    I also don't understand why you don't want to use a framework, but want to use all the components that make a framework. That didn't make much sense to me.

    I would look into the Zend Framework. Before you scream at me for recommending it, a lot of the components are modular which means you can take the bits you need.

    http://framework.zend.com/manual/en/

    PS : Although Zend Framework has "framework" in the title, a lot of people see it as a collection of classes. So check it out :)

  • Frameworks by their very definition are a set of objects that come in handy when starting a new project.

    PDO can give you database abstraction without resorting to third-party libraries. Templating can be accomplished with the alternate syntax (foreach():) and short tags (<?= $var ?>). You can do it all yourself if you want, just keep the docs up and get crackin' redefining that wheel.

  • I can certainly recommend Zend Framework for this purpose. As a glue framework you can use as many or as few of the components as you like, with or without the MVC.

Call external dll function (c#) from postgres...

Is it possible? How?

Thanks!

From stackoverflow
  • It looks like there is some overhead to calling C functions from PosgtreSQL, External Functions in Postgres @ Linux Gazette covers writing such a method in C. I would surmise from that that you could, if you REALLY need to, build a hosting module that loads a CLR instance and provides an entry point into your C# method that is mapped with the appropriate Postgres linking logic. This seems extremely costly.

    Another approach that might be better would be to encapsulate the C# method (which I am assuming is already written) in a web service, and use a wrapper (not necessarily C, but one of the lighter-weight extension development bindings for Postgres) that will call out to the web service.

  • You can define an external function using CREATE FUNCTION. This reference implies that the DLL must be a native DLL using the C calling convention and hence cannot handle managed code.

    You could try to use Mono AOT or MS Native Image Generator to compile the managed code to native code, but I cannot tell if this will result in a native DLL using C calling convention. You have to try.

    If this does not work, you could create an unmanged wrapper for your DLL.

    UPDATE

    You have to write a wrapper - C-Language Functions states that the DLL must include a magic block that will not be availiable if you compile managed code to native code.

What steps do you take when writing a really large business processes?

I was given this really long flow chart (8 pages) of how we process our financial transactions. The most critical piece of our application. I'm sure you can imagine lots of squares and diamons with lines drawn all over the place, and then separate detail sheets describing which fields in the database to update, calculate, retrieve, store, etc. etc. etc.

Does anyone have any tips/tricks/suggestions as to the best approach to coding this thing? Do you start with 5 billion if statements just like the flow chart does and then refactor from there?

Any thoughts are appreciated.

From stackoverflow
  • I like to extract some business object out of the mess first. Try to come up with some classes to separate the responsibilities using the typical low coupling, high cohesion guidelines. After that, look for duplication in the flow and think of ways to keep that duplication out of the code.

  • Personally, I look for patterns that I can turn into reusable functions. Often I'll find several different areas that I can create very generic functions that can handle multiple tasks, depending on the parameters I pass in.

    Tetsujin no Oni : -1 for the blatant violation of SRP
  • You have my sympathy.

    That being said, look for anything you can abstract away. Look for similarities. Take more or less coherent sections and write functions out of them.

    Then, when you've done your best, be prepared to spend a lot of time refactoring.

    Then, since I don't believe humans can write accurate 8-page flow charts, be prepared for people blaming you for stuff that doesn't work right. Ideally, you should be able to trace your logic back to the flow chart, and at least divert the blame. Unfortunately, this is not really compatible with refactoring it to something sane.

    Micah : Amen on the blame point!
  • For something that large, you'd better have a good model in place. Look at your business process and identify the actors, the acted-upon, etc. Once you have that model in place, start to flesh it out - work out what a given step means in terms of the actors and the acted upon, determine whether the actor or the acted upon is the best place to handle that part of the interaction, and so on.

    This will translate your big long flow chart into more of a Sequence Diagram which will help you turn the flow chart into code, by identifying the methods you'll need on your objects (and potentially identify more objects that you overlooked in the first place).

    Granted, this assumes an OO perspective, so if you're doing this in COBOL it may not help much.

  • First off, don't do it. Seriously, don't.

    You are going to take ownership of a few hundred processes that not only are not your job, they aren't your specialty either. More so, you won't do it right ( trust me ) and you will be creating a full time job ( or 6! ) fixing and maintaining a system that never works right.

    If you are in an environment that can generate such a large business process, I hope you are in an environment where you can get reasonable funding. What you want to look into is BPM software, meaning Business Process Management. Companies like Captaris, K2 and Ultimus all develop products, as do big boys like IBM and the like. These tools allow you to give the tools to business experts, so the people in the process that actually know what they are doing, then you have domain experts controlling their own domains, generally in a very excel like environment, but sometimes using tools that look alot like Visio ( or.. that are visio! ). You as a developer then, provide the transport between departments, and specialized logic to tie your various business systems together ( or to pull data from various sources.

    Granted, BPM software start in about the 20K range, and the sky is the limit from there. Plus, as you expand you will probably want a document repository like Sharepoint ( which can start from as low as free and up to 100K+$ ) and a forms system like Adobe or Infopath, but all told, you can build a scalable end to end system starting around 30 - 40 grand, depending on the number of users you have.

    That might sound like an obscene amount, but truly it isn't. Factor in you labour costs, all the labour costs of the various people you will have to meet with, the fact your are probably creating a full time job for a few people and creating a system that probably will never really be 100%... the money is well spent.

    Keep domain specific knowledge in the knowledge experts hands, and keep IT specific tasks in IT's hands. This is an area where I see IT department after department screw up and end up with highly valuable employees getting paid high salaries to baby sit report generation. Honestly, I say again, don't do it! This is one of those areas where off the shelf products will be vastly superior than anything you can create.

    (Note, most of my examples betray my most recent background. I am from a Windows shop with a .Net centric development team. You can just as easily find BPM solutions for Unix, Linux, whatever, including a few open source options with Java as the back end instead. )

    EDIT: Oh, and for the record, if you absolutely can't buy off the shelf, if your budget is zero $, you will find most commercial BPMs implement a state based machine, with a gated flow chart-eqsue structure with a database ( or even spreadsheet ) driven back end. If you need to do it yourself, you would still be well served trying a demo from any of the various BPM providers and emulating their approach.

    EDIT2: Links. Captaris

    Ultimus

    Open Source (ProcessMaker)

    There are tons more, but these will get you started. BPM software is one of those things that can go from thousands to millions of dollars, and potentially free. I can only speak towards Captaris, which I choose about 3 years ago and worked as advertised.

  • If the process requires any human interaction (i.e. decision making) and is long running or there are lots of similar business processes, then a workflow (BPM) product like K2 or ProcessMaker might be appropriate. If your business process is largely orchestrating data flows then a product like BizTalk might be appropriate.

    Note that there is a significant learning curve for workflow programming (generally due to poor debugging support). So, if you aren't already familiar with workflow programming and quick turnaround is required, you probably don't want to go down this path.

    If your task is to implement a relatively fast process (meaning it runs from beginning to end without pause and does not require human interaction), then I would tackle it in the following manner:

    1. identify the process steps
    2. Code a runnable shell of the process without implementing any of process step details, along with (failing) unit tests for each process step and for the overall process.
    3. identify the subsystems the process interacts with
    4. identify the business objects each process step interacts with
    5. identify the data flows to and from each subsystem
    6. identify the contracts for interacting with the business objects
    7. if necessary, code the business objects, with unit tests for functionality required in the final product
    8. implement the process steps one by one until all the unit tests pass.
    9. debug the overall process (add unit tests if necessary)
    10. integration testing (add unit tests if necessary)
    11. deployment
  • I totally agree with David T - no human can create an 8 page flow-chart and have it be a solid-bullet proof set of steps. I would change to a state diagram (http://en.wikipedia.org/wiki/State_diagram) basically describing the life cycle of the financial transaction - from conception to final state. This would probably help with the overall approach you take to the design and implementation of the process and greatly reduce the nested if statements (each nesting exponentially increases the complexity and brittleness of the code). If each step in the life cycle is clearly decoupled you can then easily modify as needed (yes - business needs change - ha ha) without impacting the other steps. Keep in mind testing needs and logging to ensure each step is clearly recorded for traceability and debugging.

Fixing BeanNotOfRequiredTypeException on Spring proxy cast on a non-singleton bean?

I'm having an issue with pulling a Spring bean from an application context.

When I try;

InnerThread instance = (InnerThread) SpringContextFactory.getApplicationContext().getBean("innerThread", InnerThread.class);

I get;

org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'innerThread' must be of type [com.generic.InnerThread], but was actually of type [$Proxy26]

Without the specified class in the getBean() call I get a ClassCastException (which you can see in detail below).

The InnerThread bean is being initialized as a non-singleton, because I need multiple instances. The InnerThread class also extends Thread. The interesting thing is that this error shows up within OuterThread, which is set up in the exact same way the InnerThread is.

I've tried to include all relevant code listings/stack traces below. Could someone with more Spring experience tell me what is going on here?


Code/Configuration Listing

OuterThread.java snippet:

public class OuterThread extends Thread {
    private Queue<InnerThread> createInnerThreads() {
     Queue<InnerThread> threads = new ArrayBlockingQueue();

     ApplicationContext ctx = SpringContextFactory.getApplicationContext();
     int i = 0;
     for (SearchRule search : searches) {
      logger.debug("Number of times looped " + i++);
      //Seprated lines to get a better sense of what is going on
      Object proxy = ctx.getBean("innerThread", InnerThread.class);
      logger.debug(ReflectionToStringBuilder.toString(proxy));
      logger.debug("proxy.getClass(): " + proxy.getClass());
      logger.debug("proxy.getClass().getClassLoader(): " + proxy.getClass().getClassLoader());
      logger.debug("proxy.getClass().getDeclaringClass(): " + proxy.getClass().getDeclaringClass());
      logger.debug("InnerThread.class.getClassLoader(): " + InnerThread.class.getClassLoader());

      //---Exception here---
      InnerThread cst = (InnerThread) proxy;

      threads.add(cst);
     }
     return threads;
    }

    public static void main(String[] args) throws Exception {
     try {
      OuterThread instance = (OuterThread) SpringContextFactory.getApplicationContext().getBean("outerThread", OuterThread.class);
      instance.run();
     } catch (Exception ex) {
      logger.error("Fatal exception.", ex);
      throw ex;
     }
    }
}

SpringContextFactory.java:

public class SpringContextFactory {
    static final Logger logger = LoggerFactory.getLogger(SpringContextFactory.class);
    private static ApplicationContext ctx;
    private static final String DEFAULT_PATH = "META-INF/app-context.xml";

    public static ApplicationContext getApplicationContext() {
        return getApplicationContext(DEFAULT_PATH);
    }

    public static synchronized ApplicationContext getApplicationContext(String path) {
        if (ctx == null) return createApplicationContext(path);
        else return ctx;
    }

    private static ApplicationContext createApplicationContext(String path) {
        if (logger.isDebugEnabled()) logger.debug("Loading Spring Context...");
        ctx = new ClassPathXmlApplicationContext(path);
        if (logger.isDebugEnabled()) logger.debug("Spring Context Loaded");
        return ctx;
    }
}

app-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <!-- persistence context from separate jar -->
    <import resource="persistence-context.xml"/>

    <bean id="outerThread" class="com.generic.OuterThread" scope="prototype"/>
    <bean id="innerThread" class="com.generic.InnerThread" scope="prototype"/>

</beans>

Stack Trace

2009-05-08 14:34:37,341 [main] DEBUG com.generic.OuterThread.init(OuterThread.java:59) - Initializing OuterThread object, com.generic.OuterThread@1c8fb4b[em=org.hibernate.ejb.EntityManagerImpl@e2892b,currentTime=java.util.GregorianCalendar[time=1241634874841,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=sun.util.calendar.ZoneInfo[id="America/New_York",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=America/New_York,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2009,MONTH=4,WEEK_OF_YEAR=19,WEEK_OF_MONTH=2,DAY_OF_MONTH=6,DAY_OF_YEAR=126,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=2,HOUR_OF_DAY=14,MINUTE=34,SECOND=34,MILLISECOND=841,ZONE_OFFSET=-18000000,DST_OFFSET=3600000],maxConcurrentThreads=5,reconId=3,reportUsername=TEST,useOffset=false,username=removed,uuid=bf61494d-ff96-431c-a41f-1e292d0c9fbe,name={T,h,r,e,a,d,-,1},priority=5,threadQ=<null>,eetop=0,single_step=false,daemon=false,stillborn=false,target=<null>,group=java.lang.ThreadGroup[name=main,maxpri=10],contextClassLoader=sun.misc.Launcher$AppClassLoader@11b86e7,inheritedAccessControlContext=java.security.AccessControlContext@1524d43,threadLocals=<null>,inheritableThreadLocals=java.lang.ThreadLocal$ThreadLocalMap@2cbc86,stackSize=0,nativeParkEventPointer=0,tid=9,threadStatus=0,parkBlocker=<null>,blocker=<null>,blockerLock=java.lang.Object@a68fd8,stopBeforeStart=false,throwableFromStop=<null>,uncaughtExceptionHandler=<null>]
2009-05-08 14:34:37,341 [main] DEBUG org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.doJoinTransaction(ExtendedEntityManagerCreator.java:385) - No local transaction to join
2009-05-08 14:34:37,529 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:139) - Number of times looped 0
2009-05-08 14:34:37,529 [main] DEBUG org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:458) - Creating instance of bean 'searchThread' with merged definition [Root bean: class [com.generic.InnerThread]; scope=prototype; abstract=false; lazyInit=false; autowireCandidate=true; autowireMode=0; dependencyCheck=0; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null; defined in class path resource [META-INF/app-context.xml]]
2009-05-08 14:34:37,545 [main] DEBUG com.generic.InnerThread.<init>(InnerThread.java:50) - Constructing InnerThread object, com.generic.InnerThread@1080876[em=<null>,coolScheme=<null>,coolUrl=<null>,date=<null>,error=<null>,millisecondsTaken=0,thresholdMet=false,reconId=0,result=-2,searchId=0,username=<null>,uuid=<null>,name={T,h,r,e,a,d,-,2},priority=5,threadQ=<null>,eetop=0,single_step=false,daemon=false,stillborn=false,target=<null>,group=java.lang.ThreadGroup[name=main,maxpri=10],contextClassLoader=sun.misc.Launcher$AppClassLoader@11b86e7,inheritedAccessControlContext=java.security.AccessControlContext@1524d43,threadLocals=<null>,inheritableThreadLocals=java.lang.ThreadLocal$ThreadLocalMap@3aef16,stackSize=0,nativeParkEventPointer=0,tid=10,threadStatus=0,parkBlocker=<null>,blocker=<null>,blockerLock=java.lang.Object@126c6ea,stopBeforeStart=false,throwableFromStop=<null>,uncaughtExceptionHandler=<null>]
2009-05-08 14:34:37,545 [main] DEBUG org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:203) - Returning cached instance of singleton bean 'entityManagerFactory'
2009-05-08 14:34:37,545 [main] DEBUG org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:203) - Returning cached instance of singleton bean 'org.springframework.transaction.config.internalTransactionAdvisor'
2009-05-08 14:34:37,560 [main] DEBUG org.springframework.transaction.interceptor.AbstractFallbackTransactionAttributeSource.getTransactionAttribute(AbstractFallbackTransactionAttributeSource.java:108) - Adding transactional method [report] with attribute [PROPAGATION_REQUIRED,ISOLATION_DEFAULT]
2009-05-08 14:34:37,560 [main] DEBUG org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.buildAdvisors(AbstractAutoProxyCreator.java:494) - Creating implicit proxy for bean 'searchThread' with 0 common interceptors and 1 specific interceptors
2009-05-08 14:34:37,560 [main] DEBUG org.springframework.aop.framework.JdkDynamicAopProxy.getProxy(JdkDynamicAopProxy.java:113) - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [com.generic.InnerThread@1080876]
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:141) - $Proxy26@1594a88[h=org.springframework.aop.framework.JdkDynamicAopProxy@1f0cf51]
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:142) - proxy.getClass(): class $Proxy26
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:143) - proxy.getClass().getClassLoader(): sun.misc.Launcher$AppClassLoader@11b86e7
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:144) - proxy.getClass().getDeclaringClass(): null
2009-05-08 14:34:37,591 [main] DEBUG com.generic.OuterThread.createInnerThreads(OuterThread.java:145) - InnerThread.class.getClassLoader(): sun.misc.Launcher$AppClassLoader@11b86e7
2009-05-08 14:34:37,591 [main] ERROR com.generic.OuterThread.run(OuterThread.java:101) - Exception in OuterThread, ending reconciliation.
java.lang.ClassCastException: $Proxy26 cannot be cast to com.generic.InnerThread
    at com.generic.OuterThread.createInnerThreads(OuterThread.java:148)
    at com.generic.OuterThread.run(OuterThread.java:65)
    at com.generic.OuterThread.main(OuterThread.java:170)


Similar questions that do not answer my question

From stackoverflow
  • This is just a guess, but try making an interface InnerThreadInterface, then let InnerThread class extend it.

    After that you should be able to do:

    InnerThreadInterface inner = ctx.getBean("innerThread", InnerThread.class);

    James McMahon : I gave this a try, unfortunately no dice on this one.
  • Once again, after spending hours trying to debug this I find the answer right after posting on StackOverflow.

    A key point that I left out from my question is that InnerThread has a transactional method (sorry thought this was irrelevant). This is the important difference between OuterThread and InnerThread.

    From the Spring documentation:

    Note

    Multiple sections are collapsed into a single unified auto-proxy creator at runtime, which applies the strongest proxy settings that any of the sections (typically from different XML bean definition files) specified. This also applies to the and elements.

    To be clear: using 'proxy-target-class="true"' on , or elements will force the use of CGLIB proxies for all three of them.

    Adding the above to my configuration (based in persistance-context.xml, which you can see loaded above) line seems to fix the problem. However, I think this may be a quick fix workaround as opposed to real solution.

    I think I've got a few deeper issues here, number one being that I find Spring as confusing as expletive deleted. Second I should probably be using Spring's TaskExecutor to kick off my threads. Third my threads should implement Runnable instead of extending Thread (See SO question below).

    See Also

  • I had this issue even though I referenced CGLIB and used the proxy-target-class="true" setting. I determined ehcache:annotation tag was to blame... Removing the following configuration solved this for me. Fortunately, I was able to use hibernate level 2 caching instead of having to use ehcache declarative caching.

    <ehcache:annotations>
     <ehcache:caching id="myCacheModel" cacheName="myCache"/>
     <ehcache:flushing id="myFlushModel" cacheNames="myCache" when="after"/>
    </ehcache:annotations>
    
  • Another way to handle this problem would be to implement an interface and request the interface from spring, the proxy would fully implement the interface, and the cast shouldn't have a problem.