Can I disable CheckStyle complaints for deprecated methods and classes? - deprecated

I'm maintaining an API that has deprecated some public static fields.
CheckStyle complains loudly about these but I'd rather have it ignore them completely since I've dealt with the problem by marking the fields as deprecated.
Specifically, the library has constants for enumeration (public static final) but the they are not marked as final. CheckStyle will complain about them, but I can't just change them to final without breaking the contract.
My plan is to mark them as deprecated and then delete them later. But marking them as deprecated doesn't remove them from the CheckStyle Report.

I have two options for you:
suppress the warning for each line manually
This approach is less flexible as you have to maintain the suppression configuration each time you are shifting lines. But you can handle each occurrence individually.
<suppress checks="YOURCHECK" files=".*YOURCLASS\.java" lines="YOURLINES"/>
I don't know which check is causing your problem, so you have to replace YOURCHECK with the proper name. YOURCLASS names the java file, which contains the deprecated code, but you can insert .* to apply it to every file. YOURLINES is a comma-separated list of values, where each value is an integer or a range of integers denoted by integer-integer.
use a comment to advise checkstyle to ignore warnings
This solution allows you to deactivate checks for all deprecated members at once. But you have to follow a strict convention. You have to add a #deprecated comment to those members (, what you possibly do already) at the very last position, because this filter has a strict range of lines.
/**
* #deprecated I don't like this anymore.
*/
public static String EXAMPLE = "example";
This solutions needs a change within your configuration file. First you have to add FileContentsHolder as a child to TreeWalker.
<module name="TreeWalker">
...
<module name="FileContentsHolder"/>
...
</module>
Now you may configure the SuppressWithNearbyCommentFilter which is part of the Checker module.
<module name="Checker">
...
<module name="SuppressWithNearbyCommentFilter">
<property name="commentFormat" value=".*deprecated.*"/>
<property name="checkFormat" value=".*"/>
<property name="influenceFormat" value="2"/>
</module>
...
</module>
If you decide to ignore only specific checks, adjust the checkFormat attribute. Or if you want to use another comment, change the commentFormat attribute. But it is very important, that you set influenceFormat to the right value. It tells checkstyle within how many lines after the comment it is due to ignore those checks.
P.S.: Note that the Eclipse CheckStyle plugin removes the FileContentsHolder module, when you change the configuration with its user interface, so you must not use it.

Related

JSR 303 and property file

If I'm using JavaBean Validation 1.0 (JSR 303), and I extract my validation messages into a property file how does Spring become aware of my property file? I know I need to declare a ResourceBundleMessageSource bean but I'm just not clear on how Spring/JavaBean validation becomes aware of this file?
That is main idea of "Framework". It makes lots of magic/setups behind of scenes, so you don't have to write thousands rows of boilerplate code for every project. You can follow with default path/settings (messages.properties) or switch to something else using Configuraton or application.properties file (which also can be changed). But for sure, you don't want to do that in most cases, but you can.

Custom configuration section and app.config

I have some incoming XML documents, I need to check attributes that are configurable in this coming XML and if the attribute is not there in the XML document being checked add it.
The attributes to be checked will be specified in the app.config file
OrderRequest.orderDetails.orderSummary.testCount, I need to check if the testCount attribute exists under the element OrderRequest. If No add it along with it’s value which is also specified in the app.config
INCOMING XML --> Read the attributes from the app.config which need to be checked in this XML file --> Read the incoming XML using XDocument --> If the attribute is not there in the incomg XML add it to the XML.
Any ideas how I can go on this.
I wrote a series of articles on how to create custom configuration sections, which can be found here: http://dotnetslackers.com/articles/CustomConfiguration/default.aspx
Is this a static development task? What I mean is, are you only testing the testCount attribute, or is this generic to say loop through the element type and check all of its attributes to ensure they are there?
Either way, essentially you have to find an element, get its name, access your custom configuration section and lookup the name, use the GetAttribute method to check for attribute existence, and append a new attribute to the element if not.
HTH.

Is it possible to place custom values (properties) in ejb-jar.xml?

1) We are using OpenEJB (both embedded and standalone) with a few deployed EJBs. We would like to specify some simple static business rules and values (example: icon_size=200). Normally, we would put them in a regular properties file (example: rules.properties). Since we shouldn't access the file system directly while inside the application server, is is possible to place those key-value pairs somewhere inside the ejb-jar.xml?
2) If not, is there a standard mechanism to do this? What is it?
Thanks
Use env-entry. In XML:
<env-entry>
<env-entry-name>icon_size</env-entry-name>
<env-entry-type>java.lang.Integer</env-entry-type>
<env-entry-value>200</env-entry-value>
</env-entry>
In annotation:
#Resource(name="icon_size")
int icon_size;
I personally just use a .properties file; well a TernarySearchTree which reads in .properties and .XML files and allows quick retrieval. These files are available at application level. However you can in EJB 3 inject env-entry elements into your EJB. This link explains it in good detail Injection of env entry
There are some OpenEJB extensions here that might be useful.
env-entries.properties
Check out the Custom Injection example which is basically allows the <env-entry> to be specified as plain properties in a META-INF/env-entries.properties file. Nice for collapsing all those name & value pairs into a simple properties file. Internally, we just generate the xml for you using those properties. The default type is always java.lang.String, which is good for this next part.
java.beans.PropertyEditor support
Any <env-entry> which is of <env-entry-type> java.lang.String will automatically have its type converted using the VM java.beans.PropertyEditor for the target type. That's also how Spring does the converting. There are few built-in converters, such as #Resource java.util.Date myDate and #Resource java.io.File myFile

GenericSetup: What does toolset.xml accomplish if ToolInit still has to be called from initialize()

It seems that toolset.xml goes only half way. Ideally it should be able to do away with the ToolInit call in initialize() in __init__.py. But I could not get the tool icon to show in the ZMI without the ToolInit call.
The ToolInit call in the initialize function registers the tool class as something that can be added to OFS based folders in the database - primarily it register a factory for creating instances of the class. This is basically the same that ContentInit does for normal content classes.
Once the class is registered and its meta_type is known, instances of the class can be added to OFS based Folders. GenericSetup steps are responsible for managing persistent content and can be used to add tool instances to the database.
If we wanted to avoid the code in the initialize function, we would need to create some custom ZCML directives instead and use these in a configure.zcml to register the type and its factory. Dexterity has gone this route, but its not available for Archetypes based content types or general classes like tools.
The goal of toolset.xml is to instantiate tools into the database. It can also be used to remove tools; this is very useful in upgrade steps for example.
Example toolset.xml:
<?xml version="1.0"?>
<tool-setup>
<required tool_id="portal_foo" class="dotted.path.to.FooTool" />
<forbidden tool_id="portal_spam" />
</tool-setup>
This example toolset.xml would instantiate the FooTool class as portal_foo in it's context, and remove any object with id portal_spam if present.
Note that you can use a toolset.xml in any GenericSetup profile, not just in the package that defines the tool in the first place, for example, in general policy packages for a site you develop.

FxCop Suppression

I have a control on an Asp.Net page, on that page is a control with the name "PaReq" that violates the rule:
CA1704:IdentifiersShouldBeSpelledCorrectly
I've used suppression before I'm not sure how to suppress this error since it is defined in the generated file.
I could do this in a custom dictionary but that would mean all the developers would need to keep sync a copy of this dictionary which is something I'd rather avoid if possible. Also that the term "PaReq" is only used in this project.
How and where would I apply the suppression?
I keep a custom dictionary per project which contains specific words for that project.
That dictionary file is in my SourceControl repository, so other developers can use it (and add new words) as well.
AFAIK you can add a suppression at the Assembly level although I've not used it in anger! You'd end up with some false negatives if you do that. (i.e. suppress all of the incidents that break this rule).
[SuppressMessage("Microsoft.Design", "CA1704",Scope="Assembly")]
I think the custom dictionary in source control is the best option, alternatively you could keep the FxCop project in source control.

Resources