How to create xPath for java code in PMD rules in Eclipse - pmd

I want to create an xPath to add PMD rule to avoid the below code:
String variable = null;
variable.equals("String");
which throws a null pointer exception.

Related

DB2:LUW:Windows(OS) Unable to Call Java UDF

I am following the steps outlined in the URL
https://community.ibm.com/community/user/hybriddatamanagement/viewdocument/generating-universally-unique-ident?CommunityKey=ea909850-39ea-4ac4-9512-8e2eb37ea09a&tab=librarydocuments to call Java UDF from DB2 database.
import java.util.UUID; // for UUID class
public class UUIDUDF
{
public static String randomUUID()
{
return UUID.randomUUID().toString();
}
}
I am able to generate JAR and I called
call sqlj.install_jar('file:///C:/Users/XXX/Desktop/UDF/UUIDUDF.jar', 'UUIDUDFJAR')
and I am able to find Jar being deployed at
"C:\ProgramData\IBM\DB2\DB2COPY1\function\jar\DB2ADMIN"
I restarted database manager using db2stop and db2start command..when i call the function I am getting error
"Java‬‎ ‪stored‬‎ ‪procedure‬‎ ‪or‬‎ ‪user‬‎-‪defined‬‎ ‪function‬‎
‪‬‎"‪DB2ADMIN.RANDOMUUID"‬‎,‪‬‎ ‪specific‬‎ ‪name‬‎
‪‬‎"‪SQL200817125101637"‬‎ ‪could‬‎ ‪not‬‎ ‪load‬‎ ‪Java‬‎ ‪class‬‎
‪‬‎"‪C:\PROGRAMDATA\IBM\DB2\DB2COPY1"‬‎,‪‬‎ ‪reason‬‎ ‪code‬‎
‪‬‎"‪"‬‎.‪‬‎.‪‬‎ ‪SQLCODE‬‎=‪‬‎-‪4304‬‎,‪‬‎ ‪SQLSTATE‬‎=‪42724‬‎,‪‬‎
‪DRIVER‬‎=‪4‬‎.‪19‬‎.‪56"...
I created the function using below code
CREATE OR REPLACE FUNCTION RANDOMUUID()
RETURNS VARCHAR(36)
LANGUAGE JAVA
PARAMETER STYLE JAVA
NOT DETERMINISTIC NO EXTERNAL ACTION NO SQL
EXTERNAL NAME 'UUIDUDFJAR:UUIDUDF.randomUUID' ;
But when I generate DDL for my function in Db2 instance I am missing Jar reference in External name (EXTERNAL NAME 'UUIDUDF.randomUUID')
CREATE FUNCTION "DB2ADMIN"."RANDOMUUID" ()
RETURNS VARCHAR(36 OCTETS)
SPECIFIC "DB2ADMIN"."SQL200817125101637"
NO SQL
NO EXTERNAL ACTION
CALLED ON NULL INPUT
DISALLOW PARALLEL
LANGUAGE JAVA
EXTERNAL NAME 'UUIDUDF.randomUUID'
FENCED THREADSAFE
PARAMETER STYLE JAVA
NOT SECURED;
Could you please help me understand what i am missing here?
Thank you,
Pavan.

Malformed schema version 1,000

I have a problem whereby the generator is producing a malformed schema version. Does anyone know why this is the case?
public class MyDaoGenerator {
public static void main(String[] args) throws Exception {
Schema schema = new Schema(1000, "com.mydomain");
...
}
}
OUTPUT
public class MyDaoMaster extends AbstractDaoMaster {
public static final int SCHEMA_VERSION = 1,000;
...
}
The fix will be available today in maven central. Update your greenDAO generator version to 1.3.1.
Probably this is a error coming from freemarker that occurs in greendao-generator <= 1.3.0.
Freemarker is used by greendao-generator to generate the *.java-files. If not configured freemarker uses a locale-sensitive output (for numbers).
See the Freemarker Manual
If you want to use greendao with schema-version > 999 you probably have to add this line in greendao-generator-project rebuild it and use the modified greendao-generator.jar. This causes all numbers used in the templates to be without grouping signs.
File DaoGenerator.java (line 63):
config.setNumberFormat("#*");
Another way would be to manually edit the schema version in your generated file. But you would have to repeat this everytime you regenerate your code.

How to retrieve IProblem instances from current editor?

I'm writing an Eclipse plugin, and I'd like to retrieve all instances of IProblem associated with the currently open text editor. I've tried the following:
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
IEditorPart editor = window.getActivePage().getActiveEditor();
if (!(editor instanceof AbstractTextEditor)) {
return null;
}
ITextEditor textEditor = (ITextEditor)editor;
IDocumentProvider docProv = textEditor.getDocumentProvider();
IDocument doc = docProv.getDocument(editor.getEditorInput());
ASTParser parser = ASTParser.newParser(AST.JLS4);
parser.setSource(doc.get().toCharArray());
CompilationUnit cu = (CompilationUnit) parser.createAST(null);
return cu.getProblems();
However, when I run it against the following code in the editor:
import java.io.Serializable;
public class TestClass implements Serializable {
/**
*
*/
}
It doesn't find any problems. I would expect it at least finds a MissingSerialVersion problem. Perhaps I'm parsing the file incorrectly, which might cause this issue. I feel like there should be a way to get the CompilationUnit from the editor directly?
Update:
If I add a syntax error to the source file and then invoke the plugin, it reports the syntax problem, but not the missing serial version UID. I suspect this is some kind of configuration issue where warnings aren't being reported.
2nd Update:
This isn't just restricted to the MissingSerialVersion problem. Anything that is a warning, and not an error, is not found by this method. If I change the problem that I want to see to an error in the compiler settings (or even by passing in additional options to the parser making it an error instead of a warning), I still get no joy from the getProblems() method with respect to warnings. (It shows actual errors just fine, e.g. if I put an unrecognized symbol in my source code).

How do I avoid a PMD CloseResource violation?

I'm modifying my application code in order to respect the pmd rules. I had a Close Resource error in this code:
Connection c = DataSourceUtils.getConnection(dataSource);
Statement request = null;
try {
request = c.createStatement();
request.execute(loadDataRequest);
} catch (SQLException e) {
dataLogger.error(e);
throw e;
}
So I searched and found an apache utility for avoiding it: DButils
My code became like this
Connection c = DataSourceUtils.getConnection(dataSource);
Statement request = null;
try {
request = c.createStatement();
request.execute(loadDataRequest);
} catch (SQLException e) {
dataLogger.error(e);
throw e;
} finally {
DbUtils.closeQuietly(request);
DbUtils.closeQuietly(c);
}
However, I'm still having the PMD alert in eclipse and sonar reports! Do you have any idea how to fix that permanently?
You can set the closeTargets property of the CloseResource PMD rule. When PMD finds similar method names as in the closeTargets property, it won't fire the warning message:
<properties>
<property name="types" value="Connection,Statement,ResultSet"/>
<property name="closeTargets" value="closeQuietly, closeConnection, close"/>
</properties>
Alternatively, you can use SourceMeter, which includes this configuration natively.
The problem is that PMD doesn't know closeQuietly() closes the connection. And it isn't that smart because if your method was named close() you'd have the same problem. And since it is a Java rule, it isn't easy to change the implementation because then you'd have to repackage the Eclipse and Sonar PMD plugins to recognize your copy of the rule.
Your options:
1) Add //NOPMD comment to suppress
2) Refactor the code to get/close the connection in a superclass so you only have it once.
PMD defines a parameter for this rule called closeTargets. By default, this parameter is set to close method. You can change it for specifying DbUtils.closeQuietly

When writing Eclipse plugins, what is the correct way for checking if an IEditorPart is a Java editor?

I am writing Eclipse plugins for Java, and have the following problem:
Given an IEditorPart, I need to check if it is a java editor.
I could do (IEditor instanceof JavaEditor),
but JavaEditor is an org.eclipse.jdt.internal.ui.javaeditor.JavaEditor,
which falls under the JDT's "internal" classes.
Is there a smarter and safer way to do this? I'm not sure why there is no non-internal interface for this.
You should test the id of the IEditorPart:
private boolean isJavaEditor(IWorkbenchPartReference ref) {
if (ref == null) {
return false; }
String JavaDoc id= ref.getId();
return JavaUI.ID_CF_EDITOR.equals(id) || JavaUI.ID_CU_EDITOR.equals(id);
}
Testing the instance was only needed in eclipse3.1.
alt text http://blogs.zdnet.com/images/Burnette_DSCN0599.JPG
JavaUI is the main access point to the Java user interface components. It allows you to programmatically open editors on Java elements, open a Java or Java Browsing perspective, and open package and type prompter dialogs.
JavaUI is the central access point for the Java UI plug-in (id "org.eclipse.jdt.ui")
You can see that kind of utility function ("isJavaEditor()") used for instance in ASTProvider.
The mechanism of identification here is indeed simple String comparison.
Anyway, you are wise to avoid cast comparison with internal class: it has been listed as one of the 10 common errors in plugins development ;) .
One strategy might be to use JavaUI.getEditorInputJavaElement(IEditorPart):
// given IEditorPart editor
IJavaElement elt = JavaUI.getEditorInputJavaElement(editor.getEditorInput());
if (elt != null) {
// editor is a Java editor
}
The method returns null if the editor input is not in fact a Java element.