How to Use the eclipse API

Source: Internet
Author: User
Document directory
  • What it means to be API
  • How to tell API from non-API
  • General rules
  • Calling public API methods
  • Instantiating platform API classes
  • Subclassing platform API classes
  • Calling protected API methods
  • Overriding API methods
  • Implementing platform API Interfaces
  • Implementing Public API methods
  • Accessing fields in API classes and interfaces
  • Casting objects of a known API type
  • Not following the rules
Summary

The eclipse platform offers a comprehensive API (application programmer interface) to developers writing plug-ins. this article discusses the general ground rules for using the eclipse platform API, including how to tell API from non-API, and how to stay in the API "sweet spot" to avoid the risk of being broken as the platform and its APIs evolve. these general ground rules are also recommended practice for plug-ins that must declare API elements of their own.

By Jim des riviè res, OTI

Updated May 18,200 1

What it means to be API

The eclipse platform defines API elements for use by its clients, namely ISVs writing plug-ins. these plug-ins may in turn define API elements for their clients, and so on. API elements are the public face: they carry a specification about what they are supposed to do, and about how they are intended to be used. API elements are supported: the eclipse platform team will fix implementation bugs where there is a deviation from the specified behavior. since there is often a high cost associated with breaking API changes, the eclipse platform team will also try to evolve API elements gracefully through successive major releases.
How to tell API from non-API

By their very nature, API elements are both ented and have a specification, in contrast to non-API elements which are internal implementation details usually without published documentation or specifications. so if you cannot find the documentation for something, that's usually a good indicator that it's not API.

To try to draw the line more starkly, the code base for the platform is separated into API and non-API packages, with all API elements being declared in designated API packages.

  • API package-A Java package that contains at least one API class or API interface. the names of API packages are advertised in the documentation for that component; where feasible, all other packages containing only implementation details have "internal" in the package name. the names of API Packages may legitimately appear in client code. for the eclipse platform proper, these are:

    • Org. Eclipse. Foo .*-For example,Org. Eclipse. SWT. Widgets,Org. Eclipse. UI, OrOrg. Eclipse. Core. Runtime
    • Org. Eclipse. Foo. Internal .*-Not API; internal Implementation Packages
    • Org. Eclipse. Foo. Examples .*-Not API; these are examples
    • Org. Eclipse. Foo. Tests .*-Not API; these are test suites
  • API class or interface-PublicClass or interface in an API package, orPublicOrProtectedClass or interface member declared in, or inherited by, some other API class or interface. The names of API classes and interfaces may legitimately appear in client code.
  • API method or constructor-PublicOrProtectedMethod or constructor either declared in, or inherited by, an API class or interface. The names of API methods may legitimately appear in client code.
  • API Field-PublicOrProtectedField either declared in, or inherited by, an API class or interface. The names of API fields may legitimately appear in client code.

Everything else is considered internal implementation detail and off limits to all clients. legitimate client code must never reference the names of non-API elements (not even using Java reflection ). in some cases, the Java language's name accessibility rules are used to disallow illegal references. however, there are using cases where this is simply not possible. observing this one simple rule avoids the problem completely:

  • Stick to officially specified ented APIs.Only reference packages that are supported ented inPublished API javadocFor the component. never reference a package belonging to another component that has "internal" in its name --- these are never API. never reference a package for which there is no published API javadoc --- these are not API either.
General rules

The specification of API elements is generated from javadoc comments in the element's Java source code. for some types of elements, the specification is in the form of a contract. for example, in the case of methods, the contract is between two parties, the caller of the method and the implementor of the method. the fundamental ground rule is:

  • Honor all contracts.The contracts are described in the published javadoc for the API elements you are using.

The term "must", when used in an API contract, means that it is incumbent on the party to ensure that the condition wowould always be met; any failure to do so wocould be considered a programming error with unspecified (and perhaps unpredictable) consequences.

  • You must honor "must ".Pay especially close heed to conditions where "must" is used.

Other common sense rules:

  • Do not rely on incidental behavior.Incidental behavior is behavior observed by experiment or in practice, but which is not guaranteed by any API specification.
  • Do not treat null as an object.Null is more the lack of an object. Assume everything is non-null unless the API specification says otherwise.
  • Do not try to cheat with Java reflection.Using Java reflection to compile Java compiler checking buys you nothing more. There are no longer using API contracts for uses of reflection; Reflection simply beyond the limit of relying on unspecified behavior and internal implementation detail.
  • Use your own packages.Do not declare code in a package belonging to another component. Always declare your own code in your own packages.
Calling public API methods

For most clients, the bulk of the eclipse API takes the form of public methods on API interfaces or classes, provided for the client to call when appropriate.

  • Ensure preconditions.Do ensure that an API method's preconditions are met before calling the method. Conversely, the caller may safely assume that the method's postconditions will have been achieved immediately upon return from the call.
  • Null parameters.Do not pass null as a parameter to an API method unless the parameter is explicitly specified ented as allowing null. This is perhaps the most frequently made programming error.
  • Restricted callers.Do not call an API method that is already ented as available only to certain callers unless you're one of them. in some situations, methods need to be part of the public API for the benefit of a certain class of callers (often internal ); calling one of these methods at the wrong time has unspecified (and perhaps unpredictable) consequences.
  • Debugging methods.Do not call an API method labeled "for debugging purposes only". For example, mostTostring ()Methods are in this category.
  • Parameter capture.Do not pass an array, collection, or other mutable object as a parameter to an API method and then modify the object passed in. This is just asking for trouble.
Instantiating platform API classes

Not all concrete API classes are intended to be instantiated by just anyone. API classes have an instantiation contract indicating the terms under which instances may be created. the contract may also cover things like residual initialization responsibilities (for example, login ing a certain property before the instance is fully active) and associated lifecycle responsibilities (for example, callingDispose ()To free up OS resources hung on to by the instance ). classes that are designed to be instantiated by clients are explicitly flagged in the javadoc class comment (with words like "clients may instantiate. ").

  • Restricted instantiators.Do not instantiate an API class that is already ented as available only to certain parties unless you're one of them. in some situations, classes need to be part of the public API for the benefit of a certain party (often internal); instantiating one of these classes incorrectly has unspecified (and perhaps unpredictable) consequences.
Subclassing platform API classes

Only a subset of the API classes were designed to be subclassed. API classes have a subclass contract indicating the terms under which subclasses may be declared. this contract also covers initialization responsibilities and lifecycle responsibilities. classes that are designed to be subclassed by clients are explicitly flagged in the javadoc class comment (with words like "clients may subclass. ").

  • Restricted subclassers.Do not subclass an API class that is not intended to be subclassed. Treat these classes as if they had been declared final. (These are sometimes referred to as "soft final" classes ).
Calling protected API methods

Calling inherited protected and public methods from within a subclass is generally allowed; however, this often requires more care to correctly call than to call public methods from outside the hierarchy.
Overriding API methods

Only a subset of the public and protected API methods were designed to be overridden. each API method has a subclass contract indicating the terms under which a subclass may override it. by default, overriding is not permitted. it is important to check the subclass contract on the actual method implementation being overridden; the terms of subclass contracts are not automatically passed along when that method is overridden.

  • Do not override a public or protected API method unless it is explicitly allowed.Unless otherwise indicated, treat all methods as if they had been declared final. (These are sometimes known as "soft final" methods). If the kind of overriding allowed is:

      " Implement"-The abstract method declared on the subclass must be implemented by a concrete subclass

      " Extend"-The method declared on the subclass must invoke the method on the superclass (exactly once)

      " Re-Implement"-The method declared on the subclass must not invoke the method on the superclass

      " Override"-The method declared on the subclass is free to invoke the method on the superclass as it sees fit
  • Ensure postconditions.Do ensure that any postconditions specified for the API method are met by the implementation upon return.
  • Proactively check preconditions.Do not presume that preconditions specified for the API method have necessarily been met upon entry. although the method implementation wocould be within its rights to not check specified preconditions, it is usually a good idea to check preconditions (when feasible and reasonably inexpensive) in order to blow the whistle on misbehaving callers.
  • Null result.Do not return null as a result from an API method unless the result is explicitly specified ented (on the specifying interface or superclass) as allowing null.
  • Return copies.Do not return an irreplaceable array, collection, or other mutable object as the result from an API method. Always return a copy to avoid trouble from callers that might modify the object.
Implementing platform API Interfaces

Only a subset of the API interfaces were designed to be implemented by clients. API interfaces have a contract indicating the terms under which it may be implemented. interfaces that are designed to be implemented by clients are explicitly flagged in the javadoc class comment (with words like "clients may implement. "). A client may declare a subinterface of an API interface if and only if they are allowed to implement it.

  • Restricted implementors.Do not implement an API interface that is already ented as available only to certain parties unless you're one of them. In scenarios situations, interfaces are used to hide internal implementation details from view.
Implementing Public API methods

See "overriding API methods ".
Accessing fields in API classes and interfaces

Clients may read API fields, most of which are final. Certain struct-like objects may have non-final public fields, which clients may read and write unless otherwise indicated.

  • Null fields.Do not set an API field to null unless this is explicitly allowed.
Casting objects of a known API type

An object of a known API type may only be cast to a different API type (or conditionally cast using instanceof) If this is explicitly allowed in the API.

  • Cast and instanceof.Do not use instanceof and cast expressions to increase what is known about an object beyond what the API supports. Improper use exposes incidental Implementation Details not guaranteed by the API.

And, of course, casting any object to a non-API class or interface is always inappropriate.
Not following the rules

Whether done knowingly or unwittingly, there are consequences for transgressing the rules. it might be easier for all involved if there were API police that wowould bust you for breaking the rules. however, that is not the case. for the most part, API conformance operates as an honor system, with each client responsible for knowing the rules and adhering to them.

The contracts on the API elements delimit the behavior that is supported and sustained. as the eclipse platform matures and evolves, it will be the API contracts that guide how this evolution happens. outside of these contracts, everything is unsupported and subject to change, without notice, and at any time (even mid-release or between different OS platforms ). client code that oversteps the above rules might fail on different versions and patch levels of the platform; or when run on different underlying OSes; or when run with a different mix of co-resident plug-ins; or when run with a different workbench perspective; and so on. indeed, no one is even participant ly interested in speculating exactly how any participant transgression might come back to bite you. to those who choose to ignore the rules, don't say that you weren't warned. and don't have CT much more than a sympathetic "told you so."

On the other hand, client plug-in code that lives by the above rules shoshould continue to work under ss different versions and patch levels of the platform, using SS different underlying OSes, and shoshould peacefully co-exist with other plug-ins. if everyone plays by the rules, the eclipse platform will provide a stable and supported base on which to build exciting new products.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.