Static Analysis and AST Transformations

H
Main sponsor




  Static Analysis &
AST Transformations
Hamlet D'Arcy – @HamletDRC
   Canoo Engineering AG
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.jetbrains.com/idea                       2
About Me




www.jetbrains.com/idea   3
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.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
Correctness
Multi-threaded correctness
Malicious code vulnerability
Bad practice
Internationalization
Performance
Code style violations
Dodgy
                       * Bill Pugh, FindBugs
www.jetbrains.com/idea                    19
IDEA Static Analysis
Access to more than bytecode
Access to parameter names
Access to whitespace
Access to parenthesis
… and much more




www.jetbrains.com/idea         20
… and more
Suppress False Positives
Define profiles and scopes
Run on demand or one at a time
Run from command line
Team City integration
FindBugs, PMD & CheckStyle plugins
Language and framework support...


www.jetbrains.com/idea               21
Supported Frameworks
Android                  JSF
Ant                      JSP
Application Server       Junit
  Inspections            LESS
CDI(Contexts and         Maven
  Dependency             OSGi
  Injection)
                         RELAX NG
CSS
                         SCSS
Faces Model
www.jetbrains.com/idea              22
10 Best Unknown Inspections
Illegal package dependencies           return of collection or array
'this' 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 parameter
Synchronization on 'this' and          method may be static
    'synchronized' method


http://hamletdarcy.blogspot.com/2008/04/10-best-idea-inspections-youre-not.html

www.jetbrains.com/idea                                                     23
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.jetbrains.com/idea                       24
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.jetbrains.com/idea                       25
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.jetbrains.com/idea                       26
AndroidLint
Inconsistent Arrays      Duplicate icons
Reference to an ID       Design issues like ...
  that is not in the       and (c), etc
  current layout         and many more
HashMap can be             resource issues
  replaced with
  SparseArray
Unused Resources

www.jetbrains.com/idea                        27
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.jetbrains.com/idea                       28
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.jetbrains.com/idea                       29
FindBugs vs PMD vs IDEA
IDEA has tons of inspections, quickfixes, and
  TeamCity integration
Dedicated IDEA shops don't need others
IDEA not always easy to run with build/CI
IDEA inspections aren't easy to use from
  Eclipse
FindBugs literally finds bugs. PMD is more
  best practices

www.jetbrains.com/idea                     30
QAPlug vs. Dedicated Plugins
QAPlug - Can run for Uncommitted Files
QAPlug - Nicer user interface
QAPlug gives you less control over rulesets
 and rules
Dedicated plugins are a little easier to
 share config files with




www.jetbrains.com/idea                    31
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.jetbrains.com/idea                       32
How it Works
Searches AST for Bug Patterns




www.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,
I'll 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 Winfrey




www.jetbrains.com/idea                  38
I want to thank my parents,
            Jesus and Oprah Winfrey




www.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 Winfrey




www.jetbrains.com/idea                  41
I want to thank my parents,
             Jesus and Oprah Winfrey


God                                      Oprah
b. ?                                     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*4




www.jetbrains.com/idea           46
2+3*4

                       +
           *                 2

3                      4
    www.jetbrains.com/idea           47
2+3*4

                       +                 *
           *                 2       +       4

3                      4         2       3
    www.jetbrains.com/idea                   48
(+ 2 (* 3 4))

                       +                         *
           *                  2              +       4

3                      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);
      }
}                                                51
www.jetbrains.com/idea
Static Analysis and AST Transformations
How it Works
Searches AST for Bug Patterns




www.jetbrains.com/idea          53
How it Works
@Override
public 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

@Override
public 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 Replace




www.jetbrains.com/idea          57
Write Your Own


IntelliJ IDEA Static Analysis:
Custom Rules with Structural Search & Replace

On http://JetBrains.tv



www.jetbrains.com/idea                     58
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.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 Lifecycle
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0

                         … run in real-time




www.jetbrains.com/idea                    67
Software Lifecycle
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0

                          … run with build




www.jetbrains.com/idea                   68
Not Covered
@Immutable, @GuardedBy
@Pattern & @Language
@Nls, @NonNls, @PropertyKey
Duplicate Detection & Dataflow Analysis
Dependency Analysis & Dependency
 Structure Matrix

That was last year:
http://www.slideshare.net/HamletDRC/static-analysis-in-idea
www.jetbrains.com/idea                                   69
What it is
IDEA Inspections         FindBugs
PMD                      AndroidLint
CodeNarc                 Groovy 2.0
How it works
AST                      Transformations
Rewriting Code           XPath Expressions

What is possible
Lombok                   Groovy
www.jetbrains.com/idea                       70
Learn More – Q & A
My JetBrains.tv Screencasts: http://tv.jetbrains.net/tags/hamlet
My IDEA blog: http://hamletdarcy.blogspot.com/search/label/IDEA
Work's IDEA blog: http://www.canoo.com/blog/tag/idea/
Main blog: http://hamletdarcy.blogspot.com
YouTube channel: http://www.youtube.com/user/HamletDRC
Twitter: http://twitter.com/hamletdrc
IDEA RefCard from DZone: http://goo.gl/Fg4Af
IDEA Keyboard Stickers: See me

Share-a-Canooie – http://people.canoo.com/share/
Hackergarten – http://www.hackergarten.net/
     www.jetbrains.com/idea                                  71
1 of 71

Recommended

Static Analysis in IDEA by
Static Analysis in IDEAStatic Analysis in IDEA
Static Analysis in IDEAHamletDRC
1.5K views56 slides
BabelJS - James Kyle at Modern Web UI by
BabelJS - James Kyle at Modern Web UIBabelJS - James Kyle at Modern Web UI
BabelJS - James Kyle at Modern Web UImodernweb
852 views102 slides
A Life of breakpoint by
A Life of breakpointA Life of breakpoint
A Life of breakpointHajime Morrita
1.5K views31 slides
Clean Code Development by
Clean Code DevelopmentClean Code Development
Clean Code DevelopmentPeter Gfader
2.6K views94 slides
Groovy 2.0 webinar by
Groovy 2.0 webinarGroovy 2.0 webinar
Groovy 2.0 webinarGuillaume Laforge
2.8K views57 slides
Groovy Update - JavaPolis 2007 by
Groovy Update - JavaPolis 2007Groovy Update - JavaPolis 2007
Groovy Update - JavaPolis 2007Guillaume Laforge
738 views47 slides

More Related Content

What's hot

PVS-Studio is there to help CERN: analysis of Geant4 project by
PVS-Studio is there to help CERN: analysis of Geant4 projectPVS-Studio is there to help CERN: analysis of Geant4 project
PVS-Studio is there to help CERN: analysis of Geant4 projectPVS-Studio
58 views12 slides
Lift off with Groovy 2 at JavaOne 2013 by
Lift off with Groovy 2 at JavaOne 2013Lift off with Groovy 2 at JavaOne 2013
Lift off with Groovy 2 at JavaOne 2013Guillaume Laforge
3.8K views179 slides
Automated Patching for Vulnerable Source Code by
Automated Patching for Vulnerable Source CodeAutomated Patching for Vulnerable Source Code
Automated Patching for Vulnerable Source CodeVladimir Kochetkov
1.3K views60 slides
Дмитрий Нестерук, Паттерны проектирования в XXI веке by
Дмитрий Нестерук, Паттерны проектирования в XXI векеДмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI векеSergey Platonov
1.4K views74 slides
Unit testing without Robolectric, Droidcon Berlin 2016 by
Unit testing without Robolectric, Droidcon Berlin 2016Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016Danny Preussler
7.5K views60 slides
Visualizing MVC, and an introduction to Giotto by
Visualizing MVC, and an introduction to GiottoVisualizing MVC, and an introduction to Giotto
Visualizing MVC, and an introduction to Giottopriestc
1.6K views32 slides

What's hot(20)

PVS-Studio is there to help CERN: analysis of Geant4 project by PVS-Studio
PVS-Studio is there to help CERN: analysis of Geant4 projectPVS-Studio is there to help CERN: analysis of Geant4 project
PVS-Studio is there to help CERN: analysis of Geant4 project
PVS-Studio58 views
Lift off with Groovy 2 at JavaOne 2013 by Guillaume Laforge
Lift off with Groovy 2 at JavaOne 2013Lift off with Groovy 2 at JavaOne 2013
Lift off with Groovy 2 at JavaOne 2013
Guillaume Laforge3.8K views
Automated Patching for Vulnerable Source Code by Vladimir Kochetkov
Automated Patching for Vulnerable Source CodeAutomated Patching for Vulnerable Source Code
Automated Patching for Vulnerable Source Code
Vladimir Kochetkov1.3K views
Дмитрий Нестерук, Паттерны проектирования в XXI веке by Sergey Platonov
Дмитрий Нестерук, Паттерны проектирования в XXI векеДмитрий Нестерук, Паттерны проектирования в XXI веке
Дмитрий Нестерук, Паттерны проектирования в XXI веке
Sergey Platonov1.4K views
Unit testing without Robolectric, Droidcon Berlin 2016 by Danny Preussler
Unit testing without Robolectric, Droidcon Berlin 2016Unit testing without Robolectric, Droidcon Berlin 2016
Unit testing without Robolectric, Droidcon Berlin 2016
Danny Preussler7.5K views
Visualizing MVC, and an introduction to Giotto by priestc
Visualizing MVC, and an introduction to GiottoVisualizing MVC, and an introduction to Giotto
Visualizing MVC, and an introduction to Giotto
priestc1.6K views
Антон Нонко, Классические строки в C++ by Sergey Platonov
Антон Нонко, Классические строки в C++Антон Нонко, Классические строки в C++
Антон Нонко, Классические строки в C++
Sergey Platonov963 views
Alexey Sintsov- SDLC - try me to implement by DefconRussia
Alexey Sintsov- SDLC - try me to implementAlexey Sintsov- SDLC - try me to implement
Alexey Sintsov- SDLC - try me to implement
DefconRussia1.1K views
How to write clean & testable code without losing your mind by Andreas Czakaj
How to write clean & testable code without losing your mindHow to write clean & testable code without losing your mind
How to write clean & testable code without losing your mind
Andreas Czakaj225 views
Much ado about randomness. What is really a random number? by Aleksandr Yampolskiy
Much ado about randomness. What is really a random number?Much ado about randomness. What is really a random number?
Much ado about randomness. What is really a random number?
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ... by Christopher Frohoff
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...
OWASP SD: Deserialize My Shorts: Or How I Learned To Start Worrying and Hate ...
Christopher Frohoff26.5K views
Java Bytecode Fundamentals - JUG.lv by Anton Arhipov
Java Bytecode Fundamentals - JUG.lvJava Bytecode Fundamentals - JUG.lv
Java Bytecode Fundamentals - JUG.lv
Anton Arhipov1.6K views
From C++ to Objective-C by corehard_by
From C++ to Objective-CFrom C++ to Objective-C
From C++ to Objective-C
corehard_by287 views
New methods for exploiting ORM injections in Java applications by Mikhail Egorov
New methods for exploiting ORM injections in Java applicationsNew methods for exploiting ORM injections in Java applications
New methods for exploiting ORM injections in Java applications
Mikhail Egorov12.1K views
The operation principles of PVS-Studio static code analyzer by Andrey Karpov
The operation principles of PVS-Studio static code analyzerThe operation principles of PVS-Studio static code analyzer
The operation principles of PVS-Studio static code analyzer
Andrey Karpov624 views
OWASP Poland Day 2018 - Pedro Fortuna - Are your Java Script based protection... by OWASP
OWASP Poland Day 2018 - Pedro Fortuna - Are your Java Script based protection...OWASP Poland Day 2018 - Pedro Fortuna - Are your Java Script based protection...
OWASP Poland Day 2018 - Pedro Fortuna - Are your Java Script based protection...
OWASP135 views

Viewers also liked

10 Years of Groovy by
10 Years of Groovy10 Years of Groovy
10 Years of GroovyHamletDRC
1.1K views76 slides
New Ideas for Old Code - Greach by
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - GreachHamletDRC
1.1K views81 slides
Презентация Программы лингвистического обеспечения города Сочи by
Презентация Программы лингвистического обеспечения города СочиПрезентация Программы лингвистического обеспечения города Сочи
Презентация Программы лингвистического обеспечения города СочиABBYY Language Serivces
702 views13 slides
Is MT ready for e-Government? The Latvian Story. Indra Samite, Tilde by
Is MT ready for e-Government? The Latvian Story. Indra Samite, TildeIs MT ready for e-Government? The Latvian Story. Indra Samite, Tilde
Is MT ready for e-Government? The Latvian Story. Indra Samite, TildeABBYY Language Serivces
1.3K views57 slides
Translation Automation Going Cloud: The New Landscape for Professional Transl... by
Translation Automation Going Cloud: The New Landscape for Professional Transl...Translation Automation Going Cloud: The New Landscape for Professional Transl...
Translation Automation Going Cloud: The New Landscape for Professional Transl...ABBYY Language Serivces
945 views11 slides
33rd degree conference by
33rd degree conference33rd degree conference
33rd degree conferencepcmanus
1K views45 slides

Viewers also liked(20)

10 Years of Groovy by HamletDRC
10 Years of Groovy10 Years of Groovy
10 Years of Groovy
HamletDRC1.1K views
New Ideas for Old Code - Greach by HamletDRC
New Ideas for Old Code - GreachNew Ideas for Old Code - Greach
New Ideas for Old Code - Greach
HamletDRC1.1K views
Презентация Программы лингвистического обеспечения города Сочи by ABBYY Language Serivces
Презентация Программы лингвистического обеспечения города СочиПрезентация Программы лингвистического обеспечения города Сочи
Презентация Программы лингвистического обеспечения города Сочи
Is MT ready for e-Government? The Latvian Story. Indra Samite, Tilde by ABBYY Language Serivces
Is MT ready for e-Government? The Latvian Story. Indra Samite, TildeIs MT ready for e-Government? The Latvian Story. Indra Samite, Tilde
Is MT ready for e-Government? The Latvian Story. Indra Samite, Tilde
Translation Automation Going Cloud: The New Landscape for Professional Transl... by ABBYY Language Serivces
Translation Automation Going Cloud: The New Landscape for Professional Transl...Translation Automation Going Cloud: The New Landscape for Professional Transl...
Translation Automation Going Cloud: The New Landscape for Professional Transl...
33rd degree conference by pcmanus
33rd degree conference33rd degree conference
33rd degree conference
pcmanus1K views
Castelao by Marlou
CastelaoCastelao
Castelao
Marlou757 views
example 5 by jennymann
example 5example 5
example 5
jennymann493 views
מאבק נכי צהל by guest446e83c
מאבק נכי צהלמאבק נכי צהל
מאבק נכי צהל
guest446e83c249 views
Gita Study Nov 7 Dr. Shriniwas Kashalikar by ppkalghatgi
Gita Study Nov 7  Dr. Shriniwas KashalikarGita Study Nov 7  Dr. Shriniwas Kashalikar
Gita Study Nov 7 Dr. Shriniwas Kashalikar
ppkalghatgi57 views
Trabalhos finais oa12 ut6 2010 2011 by amarques_1
Trabalhos finais oa12 ut6 2010 2011Trabalhos finais oa12 ut6 2010 2011
Trabalhos finais oa12 ut6 2010 2011
amarques_1363 views
Holistic Health Examination Dr Shriniwas Kashalikar by ppkalghatgi
Holistic Health Examination Dr Shriniwas KashalikarHolistic Health Examination Dr Shriniwas Kashalikar
Holistic Health Examination Dr Shriniwas Kashalikar
ppkalghatgi88 views
ELIA - Together: The growth from freelancer to translation company - Anja Jones by anjajones
ELIA - Together: The growth from freelancer to translation company - Anja JonesELIA - Together: The growth from freelancer to translation company - Anja Jones
ELIA - Together: The growth from freelancer to translation company - Anja Jones
anjajones1.6K views
example 4 by jennymann
example 4example 4
example 4
jennymann269 views

Similar to Static Analysis and AST Transformations

All of Javascript by
All of JavascriptAll of Javascript
All of JavascriptTogakangaroo
868 views40 slides
All of javascript by
All of javascriptAll of javascript
All of javascriptTogakangaroo
1.7K views41 slides
Js tacktalk team dev js testing performance by
Js tacktalk team dev js testing performanceJs tacktalk team dev js testing performance
Js tacktalk team dev js testing performanceАртем Захарченко
626 views49 slides
Dark Side of iOS [SmartDevCon 2013] by
Dark Side of iOS [SmartDevCon 2013]Dark Side of iOS [SmartDevCon 2013]
Dark Side of iOS [SmartDevCon 2013]Kuba Břečka
1.8K views23 slides
Y U NO CRAFTSMAN by
Y U NO CRAFTSMANY U NO CRAFTSMAN
Y U NO CRAFTSMANPaul Blundell
4.8K views66 slides
Everything as a Code / Александр Тарасов (Одноклассники) by
Everything as a Code / Александр Тарасов (Одноклассники)Everything as a Code / Александр Тарасов (Одноклассники)
Everything as a Code / Александр Тарасов (Одноклассники)Ontico
492 views197 slides

Similar to Static Analysis and AST Transformations(20)

All of javascript by Togakangaroo
All of javascriptAll of javascript
All of javascript
Togakangaroo1.7K views
Dark Side of iOS [SmartDevCon 2013] by Kuba Břečka
Dark Side of iOS [SmartDevCon 2013]Dark Side of iOS [SmartDevCon 2013]
Dark Side of iOS [SmartDevCon 2013]
Kuba Břečka1.8K views
Everything as a Code / Александр Тарасов (Одноклассники) by Ontico
Everything as a Code / Александр Тарасов (Одноклассники)Everything as a Code / Александр Тарасов (Одноклассники)
Everything as a Code / Александр Тарасов (Одноклассники)
Ontico492 views
Smell your Code! @ Free Dimension by Yaser Sulaiman
Smell your Code! @ Free DimensionSmell your Code! @ Free Dimension
Smell your Code! @ Free Dimension
Yaser Sulaiman621 views
WebApps e Frameworks Javascript by meet2Brains
WebApps e Frameworks JavascriptWebApps e Frameworks Javascript
WebApps e Frameworks Javascript
meet2Brains791 views
Model-driven Round-trip Engineering of REST APIs by Jordi Cabot
Model-driven Round-trip Engineering of REST APIsModel-driven Round-trip Engineering of REST APIs
Model-driven Round-trip Engineering of REST APIs
Jordi Cabot1.8K views
Manipulating object-behavior-at-runtime by Andrei Ursan
Manipulating object-behavior-at-runtimeManipulating object-behavior-at-runtime
Manipulating object-behavior-at-runtime
Andrei Ursan287 views
How to really obfuscate your pdf malware by zynamics GmbH
How to really obfuscate   your pdf malwareHow to really obfuscate   your pdf malware
How to really obfuscate your pdf malware
zynamics GmbH3.5K views
How to really obfuscate your pdf malware by zynamics GmbH
How to really obfuscate your pdf malwareHow to really obfuscate your pdf malware
How to really obfuscate your pdf malware
zynamics GmbH1.6K views
Java 7 - New Features - by Mihail Stoynov and Svetlin Nakov by Svetlin Nakov
Java 7 - New Features - by Mihail Stoynov and Svetlin NakovJava 7 - New Features - by Mihail Stoynov and Svetlin Nakov
Java 7 - New Features - by Mihail Stoynov and Svetlin Nakov
Svetlin Nakov2.3K views
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo... by Hafez Kamal
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
NanoSec Conference 2019: Code Execution Analysis in Mobile Apps - Abdullah Jo...
Hafez Kamal155 views
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge by Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Guillaume Laforge3.8K views
Professional JavaScript: AntiPatterns by Mike Wilcox
Professional JavaScript: AntiPatternsProfessional JavaScript: AntiPatterns
Professional JavaScript: AntiPatterns
Mike Wilcox4K views

More from HamletDRC

AST Transformations at JFokus by
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokusHamletDRC
945 views101 slides
Java Boilerplate Busters by
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate BustersHamletDRC
465 views108 slides
Groovy Ast Transformations (greach) by
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)HamletDRC
1.8K views88 slides
AST Transformations by
AST TransformationsAST Transformations
AST TransformationsHamletDRC
1.2K views87 slides
Java Boilerplate Busters by
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate BustersHamletDRC
1.3K views108 slides
Ast transformations by
Ast transformationsAst transformations
Ast transformationsHamletDRC
791 views75 slides

More from HamletDRC(6)

AST Transformations at JFokus by HamletDRC
AST Transformations at JFokusAST Transformations at JFokus
AST Transformations at JFokus
HamletDRC945 views
Java Boilerplate Busters by HamletDRC
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate Busters
HamletDRC465 views
Groovy Ast Transformations (greach) by HamletDRC
Groovy Ast Transformations (greach)Groovy Ast Transformations (greach)
Groovy Ast Transformations (greach)
HamletDRC1.8K views
AST Transformations by HamletDRC
AST TransformationsAST Transformations
AST Transformations
HamletDRC1.2K views
Java Boilerplate Busters by HamletDRC
Java Boilerplate BustersJava Boilerplate Busters
Java Boilerplate Busters
HamletDRC1.3K views
Ast transformations by HamletDRC
Ast transformationsAst transformations
Ast transformations
HamletDRC791 views

Recently uploaded

Throughput by
ThroughputThroughput
ThroughputMoisés Armani Ramírez
32 views11 slides
JCon Live 2023 - Lice coding some integration problems by
JCon Live 2023 - Lice coding some integration problemsJCon Live 2023 - Lice coding some integration problems
JCon Live 2023 - Lice coding some integration problemsBernd Ruecker
67 views85 slides
Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa... by
Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa...Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa...
Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa...The Digital Insurer
28 views18 slides
Future of Learning - Yap Aye Wee.pdf by
Future of Learning - Yap Aye Wee.pdfFuture of Learning - Yap Aye Wee.pdf
Future of Learning - Yap Aye Wee.pdfNUS-ISS
38 views11 slides
MemVerge: Past Present and Future of CXL by
MemVerge: Past Present and Future of CXLMemVerge: Past Present and Future of CXL
MemVerge: Past Present and Future of CXLCXL Forum
110 views26 slides
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen... by
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...NUS-ISS
23 views70 slides

Recently uploaded(20)

JCon Live 2023 - Lice coding some integration problems by Bernd Ruecker
JCon Live 2023 - Lice coding some integration problemsJCon Live 2023 - Lice coding some integration problems
JCon Live 2023 - Lice coding some integration problems
Bernd Ruecker67 views
Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa... by The Digital Insurer
Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa...Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa...
Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa...
Future of Learning - Yap Aye Wee.pdf by NUS-ISS
Future of Learning - Yap Aye Wee.pdfFuture of Learning - Yap Aye Wee.pdf
Future of Learning - Yap Aye Wee.pdf
NUS-ISS38 views
MemVerge: Past Present and Future of CXL by CXL Forum
MemVerge: Past Present and Future of CXLMemVerge: Past Present and Future of CXL
MemVerge: Past Present and Future of CXL
CXL Forum110 views
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen... by NUS-ISS
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...
Upskilling the Evolving Workforce with Digital Fluency for Tomorrow's Challen...
NUS-ISS23 views
CXL at OCP by CXL Forum
CXL at OCPCXL at OCP
CXL at OCP
CXL Forum208 views
Samsung: CMM-H Tiered Memory Solution with Built-in DRAM by CXL Forum
Samsung: CMM-H Tiered Memory Solution with Built-in DRAMSamsung: CMM-H Tiered Memory Solution with Built-in DRAM
Samsung: CMM-H Tiered Memory Solution with Built-in DRAM
CXL Forum105 views
"How we switched to Kanban and how it integrates with product planning", Vady... by Fwdays
"How we switched to Kanban and how it integrates with product planning", Vady..."How we switched to Kanban and how it integrates with product planning", Vady...
"How we switched to Kanban and how it integrates with product planning", Vady...
Fwdays61 views
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad... by Fwdays
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad..."Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad...
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad...
Fwdays40 views
.conf Go 2023 - Data analysis as a routine by Splunk
.conf Go 2023 - Data analysis as a routine.conf Go 2023 - Data analysis as a routine
.conf Go 2023 - Data analysis as a routine
Splunk90 views
Understanding GenAI/LLM and What is Google Offering - Felix Goh by NUS-ISS
Understanding GenAI/LLM and What is Google Offering - Felix GohUnderstanding GenAI/LLM and What is Google Offering - Felix Goh
Understanding GenAI/LLM and What is Google Offering - Felix Goh
NUS-ISS39 views
[2023] Putting the R! in R&D.pdf by Eleanor McHugh
[2023] Putting the R! in R&D.pdf[2023] Putting the R! in R&D.pdf
[2023] Putting the R! in R&D.pdf
Eleanor McHugh38 views
Combining Orchestration and Choreography for a Clean Architecture by ThomasHeinrichs1
Combining Orchestration and Choreography for a Clean ArchitectureCombining Orchestration and Choreography for a Clean Architecture
Combining Orchestration and Choreography for a Clean Architecture
ThomasHeinrichs168 views
The details of description: Techniques, tips, and tangents on alternative tex... by BookNet Canada
The details of description: Techniques, tips, and tangents on alternative tex...The details of description: Techniques, tips, and tangents on alternative tex...
The details of description: Techniques, tips, and tangents on alternative tex...
BookNet Canada110 views
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi113 views
AI: mind, matter, meaning, metaphors, being, becoming, life values by Twain Liu 刘秋艳
AI: mind, matter, meaning, metaphors, being, becoming, life valuesAI: mind, matter, meaning, metaphors, being, becoming, life values
AI: mind, matter, meaning, metaphors, being, becoming, life values
.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV by Splunk
.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV
.conf Go 2023 - How KPN drives Customer Satisfaction on IPTV
Splunk86 views
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor... by Vadym Kazulkin
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
How to reduce cold starts for Java Serverless applications in AWS at JCON Wor...
Vadym Kazulkin70 views

Static Analysis and AST Transformations

  • 1. Main sponsor Static Analysis & AST Transformations Hamlet D'Arcy – @HamletDRC Canoo Engineering AG
  • 2. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 2
  • 4. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 4
  • 5. try { doSomething(); } catch (UnsupportedOperationException e) { handleError(e); } catch (IllegalStateException e) { handleError(e); } catch (IllegalArgumentException e) { handleError(e); } www.jetbrains.com/idea 5
  • 6. try { doSomething(); } catch (UnsupportedOperationException | IllegalStateException | IllegalArgumentException e) { handleError(e); } www.jetbrains.com/idea 6
  • 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. int readFirst(String path) throws Exception { try (FileReader reader = new FileReader(path)) { return reader.read(); } finally { reader.close(); } } www.jetbrains.com/idea 8
  • 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. 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. private static long count = 0L; synchronized void increment() { count++; } www.jetbrains.com/idea 11
  • 12. private static long count = 0L; private static Object LOCK = new Object(); void increment() { synchronized (LOCK) { count++; } } www.jetbrains.com/idea 12
  • 13. private boolean active = false; boolean isActive() { return active; } synchronized void activate() { active = true; } www.jetbrains.com/idea 13
  • 14. private boolean active = false; synchronized boolean isActive() { return active; } synchronized void activate() { active = true; } www.jetbrains.com/idea 14
  • 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. 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. 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. 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. Correctness Multi-threaded correctness Malicious code vulnerability Bad practice Internationalization Performance Code style violations Dodgy * Bill Pugh, FindBugs www.jetbrains.com/idea 19
  • 20. IDEA Static Analysis Access to more than bytecode Access to parameter names Access to whitespace Access to parenthesis … and much more www.jetbrains.com/idea 20
  • 21. … and more Suppress False Positives Define profiles and scopes Run on demand or one at a time Run from command line Team City integration FindBugs, PMD & CheckStyle plugins Language and framework support... www.jetbrains.com/idea 21
  • 22. Supported Frameworks Android JSF Ant JSP Application Server Junit Inspections LESS CDI(Contexts and Maven Dependency OSGi Injection) RELAX NG CSS SCSS Faces Model www.jetbrains.com/idea 22
  • 23. 10 Best Unknown Inspections Illegal package dependencies return of collection or array 'this' 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 parameter Synchronization on 'this' and method may be static 'synchronized' method http://hamletdarcy.blogspot.com/2008/04/10-best-idea-inspections-youre-not.html www.jetbrains.com/idea 23
  • 24. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 24
  • 25. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 25
  • 26. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 26
  • 27. AndroidLint Inconsistent Arrays Duplicate icons Reference to an ID Design issues like ... that is not in the and (c), etc current layout and many more HashMap can be resource issues replaced with SparseArray Unused Resources www.jetbrains.com/idea 27
  • 28. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 28
  • 29. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 29
  • 30. FindBugs vs PMD vs IDEA IDEA has tons of inspections, quickfixes, and TeamCity integration Dedicated IDEA shops don't need others IDEA not always easy to run with build/CI IDEA inspections aren't easy to use from Eclipse FindBugs literally finds bugs. PMD is more best practices www.jetbrains.com/idea 30
  • 31. QAPlug vs. Dedicated Plugins QAPlug - Can run for Uncommitted Files QAPlug - Nicer user interface QAPlug gives you less control over rulesets and rules Dedicated plugins are a little easier to share config files with www.jetbrains.com/idea 31
  • 32. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 32
  • 33. How it Works Searches AST for Bug Patterns www.jetbrains.com/idea 33
  • 34. I shot an elephant in my pajamas. www.jetbrains.com/idea 34
  • 35. Subject: Verb: Direct Object: Indirect Object: I shot an elephant in my pajamas
  • 36. I shot an elephant in my pajamas. How he got in my pajamas, I'll never know. www.jetbrains.com/idea 36
  • 37. Subject: Verb: Participle Phrase I shot an elephant in my pajamas
  • 38. I want to thank my parents, Jesus and Oprah Winfrey www.jetbrains.com/idea 38
  • 39. I want to thank my parents, Jesus and Oprah Winfrey www.jetbrains.com/idea 39
  • 40. Subject: Verb: Infinitive: Participle: I want to thank my parents God Oprah Winfrey
  • 41. I want to thank my parents, Jesus and Oprah Winfrey www.jetbrains.com/idea 41
  • 42. I want to thank my parents, Jesus and Oprah Winfrey God Oprah b. ? b. 1954 You www.jetbrains.com/idea b. 1976 42
  • 43. Subject: Verb: Infinitive: Participle Phrase: I want to thank my parents God Oprah Winfrey
  • 47. 2+3*4 + * 2 3 4 www.jetbrains.com/idea 47
  • 48. 2+3*4 + * * 2 + 4 3 4 2 3 www.jetbrains.com/idea 48
  • 49. (+ 2 (* 3 4)) + * * 2 + 4 3 4 2 3 www.jetbrains.com/idea 49
  • 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); } } 51 www.jetbrains.com/idea
  • 53. How it Works Searches AST for Bug Patterns www.jetbrains.com/idea 53
  • 54. How it Works @Override public 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
  • 55. How it Works @Override public void visitIfStatement(GrIfStatement stmt) { super.visitIfStatement(stmt); int branches = calculateNumBranches(stmt); if (branches <= getLimit()) { return; } registerStatementError(stmt, stmt); } www.jetbrains.com/idea 55
  • 56. Tree Pattern Matcher (PMD) //FinallyStatement//ReturnStatement //SynchronizedStatement/Block[1][count(*) = 0] //SwitchStatement[not(SwitchLabel[@Default='true'])] www.jetbrains.com/idea 56
  • 57. Structural Search and Replace www.jetbrains.com/idea 57
  • 58. Write Your Own IntelliJ IDEA Static Analysis: Custom Rules with Structural Search & Replace On http://JetBrains.tv www.jetbrains.com/idea 58
  • 59. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 59
  • 67. Software Lifecycle IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 … run in real-time www.jetbrains.com/idea 67
  • 68. Software Lifecycle IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 … run with build www.jetbrains.com/idea 68
  • 69. Not Covered @Immutable, @GuardedBy @Pattern & @Language @Nls, @NonNls, @PropertyKey Duplicate Detection & Dataflow Analysis Dependency Analysis & Dependency Structure Matrix That was last year: http://www.slideshare.net/HamletDRC/static-analysis-in-idea www.jetbrains.com/idea 69
  • 70. What it is IDEA Inspections FindBugs PMD AndroidLint CodeNarc Groovy 2.0 How it works AST Transformations Rewriting Code XPath Expressions What is possible Lombok Groovy www.jetbrains.com/idea 70
  • 71. Learn More – Q & A My JetBrains.tv Screencasts: http://tv.jetbrains.net/tags/hamlet My IDEA blog: http://hamletdarcy.blogspot.com/search/label/IDEA Work's IDEA blog: http://www.canoo.com/blog/tag/idea/ Main blog: http://hamletdarcy.blogspot.com YouTube channel: http://www.youtube.com/user/HamletDRC Twitter: http://twitter.com/hamletdrc IDEA RefCard from DZone: http://goo.gl/Fg4Af IDEA Keyboard Stickers: See me Share-a-Canooie – http://people.canoo.com/share/ Hackergarten – http://www.hackergarten.net/ www.jetbrains.com/idea 71

Editor's Notes

  1. 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
  2. - Command line &amp; 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
  3. - Mention WebStorm for other inspections