Here is the code:
timer.schedule(new TimerTask()
{
public void run()
{
synchronized(this)
{
try
{
// System.out.println(" ITERATION = ");
pachubeCLI.update(78164);
}
catch (PachubeException e)
{
// If an exception occurs it will print the error message from the
// failed HTTP command
System.err.println(e.errorMessage);
}
catch (IOException e)
{
System.err.println(e);
}
}
}
}, 0, 5*1000);
I can tell that the code is basically being used to schedule an operation using an object of the Timer
class. The parameters passed to the schedule
method , according to eclipse , are (TimerTask task,long delay, long period)
. But looking at this code , an entire block of code is being passed as the first parameter instead of a reference to the TimerTask
class. I've never seen such a method before. What exactly is happening here?
Some background: The schedule
method of the Timer
object is being used to update a feed on Xively(previously COSM(previously pachube)) periodically.
Also I don't know which tag describes what is happening here.If you do please add it or drop in a comment.
Simply put, it's when a snippet of code appears multiple times throughout a codebase. It happens for many reasons: Somebody wanted to reuse a function in a different class, and copy-paste was the quickest solution.
Code Inspection: Duplicated code fragmentReports duplicated blocks of code from the selected scope: the same file or the entire project. The inspection features quick-fixes that help you to set the size of detected duplicates, navigate to repetitive code fragments, and compare them in a tool window.
SonarQube is telling you that this portion of the code contains duplicated logic. This doesn't necessarily mean that the code itself is copy-pasted, but that, conceptually, the exact same thing is happening at multiple places.
In computer programming, duplicate code is a sequence of source code that occurs more than once, either within a program or across different programs owned or maintained by the same entity. Duplicate code is generally considered undesirable for a number of reasons.
What you're looking at is known as an "Anonymous Inner Class".
See the javadoc here: http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
and this question: How are Anonymous (inner) classes used in Java?.
And, for the record, both of the tags you used in your question are appropriate.
The method schedule
here, expects arguments of an object of class type TimerTask
, and two other arguments
, probably int and time in ms
. We are passing an object thorugh the anonymous inner class.
The code is creating an instance of the TimerTask
class, but we do not even give the class a name – that's the anonymous inner class.
GuideLines for Anonymous Class.
- Anonymous class is declared and initialized simultaneously.
- Anonymous class must extend or implement to one and only one class or interface resp.
- As anonymous class has no name, it can be used only once.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With