1 package com.puppycrawl.tools.checkstyle.checks.modifier; 20 21 import antlr.collections.AST; 22 23 import java.util.List ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 27 import com.puppycrawl.tools.checkstyle.api.Check; 28 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 29 import com.puppycrawl.tools.checkstyle.api.DetailAST; 30 31 68 public class ModifierOrderCheck 69 extends Check 70 { 71 75 private static final String [] JLS_ORDER = 76 { 77 "public", "protected", "private", "abstract", "static", "final", 78 "transient", "volatile", "synchronized", "native", "strictfp", 79 }; 80 81 82 public int[] getDefaultTokens() 83 { 84 return new int[] {TokenTypes.MODIFIERS}; 85 } 86 87 88 public void visitToken(DetailAST aAST) 89 { 90 final List mods = new ArrayList (); 91 AST modifier = aAST.getFirstChild(); 92 while (modifier != null) { 93 mods.add(modifier); 94 modifier = modifier.getNextSibling(); 95 } 96 97 if (!mods.isEmpty()) { 98 final DetailAST error = checkOrderSuggestedByJLS(mods); 99 if (error != null) { 100 if (error.getType() == TokenTypes.ANNOTATION) { 101 log(error.getLineNo(), error.getColumnNo(), 102 "annotation.order", 103 error.getFirstChild().getText() 104 + error.getFirstChild().getNextSibling() 105 .getText()); 106 } 107 else { 108 log(error.getLineNo(), error.getColumnNo(), 109 "mod.order", error.getText()); 110 } 111 } 112 } 113 } 114 115 116 124 DetailAST checkOrderSuggestedByJLS(List aModifiers) 125 { 126 int i = 0; 127 DetailAST modifier; 128 final Iterator it = aModifiers.iterator(); 129 if (!it.hasNext()) { 131 return null; 132 } 133 134 do { 136 modifier = (DetailAST) it.next(); 137 } 138 while (it.hasNext() && (modifier.getType() == TokenTypes.ANNOTATION)); 139 140 if (modifier.getType() == TokenTypes.ANNOTATION) { 142 return null; 143 } 144 145 while (i < JLS_ORDER.length) { 146 if (modifier.getType() == TokenTypes.ANNOTATION) { 147 return modifier; 149 } 150 151 while ((i < JLS_ORDER.length) 152 && !JLS_ORDER[i].equals(modifier.getText())) 153 { 154 i++; 155 } 156 157 if (i == JLS_ORDER.length) { 158 return modifier; 160 } 161 else if (!it.hasNext()) { 162 return null; 164 } 165 else { 166 modifier = (DetailAST) it.next(); 167 } 168 } 169 170 return modifier; 171 } 172 } 173 | Popular Tags |