alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

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-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.javadoc.search;

import java.net.URL;
import java.util.StringTokenizer;
import java.util.ArrayList;
import org.netbeans.api.java.classpath.GlobalPathRegistry;

import org.openide.filesystems.Repository;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataObject;
import org.openide.cookies.SourceCookie;
import org.openide.cookies.OpenCookie;
import org.openide.src.*;


/** This class finds the source to show it instead of documentation.
 *
 * @author  Administrator
 * @version 
 */
public final class SrcFinder extends Object {

    private static final ElementFormat FORMATOR = new ElementFormat( "{C}" ); // NOI18N

    /** SrcFinder is a singleton */
    private  SrcFinder() {
    }

    static Element findSource( String aPackage, URL url  ) {

        aPackage = aPackage.replace( '.', '/' ); // NOI18N
        String thePackage = null;
        String member = url.getRef(); 
        String clazz = url.getFile();
        String filename = null;
        Type[] params;

        int pIndex;
        
        if ( ( pIndex = clazz.toLowerCase().indexOf( aPackage.trim().toLowerCase() ) ) != -1 ) {
            thePackage = clazz.substring(pIndex, pIndex + aPackage.trim().length()  - 1 );
            clazz = clazz.substring( pIndex + aPackage.trim().length(), clazz.length() - 5 );

            int ei;
            if ( ( ei = clazz.indexOf('.')) != -1 ) {
                filename = clazz.substring(0, ei );
            }
            else
                filename = clazz;

        }
        
//        System.out.println("================================");
//        System.out.println("URL     :" + url   );
//        System.out.println("aPCKG   :" + aPackage );
//        System.out.println("--------------------------------");
//        System.out.println("MEMBER  :" + member ); // NOI18N
//        System.out.println("CLASS   :" + clazz ); // NOI18N
//        System.out.println("PACKAGE :" + thePackage ); // NOI18N
//        System.out.println("FILENAME:" + filename ); // NOI18N

        String resourceName = thePackage + "/" + filename + ".java"; // NOI18N       
        FileObject fo = GlobalPathRegistry.getDefault().findResource( resourceName );
        
        if ( fo != null ) {
            try {        
                DataObject dobj = DataObject.find( fo );

                SourceCookie sc = (SourceCookie)dobj.getCookie( SourceCookie.class );
                ClassElement classes[] = sc.getSource().getAllClasses();

                // Search all classes
                for ( int i = 0; i < classes.length; i++ ) {
                    String outerName = FORMATOR.format( classes[i] );
                    if ( clazz.equals( outerName ) ) {
                        if ( member == null )
                            // We are looking for class
                            return classes[i];
                        else {

                            int pi = member.indexOf( '(' );
                            if ( pi == -1 ) {
                                // we are looking for fiels
                                FieldElement fe = classes[i].getField( Identifier.create( member ) );
                                return fe;
                            }
                            else {
                                // We are looking for method or constructor
                                return getMethod( classes[i], member );
                            }

                        }
                    }
                }


                return null;
            }
            catch ( org.openide.loaders.DataObjectNotFoundException e ) {
                System.out.println( e );
            }
        }
        return null;
    }

    /** Gets the method we are looking for
     */
    private static ConstructorElement getMethod( ClassElement ce, String member) {


        int pi = member.indexOf( '(' );
        String name = member.substring( 0, pi );

        StringTokenizer tokenizer = new StringTokenizer( member.substring( pi ), " ,()" ); // NOI18N
        ArrayList paramList = new ArrayList();

        while( tokenizer.hasMoreTokens() ) {
            paramList.add( Type.parse( tokenizer.nextToken() ) );
        }

        Type[] params = new Type[ paramList.size() ];
        paramList.toArray( params );

        ConstructorElement result = ce.getConstructor( params );

        if ( result == null )
            result = ce.getMethod( Identifier.create( name ), params );


        /*
        if ( result == null ) {
          // We didn't found the method yet let's try other way
          
          findInArray( ce.getConstructors() );
          findInArray( ce.getMethods() );
          
    }
        */
        return result;
    }


    /*
    private static void findInArray( ConstructorElement[] cs ) {
      
      for ( int i = 0; i < cs.length; i++ ) {
        
        MethodParameter[] ps = cs[i].getParameters();
        
        for( int j = 0; j < ps.length; j++ ) {
          //System.out.println( ps[j].getType().toString() );
        }
        
      }
      
}
    */

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

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.