Checkstyle - limit method exit points to five - checkstyle

We are allowing up to five return statements pro method. Is there a check rule that would give me a waring if method has more then five return statements?
ps. please do not start discussion on "why would I allow more then one exit point" ;)

Yes, there is the ReturnCount check. Configure like this:
<module name="ReturnCount">
<property name="max" value="5"/>
</module>
You can also give it the names of methods that are ignored by the check (see linked docs).

Related

Wildfly json-formatter class name metadata

Is it possible to configure a non-static value for the metadata field in the wildly json-formatter?
I didn't find anything about it in the wildfly documentation- it only has a simple static field example (meta-data=[#version=1])
For example, I would like to have a field "simpleClassName" - The class of the code calling the log method.
I also tried to use a similar syntax to pattern-formatter(example below) but it doesn't work
<formatter name="JSON">
<json-formatter>
<meta-data>
<property name="simpleClassName" value="%c{1}"/>
</meta-data>
</json-formatter>
</formatter>
No the meta-data is only static information. However what you're looking for seems to be the details of the caller. Note that this is an expensive operation should you should use it with caution. What you'd want to do is change the print-details to true. In CLI it would be something like:
/subsystem=logging/json-formatter=JSON:write-attribute(name=print-details, value=true)

Is there a checkstyle rule for forcing every field in a class to have an annotation?

We want to make compliance easy and for FedRAMP want something like this on all fields in our database objects
#FedRamp(confidentiality=LOW, integrity=MODERATE, availability=HIGH)
We want checkstyle to break the builds if people add data and forget to add these on 'any' field in the *Dbo.java class. Then, we can generate the FedRAMP compliance on each data item (and therefore the entire system). We run checkstyle on every class but only want this rule run on classes ending in *Dbo.java. Is this possible where we import some already existing checkstyle rule or plugin and add the class name filter to it?
thanks,
Dean
To report violations for such cases for any classes, you can use MatchXpathCheck (you need checkstyle 8.39+)
Config will look like:
<?xml version="1.0"?>
<!DOCTYPE module PUBLIC
"-//Checkstyle//DTD Checkstyle Configuration 1.3//EN"
"https://checkstyle.org/dtds/configuration_1_3.dtd">
<module name = "Checker">
<module name="TreeWalker">
<module name="MatchXpath">
<property name="id" value="fedramp_check"/>
<property name="query" value="//CLASS_DEF/OBJBLOCK/VARIABLE_DEF/MODIFIERS/ANNOTATION/IDENT[not(#text='FedRamp')]"/>
<message key="matchxpath.match"
value="Field should have 'FedRamp' annotation."/>
</module>
</module>
</module>
This will report violations like this:
$ cat Test.java
class Test {
#FedRamp(confidentiality=LOW, integrity=MODERATE, availability=HIGH)
private int withAnnotation = 11; // no violation
#Fed(confidentiality=LOW, integrity=MODERATE, availability=HIGH)
private int without = 11; // violation
#NotNull
int without = 11; // violation
}
$ java -jar checkstyle-8.42-all.jar -c config.xml Test.java
Starting audit...
[ERROR] C:\workdir\Test.java:6:4: Field should have FedRamp annotation. [fedramp_check]
[ERROR] C:\workdir\Test.java:9:4: Field should have FedRamp annotation. [fedramp_check]
Audit done.
Checkstyle ends with 2 errors.
Second part of your question - narrow execution only to specific classes - can be solved in several ways.
Use a bit different xpath to filter class names (not files, since there can be many classes in single file)
<property name="query"
value="//CLASS_DEF[./IDENT[ends-with(#text,
'Dbo')]]/OBJBLOCK/VARIABLE_DEF/MODIFIERS/ANNOTATION/IDENT[not(#text='FedRamp')]"/>
Use BeforeExecutionExclusionFileFilter - it is filter for whole config and will work ok only if you have a separate config only for checking annotation thing.
Suppress violations for this check (by id, for example) for other class files, see doc

Checkstyle CustomImportOrder more than 3 definable groups?

My company's Java import order standard would require more than the three definable groups that I see in checkstle, which are STANDARD_JAVA_PACKAGE, THIRD_PARTY_PACKAGE, and SPECIAL_IMPORTS. My question has two parts:
Is there a way to define more custom regular expressions, or use regexes directly in the VALUE for customImportOrderRules?
Can I do this at all, since com.our_company is supposed to come after all the other com. imports.
The import rules are approximately
Static imports
java.*
javax.*
com.* EXCEPT our company
nthing.*
org.*
pthing.*
com.mycompany.*
anything else
Since we also enforce blank lines between groups, I can't combine 5, 6, and 7 into one and depend on sort order to keep things clean. Worst case is that we already have this defined in Intellij and just have to remember to auto-order imports every time.
Answering your questions:
According to this ticket it looks like there is no way to achieve what you need using just CustomImportOrder. Instead you should use ImportOrder, complete example below.
It is possible using ImportOrder
This should work for your case:
<module name="ImportOrder">
<property name="option" value="top"/>
<property name="groups" value="/^java\./,javax,/^com\.(?!mycompany)/,nthing.org,pthing,com.mycompany"/>
<property name="ordered" value="true"/>
<property name="separated" value="true"/>
<property name="separatedStaticGroups" value="true"/>
<property name="sortStaticImportsAlphabetically" value="true"/>
</module>
Small clarification. Inside the groups property there are two groups /^com\.(?!mycompany)/ and com.mycompany, first one is the regexp that use negative look-ahead, second one is just common prefix string for the import.

OpenDDS and OpenSplice interoperability

I have two programs, one using OpenSplice 6.7.1 and the other using OpenDDS 3.10.
They are both using RTPS as protocol, the same domain id and the destination port (I verified using wireshark).
The problem is that they are not communicating.
I don't know if I am doing anything wrong with the config... I am using the basic config for OpenDDS with RTPS and for OpenSplice I used the provided ospl.xml after changing the domain ID.
Here are my config files.
For OpenDDS:
[common]
DCPSGlobalTransportConfig=$file
DCPSDefaultDiscovery=DEFAULT_RTPS
[transport/the_rtps_transport]
transport_type=rtps_udp
For OpenSplice:
<OpenSplice>
<Domain>
<Name>ospl_sp_ddsi</Name>
<Id>223</Id>
<SingleProcess>true</SingleProcess>
<Description>Stand-alone 'single-process' deployment and standard DDSI networking.</Description>
<Service name="ddsi2">
<Command>ddsi2</Command>
</Service>
<Service name="durability">
<Command>durability</Command>
</Service>
<Service name="cmsoap">
<Command>cmsoap</Command>
</Service>
</Domain>
<DDSI2Service name="ddsi2">
<General>
<NetworkInterfaceAddress>AUTO</NetworkInterfaceAddress>
<AllowMulticast>true</AllowMulticast>
<EnableMulticastLoopback>true</EnableMulticastLoopback>
<CoexistWithNativeNetworking>false</CoexistWithNativeNetworking>
</General>
<Compatibility>
<!-- see the release notes and/or the OpenSplice configurator on DDSI interoperability -->
<StandardsConformance>lax</StandardsConformance>
<!-- the following one is necessary only for TwinOaks CoreDX DDS compatibility -->
<!-- <ExplicitlyPublishQosSetToDefault>true</ExplicitlyPublishQosSetToDefault> -->
</Compatibility>
</DDSI2Service>
<DurabilityService name="durability">
<Network>
<Alignment>
<TimeAlignment>false</TimeAlignment>
<RequestCombinePeriod>
<Initial>2.5</Initial>
<Operational>0.1</Operational>
</RequestCombinePeriod>
</Alignment>
<WaitForAttachment maxWaitCount="100">
<ServiceName>ddsi2</ServiceName>
</WaitForAttachment>
</Network>
<NameSpaces>
<NameSpace name="defaultNamespace">
<Partition>*</Partition>
</NameSpace>
<Policy alignee="Initial" aligner="true" durability="Durable" nameSpace="defaultNamespace"/>
</NameSpaces>
</DurabilityService>
<TunerService name="cmsoap">
<Server>
<PortNr>Auto</PortNr>
</Server>
</TunerService>
</OpenSplice>
What am I doing wrong ?
Multi-vendor interoperability has been demonstrated repeatedly at OMG events but not recently, so maybe a regression has happened with/in either of the products.
Your OpenSplice configuration is (apart from domainId which should match the one used in your application where typically users use DDS::DOMAIN_ID_DEFAULT to indicate they want to use the ID as specified in the configuration as pointed to by the OSPL_URI environment variable) a proper default configuration. I'm sure you are aware that the AUTO setting of the to-be-used interface/IP-address is a potential source-of-confusion if you use multi-homed machines.
So next would be to look at both (DDSI)traces and/or wireshark captures and see if you spot DDSI wire-frames for both Vendors (1.2 for PrismTech, 1.3 for OCI).
When for instance there's no sign of vendor-1.3 being identified in OpenSplice DDSI-traces then that suggests there's still some 'fundamental' communication issues.
Note that at these OMG-events we typically used the (for us 'bundled') iShapes example on domain '0' and module-less IDL topic-type specification to verify interoperability, so it it doesn't work for your application that's something worth trying too (and check/use wireshark in combination with that example too)
I'll also keep watching the community-forum for new information on this ..

byte? doesn't work as a parameter type in an iBatis parameterMap

According to the iBatis docs, using "byte?" as a type value should work in parameterMap. I'm using DataMapper version 1.6.1.
The error message I'm getting is: Could not load type from string value 'byte?'.
It's a configuration exception which shows this information in the stack trace:
The error occurred while loading
SqlMap.
Check the parameter mapping typeHandler attribute '' (must be a
ITypeHandlerCallback implementation).
The error occurred in [sqlMap embedded="MyApp.Data.Config.Rate.xml, MyApp" Mxmlns="http://ibatis.apache.org/dataMapper"]
Check the Rate.InsertParams.]
Here's the XML block for Rate.InsertParams:
<parameterMaps>
<parameterMap id="InsertParams" class="RateCategory">
<parameter property="RateId" column="RateId" type="int"/>
<parameter property="Name" column="Name" type="string" size="32"/>
<parameter property="Charge" column="Charge" type="decimal?"/>
<parameter property="ChargeTypeId" column="ChargeTypeId" type="byte?"/>
</parameterMap>
Any ideas why it won't work?
As far as I can tell, there isn't any "byte?" alias. Here is a proof.
You can also figure out what is for sure supported and what not from this file.
"Code never lies." Documentation does :(
After lots of asking and posting, the final answer is that the docs are wrong.
The official iBatis.Net forum had this to say:
"You have a few options:
just use type="byte"
don't specify the type
byte? is syntactic sugar System.Nullable`1[[System.Byte]]"
Which is basically what I had already figured out myself. int? works but byte? doesn't. Neither does short? or long?.
The docs need to be fixed.