javamemberdepend.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 807 行 · 第 1/2 页
JAVA
807 行
/* * @(#)JavaMemberDepend.java 1.12 06/10/10 * * Copyright 1990-2008 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). * * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA * * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. * */import util.*;import util.Set;import java.util.Enumeration;import java.util.Vector;import dependenceAnalyzer.*;import java.io.PrintStream;import java.io.FileOutputStream;import components.*;class JavaMemberDepend extends DepgenUtil{ boolean hadError = false; boolean details = false; boolean classlistonly = false; int verbosity= 0; /* * Java linking. * The member linker determines which class members * are required to satisfy the references of the * members named, taking into account the problems * introduced by method overriding and references from * unanalysed sources, as directed by command line options. * * The member linker works off a list of things that * must be included, iteratively analysing the members * on this list, and building a new list of required * members. * * This operation is controlled by a number of command-line * options: * -classpath and -exclude, as decribed below. * * -load full-member-name * to put the named member on the work list. * -loadClass full-classname * to put all members of the named class on the * work list. This is especially useful for classes * that will be used by dynamically-loaded code * in ways that cannot be analysed at this time. * -interface full-classname * to indicate that the named class, whether a * Java class or a Java interface, defines an * interface contract which must be met by its * subclasses, even if the named class, itself * is excluded from the load. This is especially * useful when packaging up something, such as * a subclass of java.lang.Applet, for dynamic * loading, and it must meet the named interface * contract. */ /* * These are the flags that control member loading: * REQ -- the marked element is required to run. Either * it was specified with -load or -loadClass, or * was determined to be necessary for something * that was. * INITREQ -- constructor required. Marks a class if * at least one of its constructors is marked REQ. * INTF -- interface. Marks a member of a class specified * with -interface. * LISTED -- just a tag for members listed by the user * as needing loading. Either as individuals or * as a member of a listed class. */ static final int REQ =(1<<29); static final int INITREQ=(1<<28); static final int INTF =(1<<27); static final int ANY =0xffffffff; static final int LISTED =(1<<26); Vector initialList = new Vector(); Vector loaded; PrintStream nativelist = null; private void loadNode( DependenceNode node ){ node.flags |= LISTED; initialList.addElement( node ); } /* * loadMember -- triggered by the command-line option * -load full-member-name */ private void loadMember( String membername ){ MemberDependenceNode node = stringToNode( membername ); if ( node == null ){ System.out.println( Localizer.getString("javafilter.cannot_find_member", membername)); hadError = true; return; } loadNode( node ); loadNode( ((MemberName)(node.name())).classEntry ); } /* * loadConstructor -- triggered by the command-line option * -new full-class-name or * -new full-class-name(signature)V */ private void loadConstructor( String constructorname ){ MemberDependenceNode node = stringToConstructorNode( constructorname ); if ( node == null ){ System.out.println( Localizer.getString("javafilter.cannot_find_constructor", constructorname) ); hadError = true; return; } loadNode( node ); loadNode( ((MemberName)(node.name())).classEntry ); } /* * loadClass -- triggered by the command-line option * -loadClass full-classname */ private void loadClass( String classname ){ ClassEntry c = stringToClass( classname ); if ( c == null ){ System.out.println( Localizer.getString("javafilter.cannot_find_class", classname) ); hadError = true; return; } loadNode( c ); allClassMembers( classname ); } /* * interfaceClass -- triggered by the command-line option * -interface full-classname */ private boolean interfaceClass( String classname ){ if ( ! this.filter.accept( null, sanitizeClassname( classname ) ) ){ // oops. Not excluded. System.err.println( Localizer.getString("javafilter.class_interface_but_not_exclude" ,classname) ); loadClass( classname ); return true; } ClassEntry c = stringToClass( classname ); if ( c == null ){ System.out.println( Localizer.getString("javafilter.cannot_find_class", classname) ); hadError = true; return false; } if ( ! analyzer.makeInterface( c ) ){ // big magic here! System.err.println( Localizer.getString("javafilter.could_not_make_interface", classname)); hadError = true; return false;// something bad happened here. } Enumeration memberlist = c.members(); if ( memberlist == null ) return true; // no work. while ( memberlist.hasMoreElements() ){ DependenceNode node = (DependenceNode)( memberlist.nextElement() ); node.flags |= INTF; } return true; } /* * -wholeClass classname * -wholeClass packagename.* * also implicit in -loadClass */ private void allClassMembers( String name ){ if ( wholeClass == null ) wholeClass = new ClassnameFilter(); wholeClass.includeName( sanitizeClassname( name ) ); } /* * -wholeData classname * -wholeData packagename.* */ private void allClassData( String name ){ if ( wholeData == null ) wholeData = new ClassnameFilter(); wholeData.includeName( sanitizeClassname( name ) ); } class DependenceRelation{ String fromName; String verb; String toName; DependenceRelation( String f, String v, String t ){ fromName = f; verb = v; toName = t; } } /* * addedDepencence -- called in response to a command-line * option such as * -dependence full-member-name verb full-member-name */ private void addDependence( DependenceRelation r ){ DependenceNode fromNode = stringToNode( r.fromName ); MemberDependenceNode destMember = null; DependenceNode destClass = null; boolean toClass = false; int arcType; if ( r.verb.equalsIgnoreCase("reads") ){ toClass = false; arcType = ARC_READS; } else if ( r.verb.equalsIgnoreCase("writes") ){ toClass = false; arcType = ARC_WRITES; } else if ( r.verb.equalsIgnoreCase("calls") ){ toClass = false; arcType = ARC_CALLS; } else if ( r.verb.equalsIgnoreCase("classref") ){ toClass = true; arcType = ARC_CLASS; } else { System.out.println( Localizer.getString("javafilter.unrecognized_dependence_verb", r.verb)); hadError = true; return; } try { if ( toClass ){ destClass = stringToClass( r.toName ); } else { destMember = stringToNode( r.toName ); destClass = ((MemberName)(destMember.name())).classEntry; analyzer.addDependenceArc( fromNode, destMember, arcType ); } analyzer.addDependenceArc( fromNode, destClass, ARC_CLASS ); } catch ( NullPointerException e ){ /* * This is extra-cautious. I don't believe any of these * situations can occur using the current DependenceAnalyzers. * I believe that, barring catastrophies such as running out of memory, * data structures will be created on-the-fly by the stringToXXX * methods. */ if ( fromNode == null ){ System.out.println( Localizer.getString("javafilter.cannot_find", r.fromName)); hadError = true; return; } if ( !toClass ){ if ( destMember == null ){ System.out.println( Localizer.getString("javafilter.cannot_find", r.toName)); hadError = true; return; } } if ( destClass == null ){ System.out.println( Localizer.getString("javafilter.cannot_find", r.toName)); hadError = true; return; } // else, none of the above // propigate the error, as it is not one we want to deal with. throw e; } } /* * doLoad(). Called at the end of argument processing. */ private void doLoad(){ // first, work off the lists we collected // during argument processing. // all the -classpath and -exclude stuff // has been taken care of. // add interfaces and dependences first. // then things to load Enumeration work = interfaceClasses.elements(); while ( work.hasMoreElements() ){ interfaceClass( (String) ( work.nextElement() ) ); } work = dependences.elements(); while ( work.hasMoreElements() ){ addDependence( (DependenceRelation)( work.nextElement() ) ); } work = membersToLoad.elements(); while ( work.hasMoreElements() ){ loadMember( (String)( work.nextElement() ) ); } work = constructorsToLoad.elements(); while ( work.hasMoreElements() ){ loadConstructor( (String)( work.nextElement() ) ); } work = classesToLoad.elements(); while ( work.hasMoreElements() ){ loadClass( (String)( work.nextElement() ) ); } work = paths.elements(); while ( work.hasMoreElements() ){ PathPair pp = (PathPair)(work.nextElement()); loadMember( (String) pp.src ); loadMember( (String) pp.dest ); } work = showClasses.elements(); while ( work.hasMoreElements() ){ loadClass( (String)( work.nextElement() ) ); } if ( hadError ) return; // oops. if ( initialList.size() == 0) return; // vacuous! if ( verbosity > 0 ){ System.err.println( Localizer.getString("javafilter.loading_based_on_these_requirements")); printList( System.err, initialList ); } if ( wholeClass == null ) wholeClass = new NeverAccept(); if ( wholeData == null ) wholeData = new NeverAccept(); loaded = processList( initialList, REQ, INITREQ, REQ|INTF, verbosity ); if ( loaded == null ) { System.err.println(Localizer.getString("javafilter.nothing_to_load")); } else if (verbosity > 0 ){ System.out.println("These members loaded:"); printList( System.out, loaded ); } } static void printDependences( DependenceNode node ){ if ( node.state() != DependenceNode.ANALYZED ){ if (node.state() == DependenceNode.ERROR) System.out.println( node.name()+" unanalysed"); return; } System.out.println( node.name()+" depends on:"); Enumeration e = node.dependsOn(); while ( e.hasMoreElements() ){ DependenceArc a = (DependenceArc)(e.nextElement()); Object mn = a.to().name(); String dep = (String) mn.toString();/* if (dep.startsWith("java/io") || dep.startsWith("java/lang") || dep.startsWith("java/net") || dep.startsWith("java/util") ) continue;*/ System.out.println(" "+dep ); } } static void printClassList( DependenceNode node ){ if ( node.state() != DependenceNode.ANALYZED ){ // if (node.state() == DependenceNode.ERROR) // System.out.println( node.name()+" unanalysed"); return; } String str = node.name().toString();
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?