Packages in Java - IMPORTANT FOR HW2


Subject: Packages in Java - IMPORTANT FOR HW2
From: Paul Freeman (pfreeman@ccs.neu.edu)
Date: Wed Oct 09 2002 - 10:52:01 EDT


Hi Class -

I have received a couple of questions regarding the use of Packages in
Java.

You can think of a Java package as a grouping of files that support a
particular concern. For example in the second part of homework 2, the
following files are part of the "lawOfDemeter" package:
Any.java
Supplier.java
Checker.java

This is noted in each of the above classes by the line:
package lawOfDemeter;

as the first line of code in the file.

it is not enough to simply denote the package at the top of the file,
the classes must also be placed in a folder that corresponds to the
package name, in this case a folder named "lawOfDemeter"
THIS IS IMPORTANT for hw 2 since the "scope" aspect in the Any.java file
restricts the application of pointcuts to:
!within(lawOfDemeter..*)
This means that all classes in the lawOfDemeter package will be ignored
the pointcuts that utilize the "scope" pointcut. So in order to have
the pointcuts in the Any.java file be applied to a test class, that test
class must NOT be in the lawOfDemeter package. It can be in either it's
own package or no package at all, that is up to you.

Packages can be nested. Each nesting (or subdirectory) must be separated
by a period "." in the package declaration line. for example if the
lawOfDemeter package was placed in a com3205_hw2 package the package
statement at the top of all 2 files would need to be changed to:
package com3205_hw2.lawOfDemeter;
and of course the lawOfDemeter directory would need to be placed within
a com3205_hw2 directory.

What does all this mean? Well it changes how you compile your classes
with the aspectj compiler. You can't compile your packages separately
since the lawOfDemeter package containing your aspects must be aware of
the java classes you are applying them to at compile time (see previous
email regarding how aspectj works and the -preprocess command). This is
why using a ".lst" file might be easier at this point.

So as an example for part 2 of homework 2...

all of my files are contained in the directory:
hw2/part2/

my java source files are contained in the directory:
hw2/part2/src/

within the src directory I have two directories corresponding to the
packages my source files belong to, i.e. corresponding to the package
declaration at the top of the files:
lawOfDemeter/
testcase/

lawOfDemeter contains (these are files with "package lawOfDemeter;"
stated at the top):
Any.java
Supplier.java
Checker.java

testcase contains (these are files with "package testcase;" stated at
the top):
Foo.java

For easier compilation with ajc I created a ".lst" file named
"part2.lst" located in the directory:
hw2/part2/

The body of the file is as follows:
src/lawofdemeter/Any.java
src/lawofdemeter/Checker.java
src/lawofdemeter/Supplier.java
src/testcase/Foo.java

I prefer to compile my classes to a separate directory called "classes".
 This can be done using "-d <directoryname>" option of ajc. So to do
the compilation I navigate to the directory:
hw2/part2/
and use the command:
ajc -argfile part2.lst -d classes

Now to run the application I need to change to the directory:
hw2/part2/classes/

and enter the command
java testcase.Foo

Hope this helps clear up how to use packages.

Paul



This archive was generated by hypermail 2b28 : Wed Oct 09 2002 - 10:54:26 EDT