[
Date Prev][
Date Next][
Thread Prev][
Thread Next][
Date Index][
Thread Index]
[
List Home]
RE: [aspectj-users] Declare error
|
Jitesh,
Your code does a good job of testing for calls to non-default constructors. However, I believe Andre wants to check whether a class does NOT have a default constructor. AspectJ's statically resolvable pointcuts don't allow you to test for such conditions. You could test for this case with a run time check right after loading the class:
aspect EnforceRequiresDefaultConstructor {
after() returning : staticinitialization(RequiresDefaultConstructor+) {
Class clazz = thisJoinPointStaticPart.getSourceLocation().getWithinType();
try {
clazz.getConstructor(new Class[0]);
} catch (NoSuchMethodException e) {
throw new IllegalStateException("class "+clazz+"without default constructor");
} catch (SecurityException e) {
//logWarning?
}
}
}
public class RequiresDefaultConstructor {
public static void main(String args[]) throws Exception {
Legal l = new Legal("hi");
Class.forName("Illegal"); // should bomb
}
}
class Legal extends RequiresDefaultConstructor {
private int y;
public Legal() {
y=0;
}
public Legal(String x) {
this();
System.out.println("legal constructor worked: "+x);
}
}
class Illegal extends RequiresDefaultConstructor {
public Illegal(String x) {}
}
Output:
legal constructor worked: hi
Exception in thread "main" java.lang.ExceptionInInitializerError
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at RequiresDefaultConstructor.main(RequiresDefaultConstructor.aj:17)
Caused by: java.lang.IllegalStateException: class class Illegalwithout default c
onstructor
at EnforceRequiresDefaultConstructor.ajc$afterReturning$EnforceRequiresD
efaultConstructor$30(RequiresDefaultConstructor.aj:7)
at Illegal.<clinit>(RequiresDefaultConstructor.aj)
... 3 more
Ron
Ron Bodkin
Chief Technology Officer
New Aspects of Software
o: (415) 824-4690
m: (415) 509-2895
> ------------Original Message------------
> From: "JITESH KALYANI Shivanand" <SJitesh@xxxxxxxxxxxx>
> To: <aspectj-users@xxxxxxxxxxx>
> Date: Fri, Mar-26-2004 11:45 AM
> Subject: RE: [aspectj-users] Declare error
>
> HI,
>
> I have tried small example. We can have poincut to constructor and check if it is default constructor OR prameterised constructor. If it is prameterised constructor, throw error. See if this u want.
>
>
> class constructorTest
> {
> int a = 0;
>
> public static void main(String[] args)
> {
> constructorTest obj1 = new constructorTest();
> constructorTest obj2 = new constructorTest(10);
> System.out.println("Program Over!");
> }
>
> public constructorTest() {
> System.out.println("Executing Default Constructor");
> }
>
> public constructorTest(int a) {
> this.a = a;
> System.out.println("Executing parameterised Constructor : a = "+a);
> }
> }
>
>
> aspect testaspect
> {
> pointcut myConstructor() : within(constructorTest) && execution(constructorTest+.new(..));
>
> before (): myConstructor() {
> Object[] args = thisJoinPoint.getArgs();
> if(args.length == 0) {
> System.out.println("Default Constructor");
> }else {
> System.out.println("Parameterised Constructor");
> //throw error.
> }
> }
> }
>
> Jitesh
>
>
>
> > ----------
> > From: aspectj-users-admin@xxxxxxxxxxx[SMTP:aspectj-users-admin@xxxxxxxxxxx] on behalf of André Dantas Rocha[SMTP:ad-rocha@xxxxxxxxxx]
> > Reply To: aspectj-users@xxxxxxxxxxx
> > Sent: Friday, March 26, 2004 5:17 PM
> > To: aspectj-users@xxxxxxxxxxx
> > Subject: RES: [aspectj-users] Declare error
> >
> > I don't know how to....
> >
> > _____
> >
> > De: aspectj-users-admin@xxxxxxxxxxx [mailto:aspectj-users-admin@xxxxxxxxxxx] Em nome de Nicholas Lesiecki
> > Enviada em: sexta-feira, 26 de março de 2004 01:51
> > Para: aspectj-users@xxxxxxxxxxx
> > Assunto: Re: [aspectj-users] Declare error
> >
> >
> > Possibly. Can you write an attempt at doing this and submit it to the list?
> >
> > Nick
> > On Mar 25, 2004, at 12:53 PM, André Dantas Rocha wrote:
> >
> >
> >
> > Hi,
> >
> > I would like to declare an error if any subtype of ClassA do not have a default constructor.
> > Is it possible?
> >
> > Thanks
> >
> >
> >
> _______________________________________________
> aspectj-users mailing list
> aspectj-users@xxxxxxxxxxx
> http://dev.eclipse.org/mailman/listinfo/aspectj-users
>
>