Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Static Analysis and AST Transformations

1,471 views

Published on

Slides from 33rd Degree 2012 conference in Poland. This was my talk on Static Analyis and AST Transformations.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Static Analysis and AST Transformations

  1. 1. Main sponsor Static Analysis &AST TransformationsHamlet DArcy – @HamletDRC Canoo Engineering AG
  2. 2. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 2
  3. 3. About Mewww.jetbrains.com/idea 3
  4. 4. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 4
  5. 5. try { doSomething();} catch (UnsupportedOperationException e) { handleError(e);} catch (IllegalStateException e) { handleError(e);} catch (IllegalArgumentException e) { handleError(e);} www.jetbrains.com/idea 5
  6. 6. try { doSomething();} catch (UnsupportedOperationException | IllegalStateException | IllegalArgumentException e) { handleError(e);} www.jetbrains.com/idea 6
  7. 7. int readFirst(String path) throws Exception { FileReader reader = new FileReader(path); try { return reader.read(); } finally { reader.close(); }} www.jetbrains.com/idea 7
  8. 8. int readFirst(String path) throws Exception { try (FileReader reader = new FileReader(path)) { return reader.read(); } finally { reader.close(); }} www.jetbrains.com/idea 8
  9. 9. Frame makeFrame(int height, int width) { Frame frame = new Frame(); frame.setSize(height, width); return frame;}Rectangle makeRectangle() { int x = 0; int y = 0; return new Rectangle(y, x, 20, 20);} www.jetbrains.com/idea 9
  10. 10. Frame makeFrame(int height, int width) { Frame frame = new Frame(); frame.setSize(width, height); return frame;}Rectangle makeRectangle() { int x = 0; int y = 0; return new Rectangle(x, y, 20, 20);} www.jetbrains.com/idea 10
  11. 11. private static long count = 0L;synchronized void increment() { count++;} www.jetbrains.com/idea 11
  12. 12. private static long count = 0L;private static Object LOCK = new Object();void increment() { synchronized (LOCK) { count++; }} www.jetbrains.com/idea 12
  13. 13. private boolean active = false;boolean isActive() { return active;}synchronized void activate() { active = true;}www.jetbrains.com/idea 13
  14. 14. private boolean active = false;synchronized boolean isActive() { return active;}synchronized void activate() { active = true;}www.jetbrains.com/idea 14
  15. 15. private boolean active = false;private final ReentrantLock lock = new ReentrantLock();boolean isActive() { lock.lock(); boolean result = active; lock.unlock(); return result;} www.jetbrains.com/idea 15
  16. 16. private boolean active = false;private final ReentrantLock lock = new ReentrantLock();boolean isActive() { lock.lock(); try { return active; } finally { lock.unlock(); }} www.jetbrains.com/idea 16
  17. 17. private static final boolean DEFAULT = true; void myMethod(Boolean value) { if (value == null) System.out.println("value: null"); value = DEFAULT; System.out.println("received: " + value); } www.jetbrains.com/idea 17
  18. 18. private static final boolean DEFAULT = true; void myMethod(Boolean value) { if (value == null) { System.out.println("value: null"); value = DEFAULT; } System.out.println("received: " + value); } www.jetbrains.com/idea 18
  19. 19. CorrectnessMulti-threaded correctnessMalicious code vulnerabilityBad practiceInternationalizationPerformanceCode style violationsDodgy * Bill Pugh, FindBugswww.jetbrains.com/idea 19
  20. 20. IDEA Static AnalysisAccess to more than bytecodeAccess to parameter namesAccess to whitespaceAccess to parenthesis… and much morewww.jetbrains.com/idea 20
  21. 21. … and moreSuppress False PositivesDefine profiles and scopesRun on demand or one at a timeRun from command lineTeam City integrationFindBugs, PMD & CheckStyle pluginsLanguage and framework support...www.jetbrains.com/idea 21
  22. 22. Supported FrameworksAndroid JSFAnt JSPApplication Server Junit Inspections LESSCDI(Contexts and Maven Dependency OSGi Injection) RELAX NGCSS SCSSFaces Modelwww.jetbrains.com/idea 22
  23. 23. 10 Best Unknown InspectionsIllegal package dependencies return of collection or arraythis reference escapes field constructor call to Thread.run()Field accessed in both expression.equals("literal") synched & unsynched rather than contexts "literal".equals(expression)non private field accessed in equals method does not check synched context class of parameterSynchronization on this and method may be static synchronized methodhttp://hamletdarcy.blogspot.com/2008/04/10-best-idea-inspections-youre-not.htmlwww.jetbrains.com/idea 23
  24. 24. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 24
  25. 25. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 25
  26. 26. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 26
  27. 27. AndroidLintInconsistent Arrays Duplicate iconsReference to an ID Design issues like ... that is not in the and (c), etc current layout and many moreHashMap can be resource issues replaced with SparseArrayUnused Resourceswww.jetbrains.com/idea 27
  28. 28. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 28
  29. 29. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 29
  30. 30. FindBugs vs PMD vs IDEAIDEA has tons of inspections, quickfixes, and TeamCity integrationDedicated IDEA shops dont need othersIDEA not always easy to run with build/CIIDEA inspections arent easy to use from EclipseFindBugs literally finds bugs. PMD is more best practiceswww.jetbrains.com/idea 30
  31. 31. QAPlug vs. Dedicated PluginsQAPlug - Can run for Uncommitted FilesQAPlug - Nicer user interfaceQAPlug gives you less control over rulesets and rulesDedicated plugins are a little easier to share config files withwww.jetbrains.com/idea 31
  32. 32. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 32
  33. 33. How it WorksSearches AST for Bug Patternswww.jetbrains.com/idea 33
  34. 34. I shot an elephant in my pajamas. www.jetbrains.com/idea 34
  35. 35. Subject: Verb: Direct Object: Indirect Object: I shot an elephant in my pajamas
  36. 36. I shot an elephant in my pajamas.How he got in my pajamas,Ill never know. www.jetbrains.com/idea 36
  37. 37. Subject: Verb: Participle Phrase I shot an elephant in my pajamas
  38. 38. I want to thank my parents, Jesus and Oprah Winfreywww.jetbrains.com/idea 38
  39. 39. I want to thank my parents, Jesus and Oprah Winfreywww.jetbrains.com/idea 39
  40. 40. Subject: Verb: Infinitive: Participle: I want to thank my parents God Oprah Winfrey
  41. 41. I want to thank my parents, Jesus and Oprah Winfreywww.jetbrains.com/idea 41
  42. 42. I want to thank my parents, Jesus and Oprah WinfreyGod Oprahb. ? b. 1954 You www.jetbrains.com/idea b. 1976 42
  43. 43. Subject: Verb: Infinitive: Participle Phrase: I want to thank my parents God Oprah Winfrey
  44. 44. www.jetbrains.com/idea 44
  45. 45. www.jetbrains.com/idea 45
  46. 46. 2+3*4www.jetbrains.com/idea 46
  47. 47. 2+3*4 + * 23 4 www.jetbrains.com/idea 47
  48. 48. 2+3*4 + * * 2 + 43 4 2 3 www.jetbrains.com/idea 48
  49. 49. (+ 2 (* 3 4)) + * * 2 + 43 4 2 3 www.jetbrains.com/idea 49
  50. 50. www.jetbrains.com/idea 50
  51. 51. public class Person { private String name; public void setName(String name) { this.name = name; } public String getNameName() { return name; } public static void main(String[] args) { Person p = new Person(); p.setName(“Hamlet”); System.out.println(p); }} 51www.jetbrains.com/idea
  52. 52. How it WorksSearches AST for Bug Patternswww.jetbrains.com/idea 53
  53. 53. How it Works@Overridepublic void visitMethod(@NotNull final PsiMethod method) { super.visitMethod(method); if (method.hasModifierProperty(PsiModifier.ABSTRACT)) { return; } if (!RecursionUtils.methodMayRecurse(method)) { return; } if (!RecursionUtils.methodDefinitelyRecurses(method)) { return; } super.registerMethodError(method);} www.jetbrains.com/idea 54
  54. 54. How it Works@Overridepublic void visitIfStatement(GrIfStatement stmt) { super.visitIfStatement(stmt); int branches = calculateNumBranches(stmt); if (branches <= getLimit()) { return; } registerStatementError(stmt, stmt);} www.jetbrains.com/idea 55
  55. 55. Tree Pattern Matcher (PMD)//FinallyStatement//ReturnStatement//SynchronizedStatement/Block[1][count(*) = 0]//SwitchStatement[not(SwitchLabel[@Default=true])] www.jetbrains.com/idea 56
  56. 56. Structural Search and Replacewww.jetbrains.com/idea 57
  57. 57. Write Your OwnIntelliJ IDEA Static Analysis:Custom Rules with Structural Search & ReplaceOn http://JetBrains.tvwww.jetbrains.com/idea 58
  58. 58. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 59
  59. 59. www.jetbrains.com/idea 60
  60. 60. www.jetbrains.com/idea 61
  61. 61. www.jetbrains.com/idea 62
  62. 62. www.jetbrains.com/idea 63
  63. 63. www.jetbrains.com/idea 64
  64. 64. www.jetbrains.com/idea 65
  65. 65. www.jetbrains.com/idea 66
  66. 66. Software LifecycleIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0 … run in real-timewww.jetbrains.com/idea 67
  67. 67. Software LifecycleIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0 … run with buildwww.jetbrains.com/idea 68
  68. 68. Not Covered@Immutable, @GuardedBy@Pattern & @Language@Nls, @NonNls, @PropertyKeyDuplicate Detection & Dataflow AnalysisDependency Analysis & Dependency Structure MatrixThat was last year:http://www.slideshare.net/HamletDRC/static-analysis-in-ideawww.jetbrains.com/idea 69
  69. 69. What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 70
  70. 70. Learn More – Q & AMy JetBrains.tv Screencasts: http://tv.jetbrains.net/tags/hamletMy IDEA blog: http://hamletdarcy.blogspot.com/search/label/IDEAWorks IDEA blog: http://www.canoo.com/blog/tag/idea/Main blog: http://hamletdarcy.blogspot.comYouTube channel: http://www.youtube.com/user/HamletDRCTwitter: http://twitter.com/hamletdrcIDEA RefCard from DZone: http://goo.gl/Fg4AfIDEA Keyboard Stickers: See meShare-a-Canooie – http://people.canoo.com/share/Hackergarten – http://www.hackergarten.net/ www.jetbrains.com/idea 71

×