You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@forrest.apache.org by Diwaker Gupta <di...@apache.org> on 2005/08/25 09:23:00 UTC

Automated formatting of Java files

Following up with our earlier discussion on whitespace cleanup, I have added 
two "jalopied" Java source files in the test-whitespace directory. I would 
urge the devs to take a look at both the original Java file and the formatted 
file and see if it suits their tastes.

Please post any comments and suggestions in this thread. Bear in mind that 
with Jalopy, almost *anything* is configurable. So if you find anything, 
however small/trivial it is that you don't like, please let me know. Its a 
great tool, so lets make the best use of it. 

To see exactly what is configurable, take a look at:
http://jalopy.sourceforge.net/manual.html

As an example of things that it can do:

o automatically add/update headers (the Apache license, for instance. In 
future if we have to change one line in the license, Jalopy will 
automatically delete the old one and insert the new one)

o sorting/group of imports, methods, variables

o alignments, braces, spaces -- you name it!

o automatic javadoc generation, correction

-- 
Web/Blog/Gallery: http://floatingsun.net

Re: Automated formatting of Java files

Posted by Addi <ad...@rocktreesky.com>.
Diwaker Gupta wrote:

>>Can you tell me what settings you are using so I can try to match them
>>with my Jalopy plugin without thinking too hard?  :)
>>    
>>
>
>Attaching the convention file. I use it with the Ant plugin. But you
>can use whatever you want.
>  
>
Ugh.  I am using jEdit and it took me a while to figure out that the 
standard jEdit plugin through the plugin manager uses an older version 
of Jalopy and so couldn't read your file properly.  I got a newer plugin 
from the Jalopy site and successfully imported your file but now I 
always get a StackOverflowError when I try to clean a file.  I 
understand that to mean that it is recursing too deeply but I have no 
idea what that really means or how to fix it.  I'm using JDK 1.5.0_04.  
Any suggestions other than trying another app?

- Addi

>  
>
>------------------------------------------------------------------------
>
><?xml version="1.0" encoding="UTF-8"?>
><jalopy>
>    <general>
>        <compliance>
>            <version>14</version>
>        </compliance>
>        <style>
>            <description>Forrest Coding Conventions</description>
>            <name>Forrest</name>
>        </style>
>    </general>
>    <inspector>
>        <enable>false</enable>
>        <naming>
>            <classes>
>                <abstract>[A-Z][a-zA-Z0-9]+</abstract>
>                <general>[A-Z][a-zA-Z0-9]+</general>
>            </classes>
>            <fields>
>                <default>[a-z][\w]+</default>
>                <defaultStatic>[a-z][\w]+</defaultStatic>
>                <defaultStaticFinal>[a-zA-Z][\w]+</defaultStaticFinal>
>                <private>[a-z][\w]+</private>
>                <privateStatic>[a-z][\w]+</privateStatic>
>                <privateStaticFinal>[a-zA-Z][\w]+</privateStaticFinal>
>                <protected>[a-z][\w]+</protected>
>                <protectedStatic>[a-z][\w]+</protectedStatic>
>                <protectedStaticFinal>[a-zA-Z][\w]+</protectedStaticFinal>
>                <public>[a-z][\w]+</public>
>                <publicStatic>[a-z][\w]+</publicStatic>
>                <publicStaticFinal>[a-zA-Z][\w]+</publicStaticFinal>
>            </fields>
>            <interfaces>[A-Z][a-zA-Z0-9]+</interfaces>
>            <labels>\w+</labels>
>            <methods>
>                <default>[a-z][\w]+</default>
>                <defaultStatic>[a-z][\w]+</defaultStatic>
>                <defaultStaticFinal>[a-z][\w]+</defaultStaticFinal>
>                <private>[a-z][\w]+</private>
>                <privateStatic>[a-z][\w]+</privateStatic>
>                <privateStaticFinal>[a-z][\w]+</privateStaticFinal>
>                <protected>[a-z][\w]+</protected>
>                <protectedStatic>[a-z][\w]+</protectedStatic>
>                <protectedStaticFinal>[a-z][\w]+</protectedStaticFinal>
>                <public>[a-z][\w]+</public>
>                <publicStatic>[a-z][\w]+</publicStatic>
>                <publicStaticFinal>[a-z][\w]+</publicStaticFinal>
>            </methods>
>            <packages>[a-z]+(?:\.[a-z]+)*</packages>
>            <parameters>
>                <default>[a-z][\w]+</default>
>                <final>[a-z][\w]+</final>
>            </parameters>
>            <variables>[a-z][\w]*</variables>
>        </naming>
>        <tips>
>            <adhereToNamingConvention>false</adhereToNamingConvention>
>            <alwaysOverrideHashCode>false</alwaysOverrideHashCode>
>            <avoidThreadGroups>false</avoidThreadGroups>
>            <declareCollectionComment>false</declareCollectionComment>
>            <dontIgnoreExceptions>false</dontIgnoreExceptions>
>            <dontSubstituteObjectEquals>false</dontSubstituteObjectEquals>
>            <neverDeclareException>false</neverDeclareException>
>            <neverDeclareThrowable>false</neverDeclareThrowable>
>            <neverInvokeWaitOutsideLoop>false</neverInvokeWaitOutsideLoop>
>            <neverReturnZeroArrays>false</neverReturnZeroArrays>
>            <neverUseEmptyFinally>false</neverUseEmptyFinally>
>            <obeyContractEquals>false</obeyContractEquals>
>            <overrideToString>false</overrideToString>
>            <referToObjectsByInterface>false</referToObjectsByInterface>
>            <replaceStructureWithClass>false</replaceStructureWithClass>
>            <stringLiterallI18n>false</stringLiterallI18n>
>            <useInterfaceOnlyForTypes>false</useInterfaceOnlyForTypes>
>            <wrongCollectionComment>false</wrongCollectionComment>
>        </tips>
>    </inspector>
>    <internal>
>        <version>6</version>
>    </internal>
>    <messages>
>        <priority>
>            <general>30000</general>
>            <parser>30000</parser>
>            <parserJavadoc>30000</parserJavadoc>
>            <printer>30000</printer>
>            <printerJavadoc>30000</printerJavadoc>
>            <transform>30000</transform>
>        </priority>
>        <showErrorStackTrace>true</showErrorStackTrace>
>    </messages>
>    <misc>
>        <threadCount>1</threadCount>
>    </misc>
>    <printer>
>        <alignment>
>            <methodCallChain>true</methodCallChain>
>            <parameterMethodDeclaration>false</parameterMethodDeclaration>
>            <ternaryOperator>true</ternaryOperator>
>            <variableAssignment>true</variableAssignment>
>            <variableIdentifier>true</variableIdentifier>
>        </alignment>
>        <backup>
>            <directory>bak</directory>
>            <level>0</level>
>        </backup>
>        <blanklines>
>            <after>
>                <block>1</block>
>                <braceLeft>0</braceLeft>
>                <class>1</class>
>                <declaration>0</declaration>
>                <footer>1</footer>
>                <header>1</header>
>                <interface>1</interface>
>                <lastImport>2</lastImport>
>                <method>1</method>
>                <package>1</package>
>            </after>
>            <before>
>                <block>1</block>
>                <braceRight>0</braceRight>
>                <caseBlock>1</caseBlock>
>                <comment>
>                    <javadoc>1</javadoc>
>                    <multiline>1</multiline>
>                    <singleline>1</singleline>
>                </comment>
>                <controlStatement>1</controlStatement>
>                <declaration>1</declaration>
>                <footer>0</footer>
>                <header>0</header>
>            </before>
>            <keepUpTo>1</keepUpTo>
>        </blanklines>
>        <braces>
>            <empty>
>                <cuddle>false</cuddle>
>                <insertStatement>false</insertStatement>
>            </empty>
>            <insert>
>                <dowhile>true</dowhile>
>                <for>true</for>
>                <ifelse>true</ifelse>
>                <while>true</while>
>            </insert>
>            <remove>
>                <block>true</block>
>                <dowhile>false</dowhile>
>                <for>false</for>
>                <ifelse>false</ifelse>
>                <while>false</while>
>            </remove>
>            <treatDifferent>
>                <methodClass>false</methodClass>
>                <methodClassIfWrapped>false</methodClassIfWrapped>
>            </treatDifferent>
>        </braces>
>        <chunks>
>            <blanklines>true</blanklines>
>            <comments>true</comments>
>        </chunks>
>        <comments>
>            <format>
>                <multiline>true</multiline>
>            </format>
>            <javadoc>
>                <check>
>                    <innerclass>false</innerclass>
>                    <tags>false</tags>
>                    <throwsTags>false</throwsTags>
>                </check>
>                <fieldsShort>true</fieldsShort>
>                <generate>
>                    <class>0</class>
>                    <constructor>0</constructor>
>                    <field>0</field>
>                    <method>0</method>
>                </generate>
>                <parseComments>false</parseComments>
>                <tags>
>                    <in-line />
>                    <standard />
>                </tags>
>                <templates>
>                    <method>
>                        <bottom> */</bottom>
>                        <exception> * @throws $exceptionType$ DOCUMENT ME!</exception>
>                        <param> * @param $paramType$ DOCUMENT ME!</param>
>                        <return> * @return DOCUMENT ME!</return>
>                        <top>/**| * DOCUMENT ME!</top>
>                    </method>
>                </templates>
>            </javadoc>
>            <remove>
>                <javadoc>false</javadoc>
>                <multiline>false</multiline>
>                <singleline>false</singleline>
>            </remove>
>            <separator>
>                <fillCharacter>-</fillCharacter>
>                <insert>true</insert>
>                <insertRecursive>false</insertRecursive>
>                <text>
>                    <class>Inner Classes</class>
>                    <constructor>Constructors</constructor>
>                    <field>Instance fields</field>
>                    <initializer>Instance initializers</initializer>
>                    <interface>Inner Interfaces</interface>
>                    <method>Methods</method>
>                    <static>Static fields/initializers</static>
>                </text>
>            </separator>
>        </comments>
>        <environment />
>        <footer>
>            <keys />
>            <smartMode>0</smartMode>
>            <use>false</use>
>        </footer>
>        <header>
>            <keys>The Apache Software Foundation</keys>
>            <smartMode>0</smartMode>
>            <text>/*| * Copyright 1999-2004 The Apache Software Foundation or its licensors,| * as applicable.| *| * Licensed under the Apache License, Version 2.0 (the "License");| * you may not use this file except in compliance with the License.| * You may obtain a copy of the License at| *| *      http://www.apache.org/licenses/LICENSE-2.0| *| * Unless required by applicable law or agreed to in writing, software| * distributed under the License is distributed on an "AS IS" BASIS,| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.| * See the License for the specific language governing permissions and| * limitations under the License.| */</text>
>            <use>true</use>
>        </header>
>        <history>
>            <policy>disabled</policy>
>        </history>
>        <imports>
>            <grouping>
>                <defaultDepth>3</defaultDepth>
>                <packages>*:0|gnu:2|java:2|javax:2</packages>
>            </grouping>
>            <policy>disabled</policy>
>            <sort>true</sort>
>        </imports>
>        <indentation>
>            <caseFromSwitch>true</caseFromSwitch>
>            <continuation>
>                <block>false</block>
>                <operator>false</operator>
>            </continuation>
>            <firstColumnComments>true</firstColumnComments>
>            <label>false</label>
>            <policy>
>                <deep>false</deep>
>            </policy>
>            <sizes>
>                <braceCuddled>1</braceCuddled>
>                <braceLeft>1</braceLeft>
>                <braceRight>0</braceRight>
>                <braceRightAfter>1</braceRightAfter>
>                <continuation>4</continuation>
>                <deep>55</deep>
>                <extends>-1</extends>
>                <general>4</general>
>                <implements>-1</implements>
>                <leading>0</leading>
>                <tabs>4</tabs>
>                <throws>-1</throws>
>                <trailingComment>1</trailingComment>
>            </sizes>
>            <tabs>
>                <enable>false</enable>
>                <onlyLeading>false</onlyLeading>
>            </tabs>
>        </indentation>
>        <misc>
>            <arrayBracketsAfterIdent>false</arrayBracketsAfterIdent>
>            <forceFormatting>false</forceFormatting>
>            <insertExpressionParentheses>true</insertExpressionParentheses>
>            <insertLoggingConditional>false</insertLoggingConditional>
>            <insertTrailingNewline>true</insertTrailingNewline>
>            <insertUID>false</insertUID>
>        </misc>
>        <sorting>
>            <declaration>
>                <class>false</class>
>                <constructor>false</constructor>
>                <enable>true</enable>
>                <interface>false</interface>
>                <method>false</method>
>                <order>static|field|initializer|constructor|method|interface|class</order>
>                <variable>false</variable>
>            </declaration>
>            <modifier>
>                <enable>false</enable>
>                <order>public|protected|private|abstract|static|final|synchronized|transient|volatile|native|strictfp</order>
>            </modifier>
>        </sorting>
>        <whitespace>
>            <after>
>                <comma>true</comma>
>                <semicolon>true</semicolon>
>                <typeCast>true</typeCast>
>            </after>
>            <before>
>                <braces>false</braces>
>                <brackets>false</brackets>
>                <bracketsTypes>false</bracketsTypes>
>                <caseColon>false</caseColon>
>                <operator>
>                    <not>false</not>
>                </operator>
>                <parentheses>
>                    <methodCall>false</methodCall>
>                    <methodDeclaration>false</methodDeclaration>
>                    <statement>false</statement>
>                </parentheses>
>            </before>
>            <padding>
>                <braces>false</braces>
>                <brackets>false</brackets>
>                <operator>
>                    <assignment>true</assignment>
>                    <bitwise>true</bitwise>
>                    <logical>true</logical>
>                    <mathematical>true</mathematical>
>                    <relational>true</relational>
>                    <shift>true</shift>
>                </operator>
>                <parenthesis>false</parenthesis>
>                <typeCast>false</typeCast>
>            </padding>
>        </whitespace>
>        <wrapping>
>            <always>
>                <after>
>                    <arrayElement>0</arrayElement>
>                    <braceRight>false</braceRight>
>                    <extendsTypes>false</extendsTypes>
>                    <implementsTypes>false</implementsTypes>
>                    <label>true</label>
>                    <methodCallChained>false</methodCallChained>
>                    <ternaryOperator>
>                        <first>false</first>
>                        <second>false</second>
>                    </ternaryOperator>
>                    <throwsTypes>false</throwsTypes>
>                </after>
>                <before>
>                    <braceLeft>false</braceLeft>
>                    <extends>false</extends>
>                    <implements>false</implements>
>                    <throws>false</throws>
>                </before>
>                <parameter>
>                    <methodCall>false</methodCall>
>                    <methodCallNested>false</methodCallNested>
>                    <methodDeclaration>false</methodDeclaration>
>                </parameter>
>            </always>
>            <general>
>                <beforeOperator>false</beforeOperator>
>                <enable>true</enable>
>                <lineLength>80</lineLength>
>            </general>
>            <ondemand>
>                <after>
>                    <assignment>false</assignment>
>                    <leftParenthesis>false</leftParenthesis>
>                    <parameter>false</parameter>
>                    <types>
>                        <extends>false</extends>
>                        <implements>false</implements>
>                        <throws>false</throws>
>                    </types>
>                </after>
>                <before>
>                    <rightParenthesis>false</rightParenthesis>
>                </before>
>                <groupingParentheses>false</groupingParentheses>
>            </ondemand>
>        </wrapping>
>    </printer>
></jalopy>
>
>  
>



Re: Automated formatting of Java files

Posted by Diwaker Gupta <di...@apache.org>.
> Can you tell me what settings you are using so I can try to match them
> with my Jalopy plugin without thinking too hard?  :)

Attaching the convention file. I use it with the Ant plugin. But you
can use whatever you want.

-- 
Web/Blog/Gallery: floatingsun.net

Re: Automated formatting of Java files

Posted by Addi <ad...@rocktreesky.com>.
Diwaker Gupta wrote:

>Following up with our earlier discussion on whitespace cleanup, I have added 
>two "jalopied" Java source files in the test-whitespace directory. I would 
>urge the devs to take a look at both the original Java file and the formatted 
>file and see if it suits their tastes.
>
>Please post any comments and suggestions in this thread. Bear in mind that 
>with Jalopy, almost *anything* is configurable. So if you find anything, 
>however small/trivial it is that you don't like, please let me know. Its a 
>great tool, so lets make the best use of it. 
>
>To see exactly what is configurable, take a look at:
>http://jalopy.sourceforge.net/manual.html
>
>As an example of things that it can do:
>
>o automatically add/update headers (the Apache license, for instance. In 
>future if we have to change one line in the license, Jalopy will 
>automatically delete the old one and insert the new one)
>
>o sorting/group of imports, methods, variables
>
>o alignments, braces, spaces -- you name it!
>
>o automatic javadoc generation, correction
>
>  
>
Can you tell me what settings you are using so I can try to match them 
with my Jalopy plugin without thinking too hard?  :)

- Addi



Re: Automated formatting of Java files

Posted by David Crossley <cr...@apache.org>.
Diwaker Gupta wrote:
> > I think that it is doing too much, e.g. aligning like this:
> >          this.processor     = null;
> >          this.parser        = null;
> 
> Ok, we can choose not to. I'll modify the config and add it to the
> repository sometime later tonight. Meanwhile, I really don't see why
> doing "too much" is a problem. Its an automated tool, its _trivial_ to
> change such things at a minute's notice. But I'll go with the
> majority, I'm not anal about cleanup conventions :-)

See my answer in the "formatting of XML files" thread.

> > 1 whitespace at end-of-line
> 
> Ok
> 
> > 2 tabs to four-space
> 
> Ok
> 
> > 3 indentation
> 
> Ok
> 
> > 4 organise imports
> 
> Ok. Jalopy can get fancy and explode imports (so instead of
> org.apache.forrest.* it will declare only those classes that are
> actually used) or it can collapse them (use foo.* instead of
> individual classes). Any preferences there or do I just leave them as
> it is?

At Cocoon we try to get the developer to define
only the classes they actually use.

People use their own tools to do that for the files
that they are working on. Cocoon doesn't do any
automated cleanup, but occasionally we will see a
committer do some cleanup.

I think that we should apply such cleanups.

> > 5 whitespace in java statements
> 
> This is vague. Are you talking about assignments? expressions?
> operators? brackets? space before? space after? An example would
> really  help :-)

It was a general statement to cover all those cases.
Your example showed many changes. In my opinion, too much.

However we don't have much java code and it doesn't
change very often, so i am less concerned about any
changes that you want to do here. XML is the bigger problem.

> > 6 aligned variable assignments
> 
> Subject to dev preferences I guess.
> 
> -- 
> Web/Blog/Gallery: floatingsun.net

Re: Automated formatting of Java files

Posted by Diwaker Gupta <di...@apache.org>.
> I think that it is doing too much, e.g. aligning like this:
>          this.processor     = null;
>          this.parser        = null;

Ok, we can choose not to. I'll modify the config and add it to the
repository sometime later tonight. Meanwhile, I really don't see why
doing "too much" is a problem. Its an automated tool, its _trivial_ to
change such things at a minute's notice. But I'll go with the
majority, I'm not anal about cleanup conventions :-)

> 1 whitespace at end-of-line

Ok

> 2 tabs to four-space

Ok

> 3 indentation

Ok

> 4 organise imports

Ok. Jalopy can get fancy and explode imports (so instead of
org.apache.forrest.* it will declare only those classes that are
actually used) or it can collapse them (use foo.* instead of
individual classes). Any preferences there or do I just leave them as
it is?

> 5 whitespace in java statements

This is vague. Are you talking about assignments? expressions?
operators? brackets? space before? space after? An example would
really  help :-)

> 6 aligned variable assignments

Subject to dev preferences I guess.

-- 
Web/Blog/Gallery: floatingsun.net

Re: Automated formatting of Java files

Posted by David Crossley <cr...@apache.org>.
Diwaker Gupta wrote:
> Following up with our earlier discussion on whitespace cleanup, I have added 
> two "jalopied" Java source files in the test-whitespace directory. I would 
> urge the devs to take a look at both the original Java file and the formatted 
> file and see if it suits their tastes.
> 
> Please post any comments and suggestions in this thread. Bear in mind that 
> with Jalopy, almost *anything* is configurable. So if you find anything, 
> however small/trivial it is that you don't like, please let me know. Its a 
> great tool, so lets make the best use of it. 

I think that it is doing too much, e.g. aligning like this:
         this.processor     = null;
         this.parser        = null;

We should start with very simple stuff, e.g. just tabs
and trailing whitespace, then gradually add other operations.
However we don't want to get too strict on code style.

I suggest that we define a list of what we would possibly
want to adrress. Here is a start:

1 whitespace at end-of-line
2 tabs to four-space
3 indentation
4 organise imports
5 whitespace in java statements
6 aligned variable assignments

We need to at least do 1 and 2 while 5 and 6
may be doubtful.

-David

> To see exactly what is configurable, take a look at:
> http://jalopy.sourceforge.net/manual.html
> 
> As an example of things that it can do:
> 
> o automatically add/update headers (the Apache license, for instance. In 
> future if we have to change one line in the license, Jalopy will 
> automatically delete the old one and insert the new one)
> 
> o sorting/group of imports, methods, variables
> 
> o alignments, braces, spaces -- you name it!
> 
> o automatic javadoc generation, correction
> 
> -- 
> Web/Blog/Gallery: http://floatingsun.net