Highlight field in source code pane of Findbugs UI - java

I'm using a class that extends BytecodeScanningDetector to check for some problematic fields in a class.
After detecting whether the field is problematic, I add it to the bug report like below:
Once I run findbugs, it identifies the bug, lists it in the left pane, but does not highlight the corresponding source line.
Any hints/help on this will be very appreciated.
public void visit(Field f) {
if (isProblematic(getXField())) {
bugReporter.reportBug(new BugInstance(this,
tBugType,
HIGH_PRIORITY)
.addClass(currentClass) //from visit(JavaClass)
.addField(this));
}
}
public void visit(JavaClass someObj) {
currentClass = someObj.getClassName();
}
P.S. I tried posting this on the findbugs list but... no joy.

Unfortunately the java class file format does not associate line numbers with fields. The 'Line number table' attribute is an attribute of methods only. And so you can't do what you want to do.

Related

Can two exported classes in different packages interact without making their members public?

I stumbled across this issue while working on a library and have been trying to find a solution for hours now. I'm not sure if this is even possible or not. I have a module, com.gui, which contains a package called com.gui.components, com.gui.constraints and com.gui.animation. I want to implement text-based components and set up a package called com.gui.text.
I have a Font class inside of that package (com.gui.text) which should be public so the user can pass it into one of the text components. However, I'm struggling with how I transfer data like the texture id over to the text component without making the variable public (or implement a public getter). I messed around with not exporting the text package and extending the Font class inside of the text component class but this seemed like a suboptimal solution and I don't really like the feel of it.
Here is the hierarchy of my project visually:
src/com.gui
--component -> exported
----UITextComponent
--text
----mesh
------Texture
----font -> exported
------Font // contains a Texture object which should stay invisible to the user
--XXX // other packages
Am I missing anything obvious here or is this impossible to do currently?
If you want to go crazy you can do the following (this is not recommended)
in your Font class have a method like this:
public class Font {
private String textureId;
...
public void setTextureId(Texture texture) throws Exception {
if (texture == null) return;
Field field = texture.getClass().getDeclaredField("textureId");
if (field == null) return;
field.setAccessible(true);
textureId = String.valueof(texture.get(font));
}
}
Again this is not the recommended way but it is a way to get what you want with the layout you have. Also you'd have to add some more validation checks.

GWT.create(clazz) "generics" approach

I have to develop an "generic" wigdet for a GWT/GXT project and to do so I need to create an instance of an object which type is unknown. I found an approach that works perfectly in dev mode but as soon as I try to compile my project and deploy it I get an Only class literals may be used as arguments to GWT.create() error.
Here is a sample of what I do:
public class GenericEditableGrid<M> extends Grid<M>{
private final ToolBar toolBar = new ToolBar();
private final TextButton newItemButton = new TextButton();
protected GridInlineEditing<M> editing;
private final Class<M> clazzM;
public GenericEditableGrid(Class<M> parametrizedClass, String gridTitle, ListStore<M> listStore, ColumnModel<M> cm) {
super(listStore, cm);
clazzM = parametrizedClass;
// ... then I create my widget
bind();
}
private void bind(){
newItemButton.addSelectHandler(new SelectEvent.SelectHandler() {
#Override
public void onSelect(SelectEvent selectEvent) {
editing.cancelEditing();
// it is the folliwing line which is the problem obviously
M element = GWT.create(clazzM);
getStore().add(0, element);
int index = 0;
editing.startEditing(new Grid.GridCell(getStore().indexOf(element), index));
}
});
}
}
And this is how I use it in my subclasses:
super(InternationalString.class, gridTitle, new ListStore<InternationalString>(isprops.key()), buildColumnModel());
Basically, I would like to know what the problem is exactly with this approach and eventually how I should do to make it well.
Please note that my concern is not just to make it work, but more to do it the right way. As I could just avoid the problem using an abstract method which would handle the GWT.create() method in the daughter classes. But this is not the design I want, it just doesn't look right.
What I don't get also is what's the difference between doing this:
MyClass e = GWT.create(MyClass.class);
and:
Class<MyClass> clazz=MyClass.class;
MyClass e = GWT.create(clazz);
Because as far as I am concerned I think this is basically what I am doing and it looks like the same thing. Isn't it?
There's a well-worded explanation in this forum:
As the error message indicates, only class literals may be passed to the GWT.create method. The reason for this is that all GWT.create calls are basically turned into constructors at compile time, using the deferred binding rules for your module. As a result, all classes must be decided at compile time - your code requires that the at runtime the class is decided. This is too late, and so cannot be compiled.
GWT is not proper java, and so cannot be always treated as java. This is one such example of things that cannot be done in gwt. ...
What is it you are trying to do? Either you are making it far more complicated than it needs to be, or you need to write a generator to do it instead of taking this approach.

SmartGWT switch CSS class at runtime

I would like to know if and how it is possible working on SmartGWT to change the class associated to an element at runtime.
Take for example the underlying code that renders contents inside a div, I would like to know if and how I can modify at runtime the css class associated to the div.
HTMLFlow productInfo = new HTMLFlow(productInfoHtml);
productInfo.setStyleName("loginProductInfo");
productInfo.setHeight(13);
productInfo.setMargin(5);
Note: I'm using Smart GWT version 4.0
I have not used SmartGWT, but if HTMLFlow is a widget you can use GQuery for changing classes on fly, or css' rules.
Something like:
if (something) {
GQuery.$(productInfo).css("width", "70px");
else{
GQuery.$(productInfo).css("width", "30px");
}
About the css classes:
if (something) {
GQuery.$(loginProductInfo).removeClass("loginProductInfo");
} else {
GQuery.$(loginProductInfo).addClass("secondLoginProductInfoCss");
}
In fact, the solution is simpler than I had thought.
It is sufficient to keep a reference to the object of which you want to change the css class, then invoke again the method setStyleName.
Keep a reference to the widget (in our case productInfo) as a field of the original class, then change the css assignment when necessary.
So in the end the object productInfo becomes a field in the main class and then we can change the css assignment simply by invoking again the method setStyleName passing the new css class.
The object productInfo becomes a field in the main class
protected HTMLFlow productInfo = new HTMLFlow();
The code of the question example is modified as in the snippet
productInfo.setContents(productInfoHtml);
productInfo.setStyleName("login-product-info");
productInfo.setHeight(16);
productInfo.setWidth100();
Now I can change the css class assigned to the widget productInfo when the event onMouseOver occurs on the object forgot.
forgot.addMouseOverHandler(new MouseOverHandler() {
#Override
public void onMouseOver(MouseOverEvent event) {
productInfo.setStyleName("my-new-style");
}
});

refractoring java bytecode

I am trying to replace a certain class file with my own in an obfuscated jar. The original class file has methods named "new" and "null" so a quick decompile + compile doesn't work.
I tried compiling and using jbe to add new methods named "new" that relayed everything to "new_symbol" functions (with new_symbol beeing the decompiled version of the original "new" function).
This did not work. ("code segment has wrong length in class file")
Does anyone know of a way to refractor method names in class files? And if that isn't possible, a way to reliably create those "proxy functions"?
From google I learned that there are about 1000+ different backend library's but only jbe as fronted for bytecode editing?
EDIT:
Let me try to illustrate it.
Let's say that there is a jar file with a class that provides a function that logs everything you give it to a database.
I'd like to replace that class file with my own, and it should not only log everything to a database, but also print whatever data it gets to the command line.
The problem is, that class file was obfuscated and the obfuscator gave it public method names like "new" or "null". If you try:
public class replacement{
public void new (string data){
...
}
}
And compile that, you get compilation errors.
My idea was to create this :
public class replacement{
public void newsymbol (string data){
...
}
}
And use a bytecode editor to create a function named "new" that calls "newsymbol" with the same arguments. (but I get "code segment wrong length" and other errors going down this route.
My question therefore could be better frased as "give me a way to intercept calls to a class file who's public methods are named "new" "null" "weird_unicode_symbols""....
Scala allows you to use identifiers in names if you surround them by `.
class f{
def `new`():Int = {
return 3
}
}
jd-gui output
import scala.reflect.ScalaSignature;
#ScalaSignature(bytes=/* snip */)
public class f
{
public int jdMethod_new()
{
return 3;
}
}
I assume that jdMethod_ is prefixed in order to make the identifier valid. There is no jdMethod_ when looking at the class file using a hex editor.
However, this does have a flaw when you need to use public fields; scalac never generates public fields, it always makes them private and creates accessors.
So, what turned out to be the best solution for me was to use a hex editor (as suggested by user60561).
Apparantly, the name of every function and field is only saved once in the class file so if you use names with the same amount of bytes you can hexedit your way to victory.
For me it came down to replacing "new" by "abc" and every strange unicode character with a two-char sequence.
Thanks for all the suggestions.

can this be considered duplicate code?

This is my first post here, recently i have been working with JSF2.0 with primefaces. we have this requirement to export PDF in our application. initially we used primefaces default dataexporter tag. but the format was simply terrible. so, i used itext to generate PDF. we have like upto 15 datatables in our app, and all of them require PDF exporting. i have created a method called generatePDF which creates the PDF using Itext for all the tables.
Interface PDFI {
public void setColNames();
public void setColValues();
public void setContentHeader();
}
Class DataEx {
public void generatePDF(ActionEvent event) {
// generate pdf...
}
}
consider i have a Datatable A in the view
Datatable A ...
bean behind this datatable..
Class BeanA implements PDFI {
//implemented methods
}
}
Class BeanB implements PDFI {
//implemented methods
}
and behind another datatable B, i do the same thing as above ..
so, my question here is, is this considered duplicate code ?? and also, is this the efficient way to do this.
any help is appreciated.
thanks ina dvance
Rule of thumb that I use before re-factoring duplicate code- when part of the code in one place have a bug- are you need to change the other one to? cause you probably will forget
in your case, it's look like you have duplicate code block. I'll consider add the require parameters to generatePDF so it'll do all work in one place.

Categories

Resources