From 95dfe333644a75628ac2e47b62470a0a789f622e Mon Sep 17 00:00:00 2001 From: Dinesh Bolkensteyn Date: Tue, 25 Oct 2011 23:34:25 +0200 Subject: [PATCH] SONAR-2934 Transitive accessor computation now supports recursive methods --- .../sonar/java/bytecode/asm/AsmMethod.java | 2 +- .../java/bytecode/asm/AsmMethodTest.java | 8 ++++ .../bytecode/bin/properties/JavaBean.class | Bin 2009 -> 3193 bytes .../bytecode/src/properties/JavaBean.java | 45 ++++++++++++++++++ 4 files changed, 54 insertions(+), 1 deletion(-) diff --git a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/asm/AsmMethod.java b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/asm/AsmMethod.java index 51e27b1d7ee..c37d54a9706 100644 --- a/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/asm/AsmMethod.java +++ b/plugins/sonar-squid-java-plugin/src/main/java/org/sonar/java/bytecode/asm/AsmMethod.java @@ -142,8 +142,8 @@ public class AsmMethod extends AsmResource { private void ensureAccessorComputed() { if (accessFieldComputed) return; + accessFieldComputed = true; // Set accessFieldComputed to true before calling setAccessedField() to prevent infinite recursion on recursive methods. setAccessedField(); - accessFieldComputed = true; } private void setAccessedField() { diff --git a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/asm/AsmMethodTest.java b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/asm/AsmMethodTest.java index 631f0078b0a..6a2f4abb0a5 100644 --- a/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/asm/AsmMethodTest.java +++ b/plugins/sonar-squid-java-plugin/src/test/java/org/sonar/java/bytecode/asm/AsmMethodTest.java @@ -73,6 +73,11 @@ public class AsmMethodTest { assertTrue(javaBean.getMethod("accessorWithABunchOfCalls()V").isAccessor()); assertFalse(javaBean.getMethod("iShouldBeAStaticSetter()V").isAccessor()); assertTrue(javaBean.getMethod("getFirstName()Ljava/lang/String;").isAccessor()); + assertTrue(javaBean.getMethod("getFirstNameAndOneArgument(Ljava/lang/String;)Ljava/lang/String;").isAccessor()); + assertTrue(javaBean.getMethod("recursiveAbs(I)I").isAccessor()); + assertFalse(javaBean.getMethod("recursiveAbsNotAccessor(I)I").isAccessor()); + assertTrue(javaBean.getMethod("recursiveAbsSameIncrementA(I)I").isAccessor()); + assertFalse(javaBean.getMethod("recursiveAbsDifferentIncrementA(I)I").isAccessor()); } @Test @@ -89,6 +94,9 @@ public class AsmMethodTest { assertThat(javaBean.getMethod("accessorWithABunchOfCalls()V").getAccessedField().getName(), is("firstNames")); assertNull(javaBean.getMethod("iShouldBeAStaticSetter()V").getAccessedField()); assertThat(javaBean.getMethod("getFirstName()Ljava/lang/String;").getAccessedField().getName(), is("FirstName")); + assertThat(javaBean.getMethod("getFirstNameAndOneArgument(Ljava/lang/String;)Ljava/lang/String;").getAccessedField().getName(), is("FirstName")); + assertThat(javaBean.getMethod("recursiveAbs(I)I").getAccessedField().getName(), is("myIncrement")); + assertNull(javaBean.getMethod("recursiveAbsNotAccessor(I)I").getAccessedField()); } } diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/bin/properties/JavaBean.class b/plugins/sonar-squid-java-plugin/test-resources/bytecode/bin/properties/JavaBean.class index 6c6d60868f08de04a3c909733e78b30e423532d4..1724b051e656d3fb508274ed2915a1d6a68656a3 100644 GIT binary patch literal 3193 zcmah~T~`xV6y3u|62bry2?|j`s}_QawraIl+XSUz`D&w}wtfuB1qLTGaWcWu{(=68 zzW2q4cC}zvU8}EMt3Rr%`%EUoNTR@6XJ+m_XWw)7KIi7&|Ni)K;Y9XMzjxl5ui(GWv71P_u2%6ufcyWn`aphgYD(VYStI&7lc z`!@3$Mxv3;J;?9_JcyAjI5oGZW=!eDo<@&dknz)T0h1KTAH}z4OUD_U)iHr{I?jtZ ziBmeR;Hn1b_yDJMc&Je>wo9qBtz%k{VtK>nKS^0qh759&vu(WOEVy*p!b`f`QI_SL z)G;oAS)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KLa zZlP8`MSfI-izLIMMyNzlF#^}fCUE(m;L{i@Er;>Th#X5PB4=`}f|Je@Sv-X?O5`#T zx$+hw+yzYJGYmbh)7|WgID?5ixtby&5;d+RYP6_IHs2&EbM==fJv&2)+M-5HQBM&u z6GcpX9xHNvj7`)aY?JvGn^sfAhBlRWGGlcXh!p>HMIzN@*D2tOBgLQ|n*Rg`TshVqX%@Z_MH}&Y< zqD&r>=;RXZ1R)H`-_Y;WGv%4zF*~a0J+ii$D!-8Uay!v6-C5<)S=mY@pLNz9qLjhf E|7Vj!#Q*>R diff --git a/plugins/sonar-squid-java-plugin/test-resources/bytecode/src/properties/JavaBean.java b/plugins/sonar-squid-java-plugin/test-resources/bytecode/src/properties/JavaBean.java index 4750fde8e08..ca2374734ed 100644 --- a/plugins/sonar-squid-java-plugin/test-resources/bytecode/src/properties/JavaBean.java +++ b/plugins/sonar-squid-java-plugin/test-resources/bytecode/src/properties/JavaBean.java @@ -9,6 +9,8 @@ public class JavaBean { ArrayList firstNames = new ArrayList(); private static String staticMember; private String FirstName; + private int myIncrement = 1; + private int myDifferentIncrement = 2; public String getName() { return name; @@ -68,4 +70,47 @@ public class JavaBean { return FirstName; } + public String getFirstNameAndOneArgument(String argument) { + return FirstName + " " + argument; + } + + public int recursiveAbs(int value) { + if (value < 0) { + return recursiveAbs(value + myIncrement); + } else return value; + } + + public int recursiveAbsNotAccessor(int value) { + if (value < 0) { + return recursiveAbs(value + myIncrement); + } else { + iShouldBeAStaticSetter(); + return value; + } + } + + public int recursiveAbsSameIncrementA(int value) { + if (value < 0) { + return recursiveAbsSameIncrementB(value + myIncrement); + } else return value; + } + + public int recursiveAbsSameIncrementB(int value) { + if (value < 0) { + return recursiveAbsSameIncrementA(value + myIncrement); + } else return value; + } + + public int recursiveAbsDifferentIncrementA(int value) { + if (value < 0) { + return recursiveAbsDifferentIncrementB(value + myIncrement); + } else return value; + } + + public int recursiveAbsDifferentIncrementB(int value) { + if (value < 0) { + return recursiveAbsSameIncrementA(value + myDifferentIncrement); + } else return value; + } + } -- 2.39.5