Merge remote-tracking branch 'origin/master'

This commit is contained in:
Anna Kozlova
2012-05-18 10:43:05 +04:00
15 changed files with 603 additions and 305 deletions
@@ -66,7 +66,7 @@ public abstract class GenericRearranger {
* @return rearranged list of RangeEntry and CommentRuleInstance objects.
*/
public final List<RuleInstance> rearrangeEntries() {
final List<RuleInstance> prioritizedRuleInstances = new ArrayList<RuleInstance>();
final List<RuleInstance> prioritizedRuleInstances = new ArrayList<RuleInstance>();
buildRuleInstanceLists(prioritizedRuleInstances);
// Recursively reorder contents of every nested ClassEntry
@@ -26,9 +26,7 @@ import org.jdom.Element;
import java.lang.reflect.Modifier;
/** Routines to handle the 'final' modifier. */
public final class FinalAttribute
extends AndNotAttribute
{
public final class FinalAttribute extends AndNotAttribute {
// -------------------------- STATIC METHODS --------------------------
@@ -23,19 +23,24 @@ package com.wrq.rearranger;
import com.intellij.openapi.application.ApplicationManager
import com.intellij.psi.PsiModifier
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase
import com.wrq.rearranger.settings.CommentRule
import com.wrq.rearranger.settings.RearrangerSettings
import com.wrq.rearranger.settings.attributeGroups.ClassAttributes
import com.wrq.rearranger.settings.attributeGroups.FieldAttributes
import com.wrq.rearranger.settings.attributeGroups.MethodAttributes
import com.wrq.rearranger.util.CommentRuleBuilder
import org.jetbrains.annotations.NotNull
import org.jetbrains.annotations.Nullable
import com.wrq.rearranger.settings.CommentRule
import com.wrq.rearranger.util.java.*
/** JUnit tests for the rearranger plugin. */
class RearrangerTest extends LightCodeInsightFixtureTestCase {
private RearrangerSettings mySettings;
private RearrangerSettings mySettings
private JavaClassRuleBuilder classRule
private JavaInnerClassRuleBuilder innerClassRule
private JavaFieldRuleBuilder fieldRule
private JavaMethodRuleBuilder methodRule
private CommentRuleBuilder commentRule
@Override
protected String getBasePath() {
@@ -44,6 +49,7 @@ class RearrangerTest extends LightCodeInsightFixtureTestCase {
protected final void setUp() throws Exception {
super.setUp();
mySettings = new RearrangerSettings();
// rs.setAskBeforeRearranging(true); // uncomment for debugging file structure popup
mySettings.showFields = true
@@ -51,6 +57,12 @@ class RearrangerTest extends LightCodeInsightFixtureTestCase {
mySettings.showParameterTypes = true
mySettings.showRules = true
mySettings.rearrangeInnerClasses = true
classRule = new JavaClassRuleBuilder(settings: mySettings)
innerClassRule = new JavaInnerClassRuleBuilder(settings: mySettings)
fieldRule = new JavaFieldRuleBuilder(settings: mySettings)
methodRule = new JavaMethodRuleBuilder(settings: mySettings)
commentRule = new CommentRuleBuilder(settings: mySettings)
}
public final void testNoRearrangement() throws Exception {
@@ -59,102 +71,66 @@ class RearrangerTest extends LightCodeInsightFixtureTestCase {
public final void testPublicFieldRearrangement() throws Exception {
doTest('RearrangementTest', 'RearrangementResult2') {
def attributes = new FieldAttributes()
attributes.protectionLevelAttributes.plPublic = true
mySettings.addItem(attributes)
}
}
fieldRule.create {
modifier( PsiModifier.PUBLIC )
} } }
public final void testNotPublicFieldRearrangement() throws Exception {
doTest('RearrangementTest', 'RearrangementResult3') {
def attributes = new FieldAttributes()
attributes.protectionLevelAttributes.plPublic = true
attributes.protectionLevelAttributes.invertProtectionLevel = true
mySettings.addItem(attributes)
}
}
fieldRule.create {
modifier( PsiModifier.PUBLIC, invert: true )
} } }
public final void testConstructorRearrangement() throws Exception {
doTest('RearrangementTest', 'RearrangementResult4') {
def attributes = new MethodAttributes()
attributes.protectionLevelAttributes.plPackage = true
attributes.protectionLevelAttributes.plPublic = true
attributes.constructorMethodType = true
mySettings.addItem(attributes)
}
}
methodRule.create {
modifier([ PsiModifier.PUBLIC, PsiModifier.PACKAGE_LOCAL ])
target( MethodType.CONSTRUCTOR )
} } }
public final void testClassRearrangement() throws Exception {
doTest('RearrangementTest', 'RearrangementResult5') {
def attributes = new ClassAttributes()
attributes.protectionLevelAttributes.plPackage = true
mySettings.addClass(attributes)
}
}
classRule.create {
modifier( PsiModifier.PACKAGE_LOCAL )
} } }
public final void testPSFRearrangement() throws Exception {
doTest('RearrangementTest2', 'RearrangementResult6') {
def attributes = new FieldAttributes()
attributes.finalAttribute.value = true
attributes.staticAttribute.value = true
mySettings.addItem(attributes)
}
}
fieldRule.create {
modifier([ PsiModifier.FINAL, PsiModifier.STATIC ])
} } }
public final void testAnonClassInit() throws Exception {
doTest('RearrangementTest7', 'RearrangementResult7') {
def attributes = new FieldAttributes()
attributes.initialisedByAnonymousClassAttr.value = true
mySettings.addItem(attributes)
}
}
fieldRule.create {
initializer( InitializerType.ANONYMOUS_CLASS )
} } }
public final void testNameMatch() throws Exception {
doTest('RearrangementTest', 'RearrangementResult8') {
def fieldAttributes = new FieldAttributes()
fieldAttributes.nameAttribute.match = true
fieldAttributes.nameAttribute.expression = '.*5'
mySettings.addItem(fieldAttributes)
def methodAttributes = new MethodAttributes()
methodAttributes.nameAttribute.match = true
methodAttributes.nameAttribute.expression = '.*2'
mySettings.addItem(methodAttributes)
}
}
fieldRule.create { name('.*5') }
methodRule.create { name('.*2') }
} }
public final void testStaticInitializer() throws Exception {
doTest('RearrangementTest8', 'RearrangementResult8A') {
def methodAttributes = new MethodAttributes()
methodAttributes.staticAttribute.value = true
mySettings.addItem(methodAttributes)
}
}
methodRule.create { modifier( PsiModifier.STATIC ) }
} }
public final void testAlphabetizingGSMethods() throws Exception {
mySettings.keepGettersSettersTogether = false
doTest('RearrangementTest', 'RearrangementResult9') {
def attributes = new MethodAttributes()
attributes.getterSetterMethodType = true
attributes.otherMethodType = true
attributes.constructorMethodType = false
attributes.sortOptions.byName = true
mySettings.addItem(attributes)
mySettings.keepGettersSettersTogether = false
}
}
methodRule.create {
target([ MethodType.GETTER_OR_SETTER, MethodType.OTHER ])
sort(SortType.BY_NAME)
} } }
public final void testSimpleComment() throws Exception {
doTest('RearrangementTest', 'RearrangementResult10') {
def attributes = new FieldAttributes()
attributes.protectionLevelAttributes.plPublic = true
mySettings.addItem(attributes)
def comment = new CommentRule()
comment.commentText = '// simple comment **********'
comment.emitCondition = CommentRule.EMIT_IF_ITEMS_MATCH_PRECEDING_RULE
mySettings.addItem(comment)
}
}
fieldRule.create { modifier( PsiModifier.PUBLIC) }
commentRule.create {
comment( '// simple comment **********', condition: CommentRule.EMIT_IF_ITEMS_MATCH_PRECEDING_RULE )
} } }
/**
* Delete old comment and insert (identical) new one. This tests proper identification and deletion of old
@@ -164,55 +140,26 @@ class RearrangerTest extends LightCodeInsightFixtureTestCase {
*/
public final void testReplayComment() throws Exception {
doTest('RearrangementResult10', 'RearrangementResult10') {
def attributes = new FieldAttributes()
attributes.protectionLevelAttributes.plPublic = true
mySettings.addItem(attributes)
def comment = new CommentRule()
comment.commentText = '// simple comment **********'
comment.emitCondition = CommentRule.EMIT_IF_ITEMS_MATCH_PRECEDING_RULE
mySettings.addItem(comment)
}
}
fieldRule.create { modifier( PsiModifier.PUBLIC) }
commentRule.create {
comment( '// simple comment **********', condition: CommentRule.EMIT_IF_ITEMS_MATCH_PRECEDING_RULE )
} } }
public final void testMultipleRuleCommentMatch() throws Exception {
doTest('RearrangementTest11', 'RearrangementResult11') {
def allFieldsComment = new CommentRule()
allFieldsComment.commentText = '// FIELDS:'
allFieldsComment.emitCondition = CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE
allFieldsComment.allSubsequentRules = false
mySettings.addItem(allFieldsComment)
commentRule.create { comment('// FIELDS:', condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: false) }
commentRule.create { comment('// FINAL FIELDS:', condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: false,
subsequentRulesToMatch: 1) }
fieldRule.create { modifier(PsiModifier.FINAL) }
commentRule.create { comment('// NON-FINAL FIELDS:', condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: true,
subsequentRulesToMatch: 1) }
fieldRule.create { modifier(PsiModifier.FINAL, invert: true) }
} }
def finalFieldsComment = new CommentRule()
finalFieldsComment.commentText = '// FINAL FIELDS:'
finalFieldsComment.emitCondition = CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE
finalFieldsComment.allSubsequentRules = true
finalFieldsComment.NSubsequentRulesToMatch = 1
mySettings.addItem(finalFieldsComment)
def finalFields = new FieldAttributes()
finalFields.finalAttribute.value = true
mySettings.addItem(finalFields)
def nonFinalFieldsComment = new CommentRule()
nonFinalFieldsComment.commentText = '// NON-FINAL FIELDS:'
nonFinalFieldsComment.emitCondition = CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE
nonFinalFieldsComment.allSubsequentRules = true
nonFinalFieldsComment.NSubsequentRulesToMatch = 1
mySettings.addItem(nonFinalFieldsComment)
def nonFinalFields = new FieldAttributes()
nonFinalFields.finalAttribute.value = true
nonFinalFields.finalAttribute.invert = true
mySettings.addItem(nonFinalFields)
}
public final void testOpsBlockingQueueExample() throws Exception {
testOpsBlockingQueueExampleWorker(false, "OpsBlockingQueue", false, "OpsBlockingQueue");
}
// public final void testOpsBlockingQueueExample() throws Exception {
// testOpsBlockingQueueExampleWorker(false, "/com/wrq/rearranger/OpsBlockingQueue.java",
// false, "/com/wrq/rearranger/OpsBlockingQueue.java");
// }
//
// public final void testOpsBlockingQueueExampleWithGlobalPattern() throws Exception {
// testOpsBlockingQueueExampleWorker(true, "/com/wrq/rearranger/OpsBlockingQueue.java",
// false, "/com/wrq/rearranger/OpsBlockingQueue.java");
@@ -222,182 +169,131 @@ class RearrangerTest extends LightCodeInsightFixtureTestCase {
// testOpsBlockingQueueExampleWorker(false, "/com/wrq/rearranger/OpsBlockingQueueIndented.java",
// true, "/com/wrq/rearranger/OpsBlockingQueueIndentedResult.java");
// }
//
// private void testOpsBlockingQueueExampleWorker(boolean doGlobalPattern,
// String srcFilename,
// boolean doublePublicMethods,
// String compareFilename)
// throws Exception
// {
// // submitted by Joe Martinez.
// configureByFile(srcFilename);
// final PsiFile file = getFile();
// final Document doc = PsiDocumentManager.getInstance(getProject()).getDocument(file);
// CommentRule c;
// FieldAttributes fa;
// c = new CommentRule();
// c.setCommentText("//************************************** PUBLIC STATIC FIELDS *************************************");
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(2);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(false);
// rs.addItem(c, 0);
// fa = new FieldAttributes();
// fa.getSortAttr().setByName(true);
// fa.getPlAttr().setPlPublic(true);
// fa.getStAttr().setValue(true);
// fa.getfAttr().setValue(true);
// rs.addItem(fa, 1);
// fa = new FieldAttributes();
// fa.getSortAttr().setByName(true);
// fa.getPlAttr().setPlPublic(true);
// fa.getStAttr().setValue(true);
// rs.addItem(fa, 2);
// c = new CommentRule();
// c.setCommentText("//************************************** PUBLIC FIELDS *****************************************");
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(1);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(true);
// rs.addItem(c, 3);
// fa = new FieldAttributes();
// fa.getSortAttr().setByName(true);
// fa.getPlAttr().setPlPublic(true);
// rs.addItem(fa, 4);
// c = new CommentRule();
// c.setCommentText("//*********************************** PROTECTED/PACKAGE FIELDS **************************************");
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(3);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(false);
// rs.addItem(c, 5);
// fa = new FieldAttributes();
// fa.getSortAttr().setByName(true);
// fa.getPlAttr().setPlProtected(true);
// fa.getPlAttr().setPlPackage(true);
// fa.getStAttr().setValue(true);
// fa.getfAttr().setValue(true);
// rs.addItem(fa, 6);
// fa = new FieldAttributes();
// fa.getSortAttr().setByName(true);
// fa.getPlAttr().setPlProtected(true);
// fa.getPlAttr().setPlPackage(true);
// fa.getStAttr().setValue(true);
// rs.addItem(fa, 7);
// fa = new FieldAttributes();
// fa.getSortAttr().setByName(true);
// fa.getPlAttr().setPlProtected(true);
// fa.getPlAttr().setPlPackage(true);
// rs.addItem(fa, 8);
// c = new CommentRule();
// c.setCommentText("//************************************** PRIVATE FIELDS *****************************************");
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(1);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(false);
// rs.addItem(c, 9);
// fa = new FieldAttributes();
// fa.getSortAttr().setByName(true);
// fa.getPlAttr().setPlPrivate(true);
// rs.addItem(fa, 10);
// c = new CommentRule();
// c.setCommentText("//************************************** CONSTRUCTORS ************************************* ");
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(2);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(false);
// rs.addItem(c, 11);
// MethodAttributes ma = new MethodAttributes();
// ma.getPlAttr().setPlPublic(true);
// ma.setConstructorMethodType(true);
// rs.addItem(ma, 12);
// ma = new MethodAttributes();
// ma.setConstructorMethodType(true);
// rs.addItem(ma, 13);
// c = new CommentRule();
// c.setCommentText("//*********************************** GETTERS AND SETTERS ********************************** ");
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(2);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(false);
// rs.addItem(c, 14);
// ma = new MethodAttributes();
// ma.getSortAttr().setByName(true);
// ma.getPlAttr().setPlPublic(true);
// ma.setGetterSetterMethodType(true);
// rs.addItem(ma, 15);
// ma = new MethodAttributes();
// ma.getSortAttr().setByName(true);
// ma.setGetterSetterMethodType(true);
// rs.addItem(ma, 16);
// c = new CommentRule();
// String commentText =
// "//************************************** PUBLIC METHODS ************************************* ";
// if (doublePublicMethods) {
// commentText += "\n// PUBLIC METHODS LINE 2";
// }
// c.setCommentText(commentText);
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(1);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(true);
// rs.addItem(c, 17);
// ma = new MethodAttributes();
// ma.getSortAttr().setByName(true);
// ma.getPlAttr().setPlPublic(true);
// rs.addItem(ma, 18);
// c = new CommentRule();
// c.setCommentText("//********************************* PACKAGE/PROTECTED METHODS ******************************** ");
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(1);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(true);
// rs.addItem(c, 19);
// ma = new MethodAttributes();
// ma.getSortAttr().setByName(true);
// ma.getPlAttr().setPlProtected(true);
// ma.getPlAttr().setPlPackage(true);
// rs.addItem(ma, 20);
// c = new CommentRule();
// c.setCommentText("//************************************** PRIVATE METHODS *************************************");
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(1);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(true);
// rs.addItem(c, 21);
// ma = new MethodAttributes();
// ma.getSortAttr().setByName(true);
// ma.getPlAttr().setPlPrivate(true);
// rs.addItem(ma, 22);
// c = new CommentRule();
// c.setCommentText("//************************************** INNER CLASSES ************************************* ");
// c.setEmitCondition(2);
// c.setnPrecedingRulesToMatch(1);
// c.setnSubsequentRulesToMatch(1);
// c.setAllPrecedingRules(true);
// c.setAllSubsequentRules(true);
// rs.addItem(c, 23);
// InnerClassAttributes ic = new InnerClassAttributes();
// ic.getSortAttr().setByName(true);
// rs.addItem(ic, 24);
// rs.getExtractedMethodsSettings().setMoveExtractedMethods(false);
// if (doGlobalPattern) {
// rs.setGlobalCommentPattern("//\\*{20,45}[A-Z /]*\\*{20,45}\n");
// }
// final RearrangerActionHandler rah = new RearrangerActionHandler();
// rah.rearrangeDocument(getProject(), file, rs, doc);
// super.checkResultByFile(compareFilename);
// }
//
private void testOpsBlockingQueueExampleWorker(boolean doGlobalPattern,
String srcFilename,
boolean doublePublicMethods,
String compareFilename)
throws Exception
{
// submitted by Joe Martinez.
doTest(srcFilename, compareFilename) {
commentRule.create {
comment('//************************************** PUBLIC STATIC FIELDS *************************************',
condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: false, allPreceding: true,
subsequentRulesToMatch: 2, precedingRulesToMatch: 1)
}
fieldRule.create {
modifier([ PsiModifier.PUBLIC, PsiModifier.STATIC, PsiModifier.FINAL ])
sort( SortType.BY_NAME )
}
fieldRule.create {
modifier([ PsiModifier.PUBLIC, PsiModifier.STATIC ])
sort( SortType.BY_NAME )
}
commentRule.create {
comment('//************************************** PUBLIC FIELDS *****************************************',
condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: true, allPreceding: true,
subsequentRulesToMatch: 1, precedingRulesToMatch: 1)
}
fieldRule.create {
modifier( PsiModifier.PUBLIC )
sort( SortType.BY_NAME )
}
commentRule.create {
comment('//*********************************** PROTECTED/PACKAGE FIELDS **************************************',
condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: false, allPreceding: true,
subsequentRulesToMatch: 3, precedingRulesToMatch: 1)
}
fieldRule.create {
modifier([ PsiModifier.PROTECTED, PsiModifier.PACKAGE_LOCAL, PsiModifier.STATIC, PsiModifier.FINAL ])
sort( SortType.BY_NAME )
}
fieldRule.create {
modifier([ PsiModifier.PROTECTED, PsiModifier.PACKAGE_LOCAL, PsiModifier.STATIC ])
sort( SortType.BY_NAME )
}
fieldRule.create {
modifier([ PsiModifier.PROTECTED, PsiModifier.PACKAGE_LOCAL ])
sort( SortType.BY_NAME )
}
commentRule.create {
comment('//************************************** PRIVATE FIELDS *****************************************',
condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: false, allPreceding: true,
subsequentRulesToMatch: 1, precedingRulesToMatch: 1)
}
fieldRule.create {
modifier( PsiModifier.PRIVATE )
sort( SortType.BY_NAME )
}
commentRule.create {
comment('//************************************** CONSTRUCTORS ************************************* ',
condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: false, allPreceding: true,
subsequentRulesToMatch: 2, precedingRulesToMatch: 1)
}
methodRule.create {
modifier( PsiModifier.PUBLIC )
target( MethodType.CONSTRUCTOR )
}
methodRule.create { target( MethodType.CONSTRUCTOR ) }
commentRule.create {
comment('//*********************************** GETTERS AND SETTERS ********************************** ',
condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: false, allPreceding: true,
subsequentRulesToMatch: 2, precedingRulesToMatch: 1)
}
methodRule.create {
modifier( PsiModifier.PUBLIC )
target( MethodType.GETTER_OR_SETTER )
sort( SortType.BY_NAME )
}
methodRule.create {
target( MethodType.GETTER_OR_SETTER )
sort( SortType.BY_NAME )
}
def text = '//************************************** PUBLIC METHODS ************************************* '
if (doublePublicMethods) {
text += "\n// PUBLIC METHODS LINE 2";
}
commentRule.create {
comment(text, condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: true, allPreceding: true,
subsequentRulesToMatch: 1, precedingRulesToMatch: 1)
}
methodRule.create {
modifier( PsiModifier.PUBLIC )
sort( SortType.BY_NAME )
}
commentRule.create {
comment('//********************************* PACKAGE/PROTECTED METHODS ******************************** ',
condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: true, allPreceding: true,
subsequentRulesToMatch: 1, precedingRulesToMatch: 1)
}
methodRule.create {
modifier([ PsiModifier.PROTECTED, PsiModifier.PACKAGE_LOCAL ])
sort( SortType.BY_NAME )
}
commentRule.create {
comment('//************************************** PRIVATE METHODS *************************************',
condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: true, allPreceding: true,
subsequentRulesToMatch: 1, precedingRulesToMatch: 1)
}
methodRule.create {
modifier( PsiModifier.PRIVATE )
sort( SortType.BY_NAME )
}
commentRule.create {
comment('//************************************** INNER CLASSES ************************************* ',
condition: CommentRule.EMIT_IF_ITEMS_MATCH_SUBSEQUENT_RULE, allSubsequent: true, allPreceding: true,
subsequentRulesToMatch: 1, precedingRulesToMatch: 1)
}
innerClassRule.create { sort(SortType.BY_NAME ) }
mySettings.extractedMethodsSettings.moveExtractedMethods = false
if (doGlobalPattern) {
mySettings.globalCommentPattern = "//\\*{20,45}[A-Z /]*\\*{20,45}\n"
}
}
}
// public final void testReturnTypeMatch() throws Exception {
// configureByFile("/com/wrq/rearranger/RearrangementTest12.java");
// final PsiFile file = getFile();
@@ -0,0 +1,139 @@
package com.wrq.rearranger.util;
import com.wrq.rearranger.settings.RearrangerSettings
import com.wrq.rearranger.settings.atomicAttributes.AndNotAttribute
import com.wrq.rearranger.settings.attributeGroups.Rule
import org.jetbrains.annotations.NotNull
import org.junit.Assert
/**
* Defines common infrastructure for the {@link Rule} builders.
*
* @author Denis Zhdanov
* @since 5/17/12 10:43 AM
* @param <T> rule class
*/
public abstract class AbstractRuleBuilder<T> extends BuilderSupport {
@NotNull def RearrangerSettings settings
/**
* Holds rule customization handlers in the form {@code 'property id -> closure'} where <code>'property id'</code>
* is expected to be one of the {@link RearrangerTestDsl#getValue()} predefined constants}.
* <p/>
* Corresponding value is a closure that receives three arguments - target attribute value, attribute customization map
* and target rule instance .
*/
@NotNull private def myHandlers = [:].withDefault {
{ key -> Assert.fail("No handler for the rule attribute '${key}'") }
}
@Override
protected void nodeCompleted(Object parent, Object node) {
if (!parent) {
registerRule(settings, node as T)
}
}
@Override
protected void setParent(Object parent, Object child) {
}
@Override
protected Object createNode(Object name) {
if (name == 'create') {
return createRule()
}
createNode(name, [:], [])
}
@Override
protected Object createNode(Object name, Object value) {
createNode(name, [:], value)
}
@Override
protected Object createNode(Object name, Map attributes) {
createNode(name, attributes, [])
}
@Override
protected Object createNode(Object name, Map attributes, Object value) {
for (i in [value].flatten()) {
myHandlers[name](i, attributes, current)
}
getCurrent()
}
@NotNull
protected abstract T createRule()
protected abstract void registerRule(@NotNull RearrangerSettings settings, @NotNull T rule)
/**
* Registers given handler for the rule customization within the given data.
* <p/>
* Example:
* <pre>
* register('modifier', PsiModifier.FINAL, { rule, value, attributes -&gt;
* rule.finalAttribute.value = value
* if (attributes.invert) rule.finalAttribute.invert = true
* })
* </pre>
*
* @param specifier target rule attribute specifier used at the test DSL
* @param handler closure to apply target information to the rule instance. Accepts two values - target rule instance
* and additional rule attribute information map
*/
protected void registerHandler(@NotNull RearrangerTestDsl specifier, @NotNull Closure handler) {
myHandlers[specifier.value] = handler
true
}
/**
* Allows to get a closure with the following properties:
* <pre>
* <ul>
* <li>receives two arguments - target attribute data and target rule;</li>
* <li>
* assumes that target attribute (identified by the 'propertyName' method argument) IS-A {@link AndNotAttribute}
* and updates its value at the given rule object in accordance with the given attribute data;
* </li>
* </ul>
* </pre>
* I.e. returned closure looks like <code>{ attributes, rule -> ...}</code>, it configure <code>'rule.propertyName'</code>
* according to the <code>'attributes'</code> argument.
*
* @param propertyName name of the target attribute property at the target rule class
* @return closure to use for updating target attribute
*/
@NotNull
protected static Closure createBooleanAttributeHandler(@NotNull final String propertyName) {
{ attributes, T rule ->
rule."$propertyName".value = true
if (attributes[RearrangerTestDsl.INVERT.value]) {
rule."$propertyName".invert = true
}
}
}
/**
* Very similar to the {@link #createBooleanAttributeHandler(java.lang.String)} but targets boolean property with the given name
* at the given rule argument.
*
* @param propertyName name of the target attribute property at the target rule class
* @return closure to use for updating target attribute
*/
@NotNull
protected static Closure createRawBooleanAttributeHandler(@NotNull final String propertyName) {
{ attributes, T rule -> rule."$propertyName" = true }
}
protected static Closure createStringAttributeHandler(@NotNull final String propertyName) {
{ value, attributes, T rule ->
rule."$propertyName".match = true
rule."$propertyName".expression = value
}
}
}
@@ -0,0 +1,40 @@
package com.wrq.rearranger.util;
import com.wrq.rearranger.settings.CommentRule;
import com.wrq.rearranger.settings.RearrangerSettings;
import org.jetbrains.annotations.NotNull;
/**
* @author Denis Zhdanov
* @since 5/17/12 4:08 PM
*/
class CommentRuleBuilder extends AbstractRuleBuilder<CommentRule> {
{
def helper = { RearrangerTestDsl dslProperty, map, rulePropertyName, rule ->
if (map.containsKey(dslProperty.value)) {
rule."$rulePropertyName" = map[dslProperty.value]
}
}
registerHandler(RearrangerTestDsl.COMMENT, { data, attributes, rule ->
rule.commentText = data
helper(RearrangerTestDsl.CONDITION, attributes, 'emitCondition', rule)
helper(RearrangerTestDsl.ALL_SUBSEQUENT, attributes, 'allSubsequentRules', rule)
helper(RearrangerTestDsl.ALL_PRECEDING, attributes, 'allPrecedingRules', rule)
helper(RearrangerTestDsl.SUBSEQUENT_RULES_TO_MATCH, attributes, 'NSubsequentRulesToMatch', rule)
helper(RearrangerTestDsl.PRECEDING_RULES_TO_MATCH, attributes, 'NPrecedingRulesToMatch', rule)
})
}
@NotNull
@Override
protected CommentRule createRule() {
new CommentRule()
}
@Override
protected void registerRule(@NotNull RearrangerSettings settings, @NotNull CommentRule rule) {
settings.addItem(rule)
}
}
@@ -0,0 +1,47 @@
package com.wrq.rearranger.util;
import org.jetbrains.annotations.NotNull;
/**
* Enumerates constants used at the Rearranger DSL.
*
* @author Denis Zhdanov
* @since 5/17/12 12:57 PM
*/
public enum RearrangerTestDsl {
NAME,
MODIFIER,
/** Field initializer type. */
INITIALIZER,
/** Method target type (e.g. constructor). */
TARGET,
SORT,
COMMENT,
// Attributes
INVERT,
CONDITION,
ALL_SUBSEQUENT("allSubsequent"),
ALL_PRECEDING("allPreceding"),
SUBSEQUENT_RULES_TO_MATCH("subsequentRulesToMatch"),
PRECEDING_RULES_TO_MATCH("precedingRulesToMatch");
@NotNull private final String myValue;
RearrangerTestDsl() {
myValue = toString().toLowerCase();
}
RearrangerTestDsl(@NotNull String value) {
myValue = value;
}
/**
* @return string value used at the Rearranger DSL
*/
@NotNull
public String getValue() {
return myValue;
}
}
@@ -0,0 +1,51 @@
package com.wrq.rearranger.util.java
import com.intellij.openapi.util.text.StringUtil
import com.intellij.psi.PsiModifier
import com.wrq.rearranger.util.AbstractRuleBuilder
import com.wrq.rearranger.util.RearrangerTestDsl
/**
* @author Denis Zhdanov
* @since 5/17/12 10:47 AM
*/
abstract class AbstractJavaRuleBuilder<T> extends AbstractRuleBuilder<T> {{
// Name
registerHandler(RearrangerTestDsl.NAME, { value, attributes, rule ->
rule.nameAttribute.match = true
rule.nameAttribute.expression = value
})
// Sort
def sortOptions = [ (SortType.BY_NAME) : "byName"]
registerHandler(RearrangerTestDsl.SORT, { data, attributes, rule ->
rule.sortOptions."${sortOptions[data]}" = true
})
// Modifiers
// Visibility
def visibilityHandler = { propertyName, attributes, rule ->
rule.protectionLevelAttributes."$propertyName" = true
if (attributes.invert) {
rule.protectionLevelAttributes.invertProtectionLevel = true
}
}
// Final, static.
def genericHandlers = [:]
for (i in [PsiModifier.FINAL, PsiModifier.STATIC]) {
genericHandlers[i] = createBooleanAttributeHandler("${i}Attribute")
}
def visibilityModifiers = [PsiModifier.PUBLIC, PsiModifier.PROTECTED, PsiModifier.PRIVATE, PsiModifier.PACKAGE_LOCAL].toSet()
registerHandler(RearrangerTestDsl.MODIFIER, { value, attributes, rule ->
if (visibilityModifiers.contains(value)) {
def propertyName = value == PsiModifier.PACKAGE_LOCAL ? 'plPackage' : "pl${StringUtil.capitalize(value)}"
visibilityHandler(propertyName, attributes, rule)
}
else {
genericHandlers[value](attributes, rule)
}
})
}}
@@ -0,0 +1,9 @@
package com.wrq.rearranger.util.java;
/**
* @author Denis Zhdanov
* @since 5/17/12 11:51 AM
*/
public enum InitializerType {
ANONYMOUS_CLASS
}
@@ -0,0 +1,22 @@
package com.wrq.rearranger.util.java
import com.wrq.rearranger.settings.RearrangerSettings
import com.wrq.rearranger.settings.attributeGroups.ClassAttributes
import org.jetbrains.annotations.NotNull
/**
* @author Denis Zhdanov
* @since 5/17/12 11:13 AM
*/
class JavaClassRuleBuilder extends AbstractJavaRuleBuilder<ClassAttributes> {
@Override
protected ClassAttributes createRule() {
new ClassAttributes()
}
@Override
protected void registerRule(@NotNull RearrangerSettings settings, @NotNull ClassAttributes rule) {
settings.addClass(rule)
}
}
@@ -0,0 +1,28 @@
package com.wrq.rearranger.util.java
import com.wrq.rearranger.settings.RearrangerSettings
import org.jetbrains.annotations.NotNull
import com.wrq.rearranger.settings.attributeGroups.FieldAttributes
import com.wrq.rearranger.util.RearrangerTestDsl
/**
* @author Denis Zhdanov
* @since 5/17/12 10:49 AM
*/
class JavaFieldRuleBuilder extends AbstractJavaRuleBuilder<FieldAttributes> {
{
def handlers = [(InitializerType.ANONYMOUS_CLASS) : createBooleanAttributeHandler('initialisedByAnonymousClassAttr')]
registerHandler(RearrangerTestDsl.INITIALIZER, { data, attributes, rule -> handlers[data](attributes, rule) })
}
@Override
protected FieldAttributes createRule() {
new FieldAttributes()
}
@Override
protected void registerRule(@NotNull RearrangerSettings settings, @NotNull FieldAttributes rule) {
settings.addItem(rule)
}
}
@@ -0,0 +1,21 @@
package com.wrq.rearranger.util.java
import com.wrq.rearranger.settings.attributeGroups.InnerClassAttributes
import com.wrq.rearranger.settings.RearrangerSettings
/**
* @author Denis Zhdanov
* @since 5/18/12 8:51 AM
*/
class JavaInnerClassRuleBuilder extends AbstractJavaRuleBuilder<InnerClassAttributes> {
@Override
protected InnerClassAttributes createRule() {
new InnerClassAttributes()
}
@Override
protected void registerRule(RearrangerSettings settings, InnerClassAttributes rule) {
settings.addItem(rule)
}
}
@@ -0,0 +1,32 @@
package com.wrq.rearranger.util.java
import com.wrq.rearranger.settings.RearrangerSettings
import com.wrq.rearranger.settings.attributeGroups.MethodAttributes
import org.jetbrains.annotations.NotNull
import com.wrq.rearranger.util.RearrangerTestDsl
/**
* @author Denis Zhdanov
* @since 5/17/12 11:01 AM
*/
class JavaMethodRuleBuilder extends AbstractJavaRuleBuilder<MethodAttributes> {
{
def handlers = [
(MethodType.CONSTRUCTOR) : createRawBooleanAttributeHandler('constructorMethodType'),
(MethodType.GETTER_OR_SETTER) : createRawBooleanAttributeHandler('getterSetterMethodType'),
(MethodType.OTHER) : createRawBooleanAttributeHandler('otherMethodType')
]
registerHandler(RearrangerTestDsl.TARGET, { data, attributes, rule -> handlers[data](attributes, rule) })
}
@Override
protected MethodAttributes createRule() {
new MethodAttributes()
}
@Override
protected void registerRule(@NotNull RearrangerSettings settings, @NotNull MethodAttributes rule) {
settings.addItem(rule)
}
}
@@ -0,0 +1,9 @@
package com.wrq.rearranger.util.java;
/**
* @author Denis Zhdanov
* @since 5/17/12 11:05 AM
*/
public enum MethodType {
CONSTRUCTOR, GETTER_OR_SETTER, OTHER
}
@@ -0,0 +1,9 @@
package com.wrq.rearranger.util.java;
/**
* @author Denis Zhdanov
* @since 5/17/12 3:57 PM
*/
public enum SortType {
BY_NAME
}
@@ -10,22 +10,20 @@ import opsx.server.email.OpsEmailGenerator;
/** @author dchan */
public class OpsBlockingQueue extends OpsNonBlockingQueue {
//*********************************** PROTECTED/PACKAGE FIELDS **************************************
//*********************************** PROTECTED/PACKAGE FIELDS **************************************
protected final Object putLock;
protected final Object takeLock;
protected volatile int waitCount;
//************************************** PRIVATE FIELDS *****************************************
//************************************** PRIVATE FIELDS *****************************************
private static final OpsLogCategory logCat = (OpsLogCategory)OpsLogCategory.getInstance(OpsEmailGenerator.class);
//************************************** CONSTRUCTORS *************************************
/** Creates new OpsBlockingQueue */
public OpsBlockingQueue() {
takeLock = new Object();
putLock = new Object();
waitCount = 0;
}
//************************************** PUBLIC METHODS *************************************
//************************************** PUBLIC METHODS *************************************
public void put(Object obj) {
synchronized (putLock) {
insert(obj);
@@ -60,8 +58,7 @@ public class OpsBlockingQueue extends OpsNonBlockingQueue {
}
}
}
//********************************* PACKAGE/PROTECTED METHODS ********************************
//********************************* PACKAGE/PROTECTED METHODS ********************************
protected Object takeNext() {
synchronized (takeLock) {
return super.takeNext();