• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Static Analysis and AST Transformations
 

Static Analysis and AST Transformations

on

  • 910 views

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

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

Statistics

Views

Total Views
910
Views on SlideShare
909
Embed Views
1

Actions

Likes
0
Downloads
0
Comments
0

1 Embed 1

https://duckduckgo.com 1

Accessibility

Categories

Upload Details

Uploaded via as OpenOffice

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • About Me http://www.manning.com/koenig2/ http://hamletdarcy.blogspot.com Twitter: @HamletDRC Groovy, CodeNarc, JConch Committer GPars, Griffon, Gradle, etc. Contributor GroovyMag, NFJS magazine author JetBrains Academy Member
  • - Command line & CI integration - command line: need a valid .idea / .ipr file - http://www.jetbrains.com/idea/webhelp/running-inspections-offline.html - inspect.bat or inspect.sh in idea/bin - CI Integration: TeamCity has inspections built in
  • - Mention WebStorm for other inspections

Static Analysis and AST Transformations Static Analysis and AST Transformations Presentation Transcript

  • Main sponsor Static Analysis &AST TransformationsHamlet DArcy – @HamletDRC Canoo Engineering AG
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 2
  • About Mewww.jetbrains.com/idea 3
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 4
  • try { doSomething();} catch (UnsupportedOperationException e) { handleError(e);} catch (IllegalStateException e) { handleError(e);} catch (IllegalArgumentException e) { handleError(e);} www.jetbrains.com/idea 5
  • try { doSomething();} catch (UnsupportedOperationException | IllegalStateException | IllegalArgumentException e) { handleError(e);} www.jetbrains.com/idea 6
  • int readFirst(String path) throws Exception { FileReader reader = new FileReader(path); try { return reader.read(); } finally { reader.close(); }} www.jetbrains.com/idea 7
  • int readFirst(String path) throws Exception { try (FileReader reader = new FileReader(path)) { return reader.read(); } finally { reader.close(); }} www.jetbrains.com/idea 8
  • 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
  • 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
  • private static long count = 0L;synchronized void increment() { count++;} www.jetbrains.com/idea 11
  • private static long count = 0L;private static Object LOCK = new Object();void increment() { synchronized (LOCK) { count++; }} www.jetbrains.com/idea 12
  • private boolean active = false;boolean isActive() { return active;}synchronized void activate() { active = true;}www.jetbrains.com/idea 13
  • private boolean active = false;synchronized boolean isActive() { return active;}synchronized void activate() { active = true;}www.jetbrains.com/idea 14
  • 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
  • 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
  • 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
  • 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
  • CorrectnessMulti-threaded correctnessMalicious code vulnerabilityBad practiceInternationalizationPerformanceCode style violationsDodgy * Bill Pugh, FindBugswww.jetbrains.com/idea 19
  • IDEA Static AnalysisAccess to more than bytecodeAccess to parameter namesAccess to whitespaceAccess to parenthesis… and much morewww.jetbrains.com/idea 20
  • … 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
  • Supported FrameworksAndroid JSFAnt JSPApplication Server Junit Inspections LESSCDI(Contexts and Maven Dependency OSGi Injection) RELAX NGCSS SCSSFaces Modelwww.jetbrains.com/idea 22
  • 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
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 24
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 25
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 26
  • 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
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 28
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 29
  • 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
  • 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
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 32
  • How it WorksSearches AST for Bug Patternswww.jetbrains.com/idea 33
  • I shot an elephant in my pajamas. www.jetbrains.com/idea 34
  • Subject: Verb: Direct Object: Indirect Object: I shot an elephant in my pajamas
  • I shot an elephant in my pajamas.How he got in my pajamas,Ill never know. www.jetbrains.com/idea 36
  • Subject: Verb: Participle Phrase I shot an elephant in my pajamas
  • I want to thank my parents, Jesus and Oprah Winfreywww.jetbrains.com/idea 38
  • I want to thank my parents, Jesus and Oprah Winfreywww.jetbrains.com/idea 39
  • Subject: Verb: Infinitive: Participle: I want to thank my parents God Oprah Winfrey
  • I want to thank my parents, Jesus and Oprah Winfreywww.jetbrains.com/idea 41
  • I want to thank my parents, Jesus and Oprah WinfreyGod Oprahb. ? b. 1954 You www.jetbrains.com/idea b. 1976 42
  • Subject: Verb: Infinitive: Participle Phrase: I want to thank my parents God Oprah Winfrey
  • www.jetbrains.com/idea 44
  • www.jetbrains.com/idea 45
  • 2+3*4www.jetbrains.com/idea 46
  • 2+3*4 + * 23 4 www.jetbrains.com/idea 47
  • 2+3*4 + * * 2 + 43 4 2 3 www.jetbrains.com/idea 48
  • (+ 2 (* 3 4)) + * * 2 + 43 4 2 3 www.jetbrains.com/idea 49
  • www.jetbrains.com/idea 50
  • 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
  • How it WorksSearches AST for Bug Patternswww.jetbrains.com/idea 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
  • 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
  • Tree Pattern Matcher (PMD)//FinallyStatement//ReturnStatement//SynchronizedStatement/Block[1][count(*) = 0]//SwitchStatement[not(SwitchLabel[@Default=true])] www.jetbrains.com/idea 56
  • Structural Search and Replacewww.jetbrains.com/idea 57
  • Write Your OwnIntelliJ IDEA Static Analysis:Custom Rules with Structural Search & ReplaceOn http://JetBrains.tvwww.jetbrains.com/idea 58
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 59
  • www.jetbrains.com/idea 60
  • www.jetbrains.com/idea 61
  • www.jetbrains.com/idea 62
  • www.jetbrains.com/idea 63
  • www.jetbrains.com/idea 64
  • www.jetbrains.com/idea 65
  • www.jetbrains.com/idea 66
  • Software LifecycleIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0 … run in real-timewww.jetbrains.com/idea 67
  • Software LifecycleIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0 … run with buildwww.jetbrains.com/idea 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
  • What it isIDEA Inspections FindBugsPMD AndroidLintCodeNarc Groovy 2.0How it worksAST TransformationsRewriting Code XPath ExpressionsWhat is possibleLombok Groovywww.jetbrains.com/idea 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