How can i define method in file with extension .class? - java

I need to define another method in ITuple.class like
public Object getValue(int i);
but with Float
public Object getValue(float j);
How can i add it ?
I'm new to storm so Can I find the method that make the same job as I searched and couldn't find , isn't right?

I think you are misunderstanding what getValue(int) does. Here is the description from the javadocs:
Object getValue(int i)
Gets the field at position i in the tuple. Returns object since tuples are dynamically typed.
As you can see, the int argument is the position in the tuple; i.e. the index. Tuple position are inherently integers, so adding an alternative that takes a floating point argument doesn't make any sense.
Supposing (hypothetically) that it did make sense to add a getValue(float) overload, then the way to do it would be to:
download the source code (".java" files),
modify the interface in the ITuple.java source file
modify the source files for classes that implement the interface
build them all to produce new JAR files
use those JAR files in your application
... and repeat this patching procedure every time you upgraded your Apache Storm release. That is probably a bad idea, even if what you were doing made sense.
But attempting to modify ".class" files directly is an even worse idea.

.class files are compilated java files. I assume you found this file in a library.
I'm afraid to inform that this code isn't yours and you cannot edit it.
What you can do, however, is extends the ITuple interface in a MyITuple interface and add whatever you want in it.

Related

How to use java.nio in Matlab?

My goal is to check if a file with a particular (part of the name) is found in a folder on the network, also taking into account all folders below it. To do so I need a way to efficiently get a list of all files and folders in and below a given folder. My recursive function does ~2500 items/s on a local drive, but only several/sec on a network drive. I need something faster.
The core question is: what is the fastest way to get a list of items in a folder including the attribute isDirectory or something similar?
I put my hope on the walkFileTree functionality of java.nio, but I am unable to use it. (version: 8.4.0.150421 (R2014b) with Java 1.7.0_11-b21 with Oracle Corporation Java HotSpotâ„¢ 64-Bit Server VM mixed mode)
Current problem: I am unable to use any functionality from java.nio
java.io works, e.g. create a file object:
jFile = java.io.File('C:\')
% then use jFile.list or jFile.isDirectory or jFile.toPath, it all works!
Naively calling nio fails:
java.nio.file.Files('C:\')
% -> No constructor 'java.nio.file.Files' with matching signature found.
I realize java.nio.file works a bit differently, to use the methods in Files a path is needed, which can be constructed with java.nio.file.Path.get. This thing eats a string. But this also fails:
java.nio.file.Paths.get('C:\') % -> No method 'get' with matching signature found for class 'java.nio.file.Paths'.
However the method exists:
methods java.nio.file.Paths
% -> Methods for class java.nio.file.Paths:
equals getClass notify toString
get hashCode notifyAll wait
So what is going wrong here? I am not allowed to feed a matlab string? Should I use a Java string? This too fails:
jString = java.lang.String('C:\');
java.nio.file.Paths.get(jString)
% -> No method 'get' with matching signature found for class 'java.nio.file.Paths'.
An oracle workaround is to create the path in java.io, but feeding that to java.nio also fails..
path = java.io.File('C:\').toPath;
java.nio.file.Files.isDirectory(path)
% -> No method 'isDirectory' with matching signature found for class 'java.nio.file.Files'.
So I am not getting any closer to even trying the walkFileTree. I can not get java.nio to do anything in Matlab.
Help: so does anybody have any idea on how to call the java.nio.file functions or answer my core question?
ps: examples of straightforward methods so far without java.nio, examples do no include the recursive part but show the horrible performance
strategy 1: recursively use Matlab's 'dir' function. It is a nice function, as it also gives attributes, but it is a bit slow. In my starting network folder (contains 150 files/folders, path stored as string Sdir) the following command takes 34.088842 sec :
tic;d=dir(Sdir);toc
strategy 2: use java.io.File to speed things up, this hardly helps, because isDirectory needs calling.. Using a heuristic on the names of the items is too dangerous, I am forced to use folders with dots in them. Example in same dir, 31.315587 sec:
tic;jFiles = java.io.File(Sdir).listFiles;
LCVdir = arrayfun(#isDirectory, jFiles, 'UniformOutput',0);
toc
Those java.nio.file methods have variadic signatures. Looks like Matlab is unable to do the auto-boxing needed to make them work transparently, so you will need to call them with the array form of their arguments.
The signature for java.nio.file.Paths.get is get(String first, String... more). This is equivalent to get(String first, String[] more).
>> java.nio.file.Paths.get('C:\', javaArray('java.lang.String', 0))
ans =
C:\
>> class(ans)
ans =
sun.nio.fs.UnixPath
Similarly, the signature for java.nio.file.Files.isDirectory is isDirectory(Path path, LinkOption... options), so you need to supply the options argument.
>> p = java.nio.file.Paths.get('/usr/local', javaArray('java.lang.String', 0));
>> java.nio.file.Files.isDirectory(p, javaArray('java.nio.file.LinkOption', 0))
ans =
logical
1
>>
BTW, the Files.walkFileTree method will require you to implement a custom java.nio.file.FileVisitor subclass, which you will need to do in Java, not plain Matlab.
Also, since you're on a network drive, the network file I/O might actually be your bottleneck here, so don't get your hopes too high for the Java NIO solution to be much faster. To make this really fast, the traversal needs to be run on a machine that has fast access to the filesystem data, or even better, something that has indexed it for efficient searching.

Eclipse file replacement before compile (like a pre-processor)?

So, given that Java has little to no support to unsigned types, I'm right now writing a small API to handle these (for now, I have UnsignedByte and UnsignedInt). The algorithm is simple: store each of them as their higher representation (byte->short, int->long), extends the Number class and implement some calculation and representation utility methods.
The problem is: it is actually very verbose - and boring - to have to, every time, code things like:
UnsignedByte value = new UnsignedByte(15);
UnsignedByte convert = new UnsignedByte(someIntValue);
I was wondering: is there any way to implement, on Eclipse, something like a "file pre-processor", in a way that it will automatically replace some pre-defined strings with other pre-defined strings before compiling the files?
For example: replace U(x) with new UnsignedByte(x), so it would be possible to use:
UnsignedByte value = U(15);
UnsignedByte convert = U(someIntValue);
Yes, I could create a method called U(...) and use import static, but even then, it would be so much trouble doing it for every class that I would use my unsigned types.
I could write a simple Java program that would replace these expressions in a file, but the problem is: How could I integrate that on Eclipse, in a way that it would call/use it every time a Java file is compiled?
I would recommend using Eclipse Templates for doing this instead. I know its not exactly what you ask for but its very simple and can be achieved out of the box.
When you write sysout in Eclipse and press Ctrl+Space it gives you an option to replace that with System.out.println();
You can find more information in the following link
How to add shortcut keys for java code in eclipse
I can point you at how one project I know of does this, they have a set of Python scripts that generate a whole set of classes (java files) from a template base file. They run the script manually, as opposed to part of the build.
Have a look here for the specific example. In this code they have a class for operating on double, but from this class they want to generate code to operate on float, int, etc all in the same way.
There is, of course, a big debate about whether generated code should be checked in or not to source repository. I leave that issue aside and hope that the above example is good to get you going.

FindBugs: Detect invocation of Object.hashCode()

If an object doesn't implement it's own hashCode() method, then it will use the default implementation Object.hashCode() (provided there's no superclass in between). Object.hashCode() doesn't guarantee the same hash code to be generated in different JVM instance. We are having some problems because of this in a clustered environment.
Additionally to some fixes that we applied, we would like to have static analysis detect this case. We are already using FindBugs, but unfortunatly I have no experience extending the default ruleset.
I've done some research and I know that you can implement your own custom detectors, but I have not found much documentation on how to do this.
I guess my questions are:
Before I invest too much work here, is this approach reasonable, can FindBugs do this?
What's the best resources to get me started writing custom detectors?
Thanks for your input!
Findbugs has some checks for hashCode already: (see also http://findbugs.sourceforge.net/bugDescriptions.html )
HE_EQUALS_NO_HASHCODE
HE_EQUALS_USE_HASHCODE
HE_HASHCODE_NO_EQUALS
HE_HASHCODE_USE_OBJECT_EQUALS
HE_INHERITS_EQUALS_USE_HASHCODE (this might be of interest for your case)
If those are not sufficient for you, they might be a good starting point for creating a custom detector.
UPDATE. The source code of the detectors can be found in https://code.google.com/p/findbugs/source/browse/findbugs/src/java/edu/umd/cs/findbugs/ and the other packages of that repo.
You may try it the other way around:
Add a hashCode() method to all your (entity like) classes. The nonexistance of that method can easily be verified with findbugs. The implementation would look something like:
#Override
public int hashCode() {
throw new UnsupportedOperationException("hashcode() not supported.");
}
By that you can ensure that there is no Object.hashCode() "fallback" - that class will not be used in HashMaps, HashTables, HashSets or any other situation where hashCode() will be called.
I think that will be really difficult (if it's even possible). But I have two other things you could try to find places where classes should override hashCode():
At least Netbeans has a hint for "overrides equals but not hashCode" that could help a little.
Place a breakpoint on Object.hashCode() and run a more or less representative testset.
If you want to find ALL classes which do not implement getHashCode(), couldn't you just use a simple text-search / grep approach ?
Just search all Files with ending .java in your project which do not contain the string "public int getHashCode", would be fairly easy to write a script for this. You can e.g. just use a simple search-tool to find all java-files containing the text and substract this list from a list of all .java files. The resulting list will have all .java files which do not override getHashCode()

Java source refactoring of 7000 references

I need to change the signature of a method used all over the codebase.
Specifically, the method void log(String) will take two additional arguments (Class c, String methodName), which need to be provided by the caller, depending on the method where it is called. I can't simply pass null or similar.
To give an idea of the scope, Eclipse found 7000 references to that method, so if I change it the whole project will go down. It will take weeks for me to fix it manually.
As far as I can tell Eclipse's refactoring plugin of Eclipse is not up to the task, but I really want to automate it.
So, how can I get the job done?
Great, I can copy a previous answer of mine and I just need to edit a tiny little bit:
I think what you need to do is use a source code parser like javaparser to do this.
For every java source file, parse it to a CompilationUnit, create a Visitor, probably using ModifierVisitor as base class, and override (at least) visit(MethodCallExpr, arg). Then write the changed CompilationUnit to a new File and do a diff afterwards.
I would advise against changing the original source file, but creating a shadow file tree may me a good idea (e.g. old file: src/main/java/com/mycompany/MyClass.java, new file src/main/refactored/com/mycompany/MyClass.java, that way you can diff the entire directories).
Eclipse is able to do that using Refactor -> Change Method signature and provide default values for the new parameters.
For the class parameter the defaultValue should be this.getClass() but you are right in your comment I don't know how to do for the method name parameter.
IntelliJ IDEA shouldn't have any trouble with this.
I'm not a Java expert, but something like this could work. It's not a perfect solution (it may even be a very bad solution), but it could get you started:
Change the method signature with IntelliJ's refactoring tools, and specify default values for the 2 new parameters:
c: self.getClass()
methodName: Thread.currentThread().getStackTrace()[1].getMethodName()
or better yet, simply specify null as the default values.
I think that there are several steps to dealing with this, as it is not just a technical issue but a 'situation':
Decline to do it in short order due to the risk.
Point out the issues caused by not using standard frameworks but reinventing the wheel (as Paul says).
Insist on using Log4j or equivalent if making the change.
Use Eclipse refactoring in sensible chunks to make the changes and deal with the varying defaults.
I have used Eclipse refactoring on quite large changes for fixing old smelly code - nowadays it is fairly robust.
Maybe I'm being naive, but why can't you just overload the method name?
void thing(paramA) {
thing(paramA, THE_DEFAULT_B, THE_DEFAULT_C)
}
void thing(paramA, paramB, paramC) {
// new method
}
Do you really need to change the calling code and the method signature? What I'm getting at is it looks like the added parameters are meant to give you the calling class and method to add to your log data. If the only requirement is just adding the calling class/method to the log data then Thread.currentThread().getStackTrace() should work. Once you have the StackTraceElement[] you can get the class name and method name for the caller.
If the lines you need replaced fall into a small number of categories, then what you need is Perl:
find -name '*.java' | xargs perl -pi -e 's/log\(([^,)]*?)\)/log(\1, "foo", "bar")/g'
I'm guessing that it wouldn't be too hard to hack together a script which would put the classname (derived from the filename) in as the second argument. Getting the method name in as the third argument is left as an exercise to the reader.
Try refactor using intellij. It has a feature called SSR (Structural Search and Replace). You can refer classes, method names, etc for a context. (seanizer's answer is more promising, I upvoted it)
I agree with Seanizer's answer that you want a tool that can parse Java. That's necessary but not sufficient; what you really want is a tool that can carry out a reliable mass-change.
To do this, you want a tool that can parse Java, can pattern match against the parsed code, install the replacement call, and spit out the answer without destroying the rest of the source code.
Our DMS Software Reengineering Toolkit can do all of this for a variety of languages, including Java. It parses complete java systems of source, builds abstract syntax trees (for the entire set of code).
DMS can apply pattern-directed, source-to-source transformations to achieve the desired change.
To achieve the OP's effect, he would apply the following program transformation:
rule replace_legacy_log(s:STRING): expression -> expression
" log(\s) " -> " log( \s, \class\(\), \method\(\) ) "
What this rule says is, find a call to log which has a single string argument, and replace it with a call to log with two more arguments determined by auxiliary functions class and method.
These functions determine the containing method name and containing class name for the AST node root where the rule finds a match.
The rule is written in "source form", but actually matches against the AST and replaces found ASTs with the modified AST.
To get back the modified source, you ask DMS to simply prettyprint (to make a nice layout) or fidelity print (if you want the layout of the old code preserved). DMS preserves comments, number radixes, etc.\
If the exisitng application has more than one defintion of the "log" function, you'll need to add a qualifier:
... if IsDesiredLog().
where IsDesiredLog uses DMS's symbol table and inheritance information to determine if the specific log refers to the definition of interest.
Il fact your problem is not to use a click'n'play engine that will allow you to replace all occurences of
log("some weird message");
by
log(this.getClass(), new Exception().getStackTrace()[1].getMethodName());
As it has few chances to work on various cases (like static methods, as an example).
I would tend to suggest you to take a look at spoon. This tool allows source code parsing and transformation, allowing you to achieve your operation in a -obviously code based- slow, but controlled operation.
However, you could alos consider transforming your actual method with one exploring stack trace to get information or, even better, internally use log4j and a log formatter that displays the correct information.
I would search and replace log( with log(#class, #methodname,
Then write a little script in any language (even java) to find the class name and the method names and to replace the #class and #method tokens...
Good luck
If the class and method name are required for "where did this log come from?" type data, then another option is to print out a stack trace in your log method. E.g.
public void log(String text)
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw, true);
new Throwable.printStackTrace(pw);
pw.flush();
sw.flush();
String stackTraceAsLog = sw.toString();
//do something with text and stackTraceAsLog
}

Can I add and remove elements of enumeration at runtime in Java

It is possible to add and remove elements from an enum in Java at runtime?
For example, could I read in the labels and constructor arguments of an enum from a file?
#saua, it's just a question of whether it can be done out of interest really. I was hoping there'd be some neat way of altering the running bytecode, maybe using BCEL or something. I've also followed up with this question because I realised I wasn't totally sure when an enum should be used.
I'm pretty convinced that the right answer would be to use a collection that ensured uniqueness instead of an enum if I want to be able to alter the contents safely at runtime.
No, enums are supposed to be a complete static enumeration.
At compile time, you might want to generate your enum .java file from another source file of some sort. You could even create a .class file like this.
In some cases you might want a set of standard values but allow extension. The usual way to do this is have an interface for the interface and an enum that implements that interface for the standard values. Of course, you lose the ability to switch when you only have a reference to the interface.
Behind the curtain, enums are POJOs with a private constructor and a bunch of public static final values of the enum's type (see here for an example). In fact, up until Java5, it was considered best-practice to build your own enumeration this way, and Java5 introduced the enum keyword as a shorthand. See the source for Enum<T> to learn more.
So it should be no problem to write your own 'TypeSafeEnum' with a public static final array of constants, that are read by the constructor or passed to it.
Also, do yourself a favor and override equals, hashCode and toString, and if possible create a values method
The question is how to use such a dynamic enumeration... you can't read the value "PI=3.14" from a file to create enum MathConstants and then go ahead and use MathConstants.PI wherever you want...
I needed to do something like this (for unit testing purposes), and I came across this - the EnumBuster:
http://www.javaspecialists.eu/archive/Issue161.html
It allows enum values to be added, removed and restored.
Edit: I've only just started using this, and found that there's some slight changes needed for java 1.5, which I'm currently stuck with:
Add array copyOf static helper methods (e.g. take these 1.6 versions: http://www.docjar.com/html/api/java/util/Arrays.java.html)
Change EnumBuster.undoStack to a Stack<Memento>
In undo(), change undoStack.poll() to undoStack.isEmpty() ? null : undoStack.pop();
The string VALUES_FIELD needs to be "ENUM$VALUES" for the java 1.5 enums I've tried so far
I faced this problem on the formative project of my young career.
The approach I took was to save the values and the names of the enumeration externally, and the end goal was to be able to write code that looked as close to a language enum as possible.
I wanted my solution to look like this:
enum HatType
{
BASEBALL,
BRIMLESS,
INDIANA_JONES
}
HatType mine = HatType.BASEBALL;
// prints "BASEBALL"
System.out.println(mine.toString());
// prints true
System.out.println(mine.equals(HatType.BASEBALL));
And I ended up with something like this:
// in a file somewhere:
// 1 --> BASEBALL
// 2 --> BRIMLESS
// 3 --> INDIANA_JONES
HatDynamicEnum hats = HatEnumRepository.retrieve();
HatEnumValue mine = hats.valueOf("BASEBALL");
// prints "BASEBALL"
System.out.println(mine.toString());
// prints true
System.out.println(mine.equals(hats.valueOf("BASEBALL"));
Since my requirements were that it had to be possible to add members to the enum at run-time, I also implemented that functionality:
hats.addEnum("BATTING_PRACTICE");
HatEnumRepository.storeEnum(hats);
hats = HatEnumRepository.retrieve();
HatEnumValue justArrived = hats.valueOf("BATTING_PRACTICE");
// file now reads:
// 1 --> BASEBALL
// 2 --> BRIMLESS
// 3 --> INDIANA_JONES
// 4 --> BATTING_PRACTICE
I dubbed it the Dynamic Enumeration "pattern", and you read about the original design and its revised edition.
The difference between the two is that the revised edition was designed after I really started to grok OO and DDD. The first one I designed when I was still writing nominally procedural DDD, under time pressure no less.
You can load a Java class from source at runtime. (Using JCI, BeanShell or JavaCompiler)
This would allow you to change the Enum values as you wish.
Note: this wouldn't change any classes which referred to these enums so this might not be very useful in reality.
A working example in widespread use is in modded Minecraft. See EnumHelper.addEnum() methods on Github
However, note that in rare situations practical experience has shown that adding Enum members can lead to some issues with the JVM optimiser. The exact issues may vary with different JVMs. But broadly it seems the optimiser may assume that some internal fields of an Enum, specifically the size of the Enum's .values() array, will not change. See issue discussion. The recommended solution there is not to make .values() a hotspot for the optimiser. So if adding to an Enum's members at runtime, it should be done once and once only when the application is initialised, and then the result of .values() should be cached to avoid making it a hotspot.
The way the optimiser works and the way it detects hotspots is obscure and may vary between different JVMs and different builds of the JVM. If you don't want to take the risk of this type of issue in production code, then don't change Enums at runtime.
You could try to assign properties to the ENUM you're trying to create and statically contruct it by using a loaded properties file. Big hack, but it works :)

Categories

Resources