apex code analysis using the tooling api and canvas

35
Apex Code Analysis using the Tooling API and Canvas Andrew Fawcett, FinancialForce.com, CTO @andyinthecloud

Upload: salesforce-developers

Post on 10-May-2015

1.261 views

Category:

Technology


0 download

DESCRIPTION

The Tooling API provides powerful new ways to manage your code. It can help you measure code health, find circular dependencies, and identify code that's no longer in use. Join us to learn how to use the Tooling API to conduct a quality analysis of your code, and how to do this using an app integrated via Force.com Canvas -- no command line or desktop install required! Take your Apex knowledge to the next level.

TRANSCRIPT

Page 1: Apex Code Analysis Using the Tooling API and Canvas

Apex Code Analysis using the Tooling API and Canvas

Andrew Fawcett, FinancialForce.com, CTO@andyinthecloud

Page 2: Apex Code Analysis Using the Tooling API and Canvas

All about FinancialForce.comRevolutionizing the Back Office#1 Accounting, Billing and PSA Apps on the Salesforce platform

▪ Native apps

▪ San Francisco HQ, 595 Market St

▪ R&D in San Francisco, Harrogate UK, and Granada ES

▪ We are hiring! Meet us at Rehab!

Page 3: Apex Code Analysis Using the Tooling API and Canvas

IntroductionWhy do I need to know more about my Apex code?

▪ Its hard to see code complexity from within the trenches• Helps those unfamiliar learn complex code bases

▪ Tightly coupled code is harder to maintain and evolve

Goals of this Session• What are the technologies that can help?• Understand how to analyze your code with the Tooling API?• Provide a take away demo of the Tooling API you can extend

Page 4: Apex Code Analysis Using the Tooling API and Canvas

Canvas: UI Integration with SalesforceProvides a means of extending the Salesforce User Interface

▪ Chatter Tab▪ Visualforce Tabs▪ Publisher Actions▪ Other areas see Canvas Developer Guide

Open to any new or existing external web page▪ Pages can be developed and hosted within any web platform▪ Developer SDK’s for Java and JavaScript are provided▪ Pages must follow a specific authentication flow

Page 5: Apex Code Analysis Using the Tooling API and Canvas

Where do Canvas apps appear in Salesforce?Chatter Tab

Page 6: Apex Code Analysis Using the Tooling API and Canvas

Where do Canvas apps appear in Salesforce?Visualforce Page

Page 7: Apex Code Analysis Using the Tooling API and Canvas

Where do Canvas apps appear in Salesforce?Publisher Action

Page 8: Apex Code Analysis Using the Tooling API and Canvas

Canvas ArchitectureAccess Method: Signed Request (Recommended)

Salesforce User Interface(holds consumer secret)

Canvas Frame User Web Page Interactions

Initial HTTP POST passing signed_request

Canvas aware Web Site(hold consumer secret)

https://mysite.com/mypage

1. Receives HTTP POST, decodes and validates request

2. Stores CanvasRequest and presents page to user

3. Optionally calls back to Salesforce via API’s using oAuth token

Salesforce API Interactions (using oAuth Token from CanvasRequest)

Page 9: Apex Code Analysis Using the Tooling API and Canvas

How do I make my web page Canvas aware?

Two Salesforce SDK’s▪ Salesforce Canvas JavaScript SDK▪ Salesforce Canvas Java SDK

• Includes JavaScript SDK

Handle Decoding and Parsing of the “CanvasRequest”▪ Sent to the page when the user clicks in the Salesforce UI▪ HTTP POST to the page via ‘signed_request’ parameter

• Contains amongst other information, oAuth token for Salesforce API access!

Page 10: Apex Code Analysis Using the Tooling API and Canvas

What is the Tooling API?

What: Manage Apex Code and Pages on the Platform▪ More granular API than Metadata API built for …

• Building alternative IDE’s (Integrated Developer Environment)– MavensMate – Force.com IDE

• Build development tools– Tools that perform further analysis on code via Symbol Table

Page 11: Apex Code Analysis Using the Tooling API and Canvas

What is the Tooling API?▪ Use REST API bindings if you’re using a language that isn’t strongly typed, like JavaScript.▪ Use SOAP API bindings if you’re using a strongly typed language like Java that generates Web service client code.

Page 12: Apex Code Analysis Using the Tooling API and Canvas

Tooling API Architecture and ObjectsNew Objects in the Salesforce Database

▪ Creating, querying, updating and deleting records• NOTE: Only via Tooling API CRUD operations

▪ MetadataContainer Object• Think, “Workspace” in your IDE for files being worked on

Page 13: Apex Code Analysis Using the Tooling API and Canvas

Tooling API Architecture and ObjectsNew Objects in the Salesforce Database

▪ Key Objects are ….

Use without MetadataContainer Use with MetadataContainer

• ApexClass• ApexPage• ApexComponent• ApexTrigger

• ApexClassMember• ApexPageMember• ApexComponentMember• ApexTriggerMember

Page 14: Apex Code Analysis Using the Tooling API and Canvas

What is a Symbol Table?

Child of ApexClass, ApexClassMember and ApexTriggerMember▪ Variables▪ Methods▪ Inner Classes▪ External References

• Lists references to the above from other Apex Classes and Apex Triggers• NOTE: Only available after a compile!

Page 15: Apex Code Analysis Using the Tooling API and Canvas

Birds Eye View : Symbol Table Object

Only available after an Apex compilation!

Page 16: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas Application: Demo

NOTE: Code shown is from my “Building Strong Foundation: Apex Enterprise Patterns” session

Page 17: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas Application: Architecture

▪ Hosted on Heroku▪ Jetty Web Server

• Java Spring MVC Framework• SOAP Tooling API (via JAX-WS)

– via wsimport Maven plugin

▪ Web Page• jQuery• UMLCanvas JS Library

▪ Maven Build System

Page 18: Apex Code Analysis Using the Tooling API and Canvas

Configuring a Canvas Application in Salesforce▪ Makes HTTP POST to URL https://localhost:8443/app/canvas

• Note: /app/ is managed by Spring MVC and forwards to Java Controllers…

▪ Setup > Create > Applications

Page 19: Apex Code Analysis Using the Tooling API and Canvas

Integrating the Canvas SDK with Spring MVC

CanvasController.java▪ Handles the HTTP POST made by Salesforce to /canvas▪ Uses Salesforce Canvas SDK to decode and store in HTTP session

@Controller@RequestMapping("/canvas")public class CanvasController {

@RequestMapping(method = RequestMethod.POST)public String canvasRequest(@RequestParam("signed_request") String signedRequest, HttpSession session){ String secret = System.getenv("CANVAS_CONSUMER_SECRET"); CanvasRequest request = SignedRequest.verifyAndDecode(signedRequest, secret);

session.setAttribute("canvasRequest", request);return "redirect:umlcanvas";

}}

Page 20: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code Walkthrough

UmlCanvasController.java▪ Redirection from /canvas to /umlcanvas▪ Page load, rendered by umlcanvas.jsp

@Controller@RequestMapping("/umlcanvas")public class UmlCanvasController {

@RequestMapping(method = RequestMethod.GET)public String load(HttpSession session, Map<String, Object> map) throws Exception{

// List classes on the pageToolingAPIConnection toolingAPI = createToolingAPIConnection(session);ApexClass[] apexClasses =

toolingAPI.service.query( "SELECT Id, Name, SymbolTable " + "FROM ApexClass" , toolingAPI.session).getRecords().toArray(new ApexClass[0]);

for(ApexClass apexClass : apexClasses)

Page 21: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code Walkthroughumlcanvas.jsp

▪ Java Servlet Page (JSP) ▪ AJAX calls to

UmlCanvasController.java▪ JavaScript calls UmlCanvas

JavaScript library to render UML

Page 22: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code WalkthroughUmlCanvasController.java

▪ jQuery Ajax calls controller as user selects classes1. /umlcanvas/{apexclass}/symboltable

Page 23: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code WalkthroughUmlCanvasController.java1. /umlcanvas/{apexclass}/symboltable

2. /umlcanvas/{apexclass}/compile

3. /umlcanvas/containerasyncrequest/{id}

4. /umlcanvas/containerasyncrequest/{id}/{classname}/symboltable

Page 24: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/symboltable

Page 25: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/compile

Page 26: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/compile

Page 27: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/compile

Page 28: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code Walkthrough

UmlCanvasController.java : /{apexclass}/compile

Page 29: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code WalkthroughUmlCanvasController.java : /containerasyncrequest/{id}

Page 30: Apex Code Analysis Using the Tooling API and Canvas

Apex UML Canvas : Code WalkthroughUmlCanvasController.java : /navigator/containerasyncrequest/{id}/{classname}/symboltable

Page 31: Apex Code Analysis Using the Tooling API and Canvas

Tooling API Other Features▪ Debug Logs▪ Execute Anonymous Apex Code▪ Static Resources▪ Inject Execution of Apex or SOQL Code for Debug▪ Checkpoints to capture Heap Dumps▪ Manage Custom Fields▪ Accces Code Coverage Results

Page 32: Apex Code Analysis Using the Tooling API and Canvas

Other Uses of Tooling APIAnt Integration : Execute Apex Code from Ant!

Salesforce SE Execute an Apex class using Ant build script

Page 34: Apex Code Analysis Using the Tooling API and Canvas

Andrew Fawcett

CTO,@andyinthecloud

Page 35: Apex Code Analysis Using the Tooling API and Canvas