[ Pobierz całość w formacie PDF ]
.In checkClassNames( ), theclass names are extracted from the classMap (which, remember, containsonly the names in this directory, organized by file name so the file name can beprinted along with the errant class name).This is accomplished by pulling eachassociated ArrayList and stepping through that, looking to see if thefirst character is lowercase.If so, the appropriate error message is printed.[ Add Comment ]In checkIdentNames( ), asimilar approach is taken: each identifier name is extracted fromidentMap.If the name is not in the classes list, it’sassumed to be an identifier or keyword.A special case is checked: if theidentifier length is three or more and all the characters are uppercase,this identifier is ignored because it’s probably a staticfinal value such as TT_EOF.Of course, this is not a perfectalgorithm, but it assumes that you’ll eventually notice any all-uppercaseidentifiers that are out of place.[ Add Comment ]Instead of reporting every identifierthat starts with an uppercase character, this method keeps track of which oneshave already been reported in an ArrayList calledreportSet( ).This treats the ArrayList as a“set” that tells you whether an item is already in the set.The itemis produced by concatenating the file name and identifier.If the elementisn’t in the set, it’s added and then the report is made.[ Add Comment ]The rest of the listing is comprised ofmain( ), which busies itself by handling the command line argumentsand figuring out whether you’re building a repository of class names fromthe standard Java library or checking the validity of code you’ve written.In both cases it makes a ClassScanner object.[ Add Comment ]Whether you’re building arepository or using one, you must try to open the existing repository.By makinga File object and testingfor existence, you can decide whether to open the file and load( )the Properties list classes inside ClassScanner.(Theclasses from the repository add to, rather than overwrite, the classes found bythe ClassScanner constructor.) If you provide only one command-lineargument it means that you want to perform a check of the class names andidentifier names, but if you provide two arguments (the second being“-a”) you’re building a class name repository.In this case, an output file is opened and the methodProperties.save( ) is used to write the list into a file, along witha string that provides header file information.[ Add Comment ]SummaryThe Java I/O stream library does satisfythe basic requirements: you can perform reading and writing with the console, afile, a block of memory, or even across the Internet (as you will see in Chapter15).With inheritance, you can create new types of input and output objects.Andyou can even add a simple extensibility to the kinds of objects a stream willaccept by redefining the toString( ) method that’sautomatically called when you pass an object to a method that’s expectinga String (Java’s limited “automatic type conversion”).[ Add Comment ]There are questions left unanswered bythe documentation and design of the I/O stream library.For example, it wouldhave been nice if you could say that you want an exception thrown if you try tooverwrite a file when opening it for output—some programming systems allowyou to specify that you want to open an output file, but only if itdoesn’t already exist.In Java, it appears that you are supposed to use aFile object to determine whether a file exists, because if you open it asa FileOutputStream or FileWriter it will always get overwritten.[ Add Comment ]The I/O stream library brings up mixedfeelings; it does much of the job and it’s portable.But if youdon’t already understand the decorator pattern, the design isnonintuitive, so there’s extra overhead in learning and teaching it
[ Pobierz całość w formacie PDF ]