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.
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Apex Code Analysis Using the Tooling API and Canvas
1. Apex Code Analysis using the
Tooling API and Canvas
Andrew Fawcett, FinancialForce.com, CTO
@andyinthecloud
2. All about FinancialForce.com
Revolutionizing 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!
3. Introduction
Why 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
4. Canvas: UI Integration with Salesforce
Provides 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
8. Canvas Architecture
Access Method: Signed Request (Recommended)
Canvas aware Web Site
(hold consumer secret)
https://mysite.com/mypage
Salesforce User Interface
(holds consumer secret)
Initial HTTP POST passing signed_request
Canvas Frame
User Web Page Interactions
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)
9. 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!
10. 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
11. 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.
12. Tooling API Architecture and Objects
New 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
13. Tooling API Architecture and Objects
New Objects in the Salesforce Database
▪ Key Objects are ….
Use without MetadataContainer
Use with MetadataContainer
•
•
•
•
•
•
•
•
ApexClass
ApexPage
ApexComponent
ApexTrigger
ApexClassMember
ApexPageMember
ApexComponentMember
ApexTriggerMember
14. 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!
15. Birds Eye View : Symbol Table Object
Only available after an Apex
compilation!
16. Apex UML Canvas Application: Demo
NOTE: Code shown is from my “Building Strong Foundation: Apex Enterprise Patterns” session
17. 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
18. 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
19. 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";
}
}
20. 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 page
ToolingAPIConnection 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)
31. 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
32. Other Uses of Tooling API
Ant Integration : Execute Apex Code from Ant!
Salesforce SE Execute an Apex class using Ant build script
33. Summary
Read Documentation closely!
▪ Force.com Tooling API Developer’s Guide
▪ Force.com Canvas Developer’s Guide
Symbol Table
▪ Has some gaps, still maturing
Spring MVC rocks!
▪ Great for those not familiar with Java Servlet API
▪ Shades of JavaScript Remoting