|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.openide.src; import java.util.*; import java.lang.reflect.Modifier; import org.openide.filesystems.FileObject; import org.openide.util.NbBundle; /** Element that describes one class. * Note that this is a member element--in fact, it may be either a * top-level class (held in a source element), or a named inner class * (held in another class element).
true if this class could be a JavaBean,
* otherwise false
*/
public boolean isDeclaredAsJavaBean() {
if (Modifier.isPublic(getModifiers()) && !Modifier.isAbstract(getModifiers())) {
if (isClass()) {
ConstructorElement[] constructors = getConstructors();
for (int i = 0; i < constructors.length; i++) {
if (constructors[i].getParameters().length == 0)
if (Modifier.isPublic(constructors[i].getModifiers())) {
return true;
}
}
return (constructors.length == 0);
}
}
return false;
}
// [PENDING] concrete? public? what about extending [J]Applet indirectly? --jglick
/** Test whether this class is an applet.
* It must:
*
true if this class could be an applet,
* otherwise false .
*/
public boolean isDeclaredAsApplet() {
if (isClass()) {
Identifier superclass = getSuperclass();
if (superclass != null) {
String name = superclass.getFullName();
return name.equals("java.applet.Applet") || name.equals("javax.swing.JApplet"); // NOI18N
}
}
return false;
}
// ================ finders =======================================
/** List of finders */
private static List finders = new LinkedList();
/** Register a new finder for locating class elements.
* @param f the finder to add
*/
public static void register (Finder f) {
synchronized (finders) {
finders.add (f);
}
}
/** Unregister a finder for locating class elements.
* @param f the finder to remove
*/
public static void unregister (Finder f) {
synchronized (finders) {
finders.remove (f);
}
}
/**
* Search for a class element throughout the system.
* Asks all registered finders if they know where to find such a class.
* @param name class name separated by dots, e.g. java.lang.String .
* For inner classes is accepted delimiting by '$' or by '.'
* e.g. inner class A.B in package org.netbeans.test could
* be specified like: org.netbeans.A.B or org.netbeans.A$B
* Both possibilities are accepted.
* @param reference a file that is thought to belong to the same class path structure
* as the desired class, used as a reference point
* @return class element for that name, or null if none exists
* @see ClassElement.Finder
*/
public static ClassElement forName(String name, FileObject reference) {
// 0. basic step is to try one of the registered finders
// and give them chance to search for a name of the element
Iterator it;
synchronized (finders) {
it = new ArrayList(finders).iterator();
}
while (it.hasNext ()) {
ClassElement el = ((Finder)it.next()).find(name, reference);
if (el != null)
return el;
}
return null;
}
/** Search for a class element throughout the system.
* First goes through repository and find the appropriate DataObject
* Tests if it has SourceCookie and if there exist the right one,
* it is returned. Otherwise tries to creates the Class.forName
* and asks all registered finders if they know where to find such a class.
*
* @param name class name separated by dots, e.g. java.lang.String .
* For inner classes is accepted delimiting by '$' or by '.'
* e.g. inner class A.B in package org.netbeans.test could
* be specified like: org.netbeans.A.B or org.netbeans.A$B
* Both possibilities are accepted.
* @return class element for that name, or null if none exists
* @see ClassElement.Finder
* @deprecated This method variant can no longer work. Use {@link #forName(String,FileObject)} instead.
*/
public static ClassElement forName(String name) {
throw new UnsupportedOperationException("The operation is not more supppoerted, use ClassElement.forName(String,FileObject)");
}
/**
* Search for a class element throughout the system.
* Asks all registered finders if they know where to find such a class.
* @param clazz class name separated by dots, e.g. java.lang.String
* @param reference a file that is thought to belong to the same class path structure
* as the desired class, used as a reference point
* @return class element for that name, or null if none exists
* @see ClassElement.Finder
*/
public static ClassElement forClass(Class clazz, FileObject reference) {
Iterator it;
synchronized (finders) {
it = new ArrayList(finders).iterator();
}
while (it.hasNext ()) {
ClassElement el = ((Finder)it.next()).find(clazz, reference);
if (el != null)
return el;
}
return null;
}
/** Search for a class element throughout the system.
* Asks all registered finders if they know where to find such a class.
* @param clazz class name separated by dots, e.g. java.lang.String
* @return class element for that name, or null if none exists
* @see ClassElement.Finder
* @deprecated This method variant can no longer work. Use {@link #forClass(Class,FileObject)} instead.
*/
public static ClassElement forClass(Class clazz) {
return forClass (clazz, null);
}
/** Provides a "finder" for class elements.
* A module can provide its own finder to enhance the ability
* of the IDE to locate a valid class element description for different classes.
* @see ClassElement#forName
* @see ClassElement#register
* @see ClassElement#unregister
*/
public static interface Finder {
/** Find a class element description for a class.
*
* @param clazz the class to find
* @param reference file object presumed to be in the same classpath structure
* @return the class element, or null if not found
*/
public ClassElement find(Class clazz, FileObject reference);
/** Find a class element description for a class name.
*
* @param name the name of a class to find
* @param reference file object presumed to be in the same classpath structure
* @return the class element, or null if not found
*/
public ClassElement find(String name, FileObject reference);
}
// ================ implementation ===================================
/** Pluggable behavior for class elements.
* @see ClassElement
*/
public static interface Impl extends MemberElement.Impl {
/** Add some items. */
public static final int ADD = 1;
/** Remove some items. */
public static final int REMOVE = -1;
/** Set some items, replacing the old ones. */
public static final int SET = 0;
/** @deprecated Only public by accident. */
/* public static final */ long serialVersionUID = 2564194659099459416L;
/** Set the superclass for this class.
* @param superClass the superclass, by name
* @throws SourceException if impossible
*/
public void setSuperclass(Identifier superClass) throws SourceException;
/** Get the superclass for this class.
* @return the superclass, by name
*/
public Identifier getSuperclass();
/** Set whether this is a class or interface.
* @param isClass either {@link ClassElement#CLASS} or {@link ClassElement#INTERFACE}
* @throws SourceException if impossible
*/
public void setClassOrInterface(boolean isClass) throws SourceException;
/** Test whether this is a class or interface.
* @return either {@link ClassElement#CLASS} or {@link ClassElement#INTERFACE}
*/
public boolean isClassOrInterface();
/** Change the set of initializers.
* @param elems the new initializers
* @param action {@link #ADD}, {@link #REMOVE}, or {@link #SET}
* @exception SourceException if impossible
*/
public void changeInitializers (InitializerElement[] elems, int action) throws SourceException;
/** Get all initializers.
* @return the initializers
*/
public InitializerElement[] getInitializers ();
/** Change the set of fields.
* @param elems the new fields
* @param action {@link #ADD}, {@link #REMOVE}, or {@link #SET}
* @exception SourceException if impossible
*/
public void changeFields (FieldElement[] elems, int action) throws SourceException;
/** Get all fields.
* @return the fields
*/
public FieldElement[] getFields ();
/** Find a field by name.
* @param name the name to look for
* @return the field, or null if it does not exist
*/
public FieldElement getField (Identifier name);
/** Change the set of methods.
* @param elems the new methods
* @param action {@link #ADD}, {@link #REMOVE}, or {@link #SET}
* @exception SourceException if impossible
*/
public void changeMethods (MethodElement[] elems, int action) throws SourceException;
/** Get all methods.
* @return the methods
*/
public MethodElement[] getMethods ();
/** Finds a method by signature.
* @param name the name to look for
* @param arguments the argument types to look for
* @return the method, or null if it does not exist
*/
public MethodElement getMethod (Identifier name, Type[] arguments);
/** Change the set of constructors.
* @param elems the new constructors
* @param action {@link #ADD}, {@link #REMOVE}, or {@link #SET}
* @exception SourceException if impossible
*/
public void changeConstructors (ConstructorElement[] elems, int action) throws SourceException;
/** Get all constructors.
* @return the constructors
*/
public ConstructorElement[] getConstructors ();
/** Find a constructor by signature.
* @param arguments the argument types to look for
* @return the constructor, or null if it does not exist
*/
public ConstructorElement getConstructor (Type[] arguments);
/** Change the set of inner classes.
* @param elems the new inner classes
* @param action {@link #ADD}, {@link #REMOVE}, or {@link #SET}
* @exception SourceException if impossible
*/
public void changeClasses (ClassElement[] elems, int action) throws SourceException;
/** Get all inner classes.
* @return the inner classes
*/
public ClassElement[] getClasses ();
/** Find an inner class by name.
* @param name the name to look for
* @return the inner class, or null if it does not exist
*/
public ClassElement getClass (Identifier name);
/** Change the set of implemented/extended interfaces.
* @param ids the new interfaces, by name
* @param action {@link #ADD}, {@link #REMOVE}, or {@link #SET}
* @exception SourceException if impossible
*/
public void changeInterfaces (Identifier[] ids, int action) throws SourceException;
/** Get all implemented/extended interfaces.
* @return the interfaces, by name
*/
public Identifier[] getInterfaces ();
/** Get the class's documentation block.
* @return JavaDoc for the class (not its members)
*/
public JavaDoc.Class getJavaDoc();
}
/** Memory based implementation of the element factory.
*/
static final class Memory extends MemberElement.Memory implements Impl {
/** property, need not be initialized */
private Identifier superClass;
/** is class or interface */
private boolean isClass;
/** collection of interfaces */
private MemoryCollection interfaces;
/** collection of initializers */
private MemoryCollection.Initializer initializers;
/** collection of constructors */
private MemoryCollection.Constructor constructors;
/** collection of methods */
private MemoryCollection.Method methods;
/** collection of fields */
private MemoryCollection.Field fields;
/** collection of classes */
private MemoryCollection.Class classes;
/** memory implementation of java doc */
JavaDoc.Class javaDoc = null;
static final long serialVersionUID =6058485742932189851L;
public Memory() {
superClass = null;
isClass = true;
javaDoc = JavaDocSupport.createClassJavaDoc( null );
}
/** Copy constructor.
* @param el element to copy from
*/
public Memory(ClassElement el) {
super (el);
superClass = el.getSuperclass ();
isClass = el.isClassOrInterface ();
javaDoc = el.getJavaDoc().isEmpty() ?
JavaDocSupport.createClassJavaDoc( null ) :
JavaDocSupport.createClassJavaDoc( el.getJavaDoc().getRawText() );
}
void updateNameToParent() {
// set the proper name first, according to the outer element's name:
MemberElement me = (MemberElement)this.element;
if (me.getDeclaringClass() == null)
// we have just the name the caller has given to us.
return;
StringBuffer fullName = new StringBuffer();
String simpleName;
fullName.append(me.getDeclaringClass().getName().getFullName());
fullName.append('.');
fullName.append(simpleName = getName().getName());
this.setName(Identifier.create(fullName.toString(), simpleName));
}
void initialize(ClassElement model) {
updateNameToParent();
copyFrom(model);
}
/** Late initialization of initialization of copy elements.
*/
public void copyFrom (ClassElement copyFrom) {
changeInterfaces (copyFrom.getInterfaces (), SET);
changeConstructors (copyFrom.getConstructors (), SET);
changeMethods (copyFrom.getMethods (), SET);
changeFields (copyFrom.getFields (), SET);
changeClasses (copyFrom.getClasses (), SET);
}
/** Setter
*/
public void setSuperclass(Identifier superClass) throws SourceException {
Identifier old = this.superClass;
this.superClass = superClass;
firePropertyChange (PROP_SUPERCLASS, old, superClass);
}
public Identifier getSuperclass() {
return superClass;
}
public void setClassOrInterface(boolean isClass) {
boolean old = this.isClass;
this.isClass = isClass;
firePropertyChange (PROP_CLASS_OR_INTERFACE,
old ? Boolean.TRUE : Boolean.FALSE,
isClass ? Boolean.TRUE : Boolean.FALSE);
}
public boolean isClassOrInterface() {
return isClass;
}
/** Changes set of elements.
* @param elems elements to change
* @param action the action to do (ADD, REMOVE, SET)
* @exception SourceException if the action cannot be handled
*/
public synchronized void changeInitializers (InitializerElement[] elems, int action) {
initInitializers();
initializers.change (elems, action);
}
public synchronized InitializerElement[] getInitializers () {
initInitializers();
return (InitializerElement[])initializers.toArray ();
}
void initInitializers() {
if (initializers == null) {
initializers = new MemoryCollection.Initializer (this);
}
}
/** Changes set of elements.
* @param elems elements to change
* @exception SourceException if the action cannot be handled
*/
public synchronized void changeFields (FieldElement[] elems, int action) {
initFields();
fields.change (elems, action);
}
public synchronized FieldElement[] getFields () {
initFields();
return (FieldElement[])fields.toArray ();
}
/** Finds a field with given name.
* @param name the name of field to look for
* @return the element or null if field with such name does not exist
*/
public synchronized FieldElement getField (Identifier name) {
initFields();
return (FieldElement)fields.find (name, null);
}
void initFields() {
if (fields == null) {
fields = new MemoryCollection.Field (this);
}
}
/** Changes set of elements.
* @param elems elements to change
*/
public synchronized void changeMethods (MethodElement[] elems, int action) {
initMethods();
methods.change (elems, action);
}
public MethodElement[] getMethods () {
initMethods();
return (MethodElement[])methods.toArray ();
}
/** Finds a method with given name and argument types.
* @param name the name of field to look for
* @param arguments for the method
* @return the element or null if such method does not exist
*/
public synchronized MethodElement getMethod (Identifier name, Type[] arguments) {
initMethods();
return (MethodElement)methods.find (name, arguments);
}
void initMethods() {
if (methods == null) {
methods = new MemoryCollection.Method (this);
}
}
/** Changes set of elements.
* @param elems elements to change
* @exception SourceException if the action cannot be handled
*/
public synchronized void changeConstructors (ConstructorElement[] elems, int action) {
initConstructors();
constructors.change (elems, action);
}
public synchronized ConstructorElement[] getConstructors () {
initConstructors();
return (ConstructorElement[])constructors.toArray ();
}
/** Finds a constructor with argument types.
* @param arguments for the method
* @return the element or null if such method does not exist
*/
public synchronized ConstructorElement getConstructor (Type[] arguments) {
initConstructors();
return (ConstructorElement)constructors.find (null, arguments);
}
void initConstructors() {
if (constructors == null) {
constructors = new MemoryCollection.Constructor (this);
}
}
/** Changes set of elements.
* @param elems elements to change
*/
public synchronized void changeClasses (ClassElement[] elems, int action) {
initClasses();
classes.change (elems, action);
}
public synchronized ClassElement[] getClasses () {
initClasses();
return (ClassElement[])classes.toArray ();
}
/** Finds an inner class with given name.
* @param name the name to look for
* @return the element or null if such class does not exist
*/
public synchronized ClassElement getClass (Identifier name) {
initClasses();
return (ClassElement)classes.find (name, null);
}
void initClasses() {
if (classes == null) {
classes = new MemoryCollection.Class (this);
}
}
/** Changes interfaces this class implements (or extends).
* @param ids identifiers to change
*/
public synchronized void changeInterfaces (Identifier[] ids, int action) {
initInterfaces();
interfaces.change (ids, action);
}
/** @return all interfaces which this class implements or interface extends.
*/
public synchronized Identifier[] getInterfaces () {
initInterfaces();
return (Identifier[])interfaces.toArray ();
}
void initInterfaces() {
if (interfaces == null) {
interfaces = new MemoryCollection.Interface(this);
}
}
void markCurrent(Element marker, boolean after) {
MemoryCollection col;
if (marker instanceof InitializerElement) {
col = initializers;
} else if (marker instanceof ClassElement) {
col = classes;
} else if (marker instanceof FieldElement) {
col = fields;
} else if (marker instanceof MethodElement) {
col = methods;
} else if (marker instanceof ConstructorElement) {
col = constructors;
} else {
throw new IllegalArgumentException();
}
col.markCurrent(marker, after);
}
// ================ javadoc =========================================
/** @return class documentation.
*/
public JavaDoc.Class getJavaDoc() {
return javaDoc;
}
/** Getter for the associated class
* @return the class element for this impl
*/
final ClassElement getClassElement () {
return (ClassElement)element;
}
// ================ serialization ======================================
public Object readResolve() {
return new ClassElement(this, (SourceElement)null);
}
}
}
|
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.