Skip to content

Commit

Permalink
Merge pull request #136 from baratali/iss58
Browse files Browse the repository at this point in the history
Fixes #58. Update for CustomDeclarationOrderCheck.
  • Loading branch information
romani committed Sep 23, 2013
2 parents 777c28c + 217934e commit b5193cf
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 13 deletions.
Expand Up @@ -100,6 +100,7 @@
* </p>
*
* @author <a href="mailto:solid.danil@gmail.com">Danil Lopatin</a>
* @author <a href="mailto:barataliba@gmail.com">Baratali Izmailov</a>
*/
public class CustomDeclarationOrderCheck extends Check
{
Expand Down Expand Up @@ -308,25 +309,47 @@ public void visitToken(DetailAST aAST)
{

if (aAST.getType() == TokenTypes.CLASS_DEF) {
if (mClassRoot) {
mClassStates.push(new ClassStates());
mClassRoot = false;
}
else {
if (mInnerClass) {
//if we have condition to check Inner Classes order
checkOrderLogic(aAST);
if(!isClassDefInMethodDef(aAST)) {
if (mClassRoot) {
mClassStates.push(new ClassStates());
mClassRoot = false;
}
else {
if (mInnerClass) {
//if we have condition to check Inner Classes order
checkOrderLogic(aAST);
}
mClassStates.push(new ClassStates());
}
mClassStates.push(new ClassStates());
}
}
else {
final int parentParentType = aAST.getParent().getParent().getType();
if (parentParentType == TokenTypes.CLASS_DEF) {
final DetailAST parentAst = aAST.getParent().getParent();
if (parentAst.getType() == TokenTypes.CLASS_DEF
&& !isClassDefInMethodDef(parentAst)) {
checkOrderLogic(aAST);
}
}
}

/**
* Verify that class definition is in method definition.
* @param aClassDef
* DetailAST of CLASS_DEF.
* @return true if class definition is in method definition.
*/
private static boolean isClassDefInMethodDef(DetailAST aClassDef) {
boolean result = false;
DetailAST currentParentAst = aClassDef.getParent();
while (currentParentAst != null) {
if (currentParentAst.getType() == TokenTypes.METHOD_DEF) {
result = true;
break;
}
currentParentAst = currentParentAst.getParent();
}
return result;
}

/**
* Check class declaration order with custom declaration order.
Expand Down Expand Up @@ -409,7 +432,8 @@ private void writeLog(final DetailAST aAST,
@Override
public void leaveToken(DetailAST aAST)
{
if (aAST.getType() == TokenTypes.CLASS_DEF) {
if (aAST.getType() == TokenTypes.CLASS_DEF
&& !isClassDefInMethodDef(aAST)) {
mClassStates.pop();
if (mClassStates.isEmpty()) {
mClassRoot = true;
Expand Down Expand Up @@ -562,4 +586,4 @@ private void updateRegexp(final String aFormat, final int aCompileFlags)
}
}

}
}
Expand Up @@ -107,3 +107,68 @@ class Test {
void test() {
}
}

// Check must ignore these cases:
class ClassForIssue58 {
public Object createB() {
class B {

}
return new B();
}

void otherMethod() {

}
}

class ClassForIssue58 {
private void simplePrivate() {
class Inner {
int number;
public void get(){}
private void set(){}
}
}

protected void simpleProtected() {
class Inner {
private int a;
protected int b;
int c;
public int d;
private void a(){}
void c(){}
public void d(){}
class AnotherClass{
public void t(){}
private void y(){}
}
}
}

public void simplePublic() {
class Inner {
private int a;
protected int b;
int c;
public int d;
private void a(){}
void c(){}
public void d(){}
class AnotherClass{
public boolean right;
private boolean left;
}
}
}

public void abc() {
class A {
public boolean is;
}
class B {
private boolean is;
}
}
}

0 comments on commit b5193cf

Please sign in to comment.