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?
Apex gives us many useful annotations (e.g. @AuraEnabled, @Future, @IsTest, @TestVisible, etc.)
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)
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
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
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:
@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.