2

Apex gives us many useful annotations (e.g. @AuraEnabled, @Future, @IsTest, @TestVisible, etc.)

  • Can we detect whether/how a class has been annotated?
  • Is it possible for us to create our annotations?
  • What might be some good use cases?
  • 4
    Providing languages features that allow custom annotations to be created would be a big job - see e.g. Package java.lang.annotation - and as we know innovation in Apex lags far behind innovation in Java. So we are stuck with just the ones Salesforce have implemented. However, the new compiler appears more open to extension so there may be innovation in the future. – Keith C May 10 '18 at 8:59
4

Well anotations are quite useful in languages like JAVA to design your own language paradigm, in APEX they are more like representation of what do they do and how they behave

1. Can we detect whether/how a class has been annotated? Yes:)

You can get class body by this SOQL

SELECT Body FROM ApexClass where Name='MyClass'

Once you get class body you can fire a regex to see if it contains any of the below string annotations

@AuraEnabled
@Deprecated
@Future
@InvocableMethod
@InvocableVariable
@IsTest
@ReadOnly
@RemoteAction
@SuppressWarnings
@TestSetup
@TestVisible
@RestResource(urlMapping='/yourUrl')
@HttpDelete
@HttpGet
@HttpPatch
@HttpPost
@HttpPut

Or as Suggested by @sfdcfox you can use SOSL like

FIND {@RestResource} in ALL FIELDS RETURNING ApexClass(Name)
  1. Is it possible for us to create our annotations? Unfortunately at this stage its not.

3. What might be some good use cases? There is no decent Apex XML parser available, having the ability to generate auto parsed XML instance. probaly a custom anotation would do like it helps in java. Apart from that there are so many ideas present on ideaexchange wich tells how beautifully you can use new or custom annotations. Do check them out.

https://success.salesforce.com/search?type=Ideas&keywords=annotations

  • Cheers for the response. Building an Apex parser sound like an interesting exercise :-) ... not entirely sure how practical it is to use actually within the context of an Apex application to learn whether the executing code is annotated. – Brian Kessler May 10 '18 at 9:00
  • 1
    You might want to look into this as a class can have multiple annotations and that wont tell how the method is executed or called from. salesforce.stackexchange.com/questions/131140/… – Pranay Jaiswal May 10 '18 at 9:03
  • 2
    You can also SOSL to find classes that have particular annotations if you like. – sfdcfox May 11 '18 at 3:02
0

Salesforce supports only following annotations and it doesn't allow to create custom annotations.

@AuraEnabled
@Deprecated
@Future
@InvocableMethod
@InvocableVariable
@IsTest
@ReadOnly
@RemoteAction
@SuppressWarnings
@TestSetup
@TestVisible
Apex REST annotations:
@RestResource(urlMapping='/yourUrl')
@HttpDelete
@HttpGet
@HttpPatch
@HttpPost
@HttpPut
  • Cheers for the response, but I think I may have been misunderstood. I'm most interested in whether we can do MORE than just consume native annotations as originally intended. – Brian Kessler May 10 '18 at 8:31
0

Well, you asked several questions. I have answers for some of them.

Using @isTest annotation you can create test class and save it as public in stead of standard private, define general methods for creating test data, and then use this class as Test Data Creator. Several advantages of this method:

  • @isTest annotated class is not counted against limit of used code.
  • You reuse methods for writing more robust test classes.

@testSetup annotation is also one pretty annotation for optimizing Apex testing. This is not annotation for class, but for method in test class, and only one method can be assigned such test setup method.

  • Cheers for the response, but I think I may have been misunderstood. I'm most interested in whether we can do MORE than just consume native annotations as originally intended. – Brian Kessler May 10 '18 at 8:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.