📄 gop.jrag
字号:
/***
**
*/
aspect GOP{
//RootClassAccess in GranuleDecl is firstly a TYPENAME
inh NameType RootClassAccess.nameType();
eq GranuleDecl.getRootClassAccess().nameType() = NameType.TYPE_NAME;
public boolean GranuleDecl.hasRootClass(){
return true;
}
inh NameType GranuleAccess.nameType();
eq ShadowClassDecl.getGranuleAccess().nameType() = NameType.TYPE_NAME;
syn String GranuleAccess.name() = getID();
public boolean ShadowClassDecl.hasSeedClass() {
return true;
}
public boolean ShadowClassDecl.hasGranuleAccess(){
return true;
}
//=======================lookupVariable()==================================
//the scope of fielddeclration contains all its shadowclass
eq ShadowClassDecl.getBodyDecl(int i).lookupVariable(String name) {
SimpleSet list = memberFields(name);
if(!list.isEmpty()) return list;
list = lookupVariable(name);
if(inStaticContext() || isStatic())
list = removeInstanceVariables(list);
return list;
}
//Shadowclass automatically contains its seedclass's field
eq ShadowClassDecl.memberFields(String name) {
SimpleSet fields = localFields(name);
if(!fields.isEmpty())
return fields; // this causes hiding of fields in superclass and interfaces
//find field in seedclass
TypeDecl seedClass = seedClass(getID());
if(seedClass!=null){
for(Iterator iter = seedClass.memberFields(name).iterator(); iter.hasNext(); ) {
FieldDeclaration decl = (FieldDeclaration)iter.next();
fields = fields.add(decl);
}
if(!fields.isEmpty())
return fields;
}
//find field in GranuleAccess
TypeDecl granuleIn = granuleAccess(((TypeAccess)getGranuleAccess()).name());
if(granuleIn!=null){
for(Iterator iter = granuleIn.memberFields(name).iterator(); iter.hasNext(); ) {
FieldDeclaration decl = (FieldDeclaration)iter.next();
fields = fields.add(decl);
}
if(!fields.isEmpty())
return fields;
}
if(hasSuperclass()) {
for(Iterator iter = superclass().memberFields(name).iterator(); iter.hasNext(); ) {
FieldDeclaration decl = (FieldDeclaration)iter.next();
if(!decl.isPrivate() && decl.accessibleFrom(this))
fields = fields.add(decl);
}
}
for(Iterator outerIter = interfacesIterator(); outerIter.hasNext(); ) {
TypeDecl type = (TypeDecl)outerIter.next();
for(Iterator iter = type.memberFields(name).iterator(); iter.hasNext(); ) {
FieldDeclaration decl = (FieldDeclaration)iter.next();
if(!decl.isPrivate() && decl.accessibleFrom(this))
fields = fields.add(decl);
}
}
return fields;
}
//=========================lookupMethod()=============================================
syn Collection ShadowClassDecl.memberMethods(String name) {
Collection c = (Collection)methodsNameMap().get(name);
if(c != null) return c;
return Collections.EMPTY_LIST;
}
eq ShadowClassDecl.methodsSignatureMap() {
HashMap map = new HashMap(localMethodsSignatureMap());
if(hasSeedClass()){
for(Iterator iter = seedClass(getID()).methodsIterator(); iter.hasNext(); ) {
MethodDecl m = (MethodDecl)iter.next();
putSimpleSetElement(map, m.signature(), m);
}
}
if(hasSuperclass()) {
for(Iterator iter = superclass().methodsIterator(); iter.hasNext(); ) {
MethodDecl m = (MethodDecl)iter.next();
if(!m.isPrivate() && m.accessibleFrom(this) && !localMethodsSignatureMap().containsKey(m.signature()))
putSimpleSetElement(map, m.signature(), m);
}
}
for(Iterator outerIter = interfacesIterator(); outerIter.hasNext(); ) {
TypeDecl typeDecl = (TypeDecl)outerIter.next();
for(Iterator iter = typeDecl.methodsIterator(); iter.hasNext(); ) {
MethodDecl m = (MethodDecl)iter.next();
if(!m.isPrivate() && m.accessibleFrom(this) && !localMethodsSignatureMap().containsKey(m.signature()))
if(allMethodsAbstract((SimpleSet)map.get(m.signature())))
putSimpleSetElement(map, m.signature(), m);
}
}
return map;
}
//===========Granule.lookupVarialbe()==========================================
eq GranuleDecl.memberFields(String name) {
SimpleSet fields = localFields(name);
if(!fields.isEmpty())
return fields; // this causes hiding of fields in superclass and interfaces
//find field in rootclass
TypeDecl rootClass = rootClass(((TypeAccess)getRootClassAccess()).name());
if(rootClass!=null){
for(Iterator iter = rootClass.memberFields(name).iterator(); iter.hasNext(); ) {
FieldDeclaration decl = (FieldDeclaration)iter.next();
fields = fields.add(decl);
}
if(!fields.isEmpty())
return fields;
}
return fields;
}
//==========================Granule======================================
/* rewrite InstanceInitializer {
when(hostType().isGranuleDecl())
to GranuleBody {
return new GranuleBody(getBlock());
}
}
*/
rewrite InstanceInitializer {
when(hostType().isGranuleDecl())
to MethodDecl {
Modifier m = new Modifier("private");
List<Modifier> l= new List<Modifier>();
l.add(m);
Modifiers ms = new Modifiers(l);
Access access =new PrimitiveTypeAccess("boolean");
List<ParameterDeclaration> l3=null;
List<Access> l4 =null;
Opt<Block> opt = new Opt(getBlock());
return new MethodDecl(ms, access, hostType().getID(), l3, l4, opt);
}
}
eq GranuleBody.getBlock().reachable() = true;
syn TypeDecl ShadowClassDecl.seedClass(String name){
TypeDecl decl = lookupType(packageName(),name);
if(!decl.isClassDecl())
return null;
else
return decl;
}
syn TypeDecl ShadowClassDecl.granuleAccess(String name){
TypeDecl decl = lookupType(packageName(),name);
if(!decl.isGranuleDecl())
return null;
else
return decl;
}
syn TypeDecl GranuleDecl.rootClass(String name){
TypeDecl decl = lookupType(packageName(),name);
if(!decl.isClassDecl())
return null;
else
return decl;
}
syn boolean TypeDecl.isShadowClassDecl() = false;
eq ShadowClassDecl.isShadowClassDecl() = true;
syn boolean TypeDecl.isGranuleDecl() = false;
eq GranuleDecl.isGranuleDecl() = true;
//===========================================================================
//Type check
inh TypeDecl GranuleBody.typeBoolean();
eq GranuleBody.getBlock().returnType() = typeBoolean();
refine TypeCheck public void ReturnStmt.typeCheck() {
if(hasResult() && !returnType().isVoid()) {
if(!getResult().type().assignConversionTo(returnType(), getResult()))
error("return value must be an instance of " + returnType().typeName() + " which " + getResult().type().typeName() + " is not");
}
if(enclosingBodyDecl() instanceof GranuleBody && !getResult().type().isBoolean())
error("GranuleBody must retuan type boolean!");
// 8.4.5 8.8.5
if(returnType().isVoid() && hasResult())
error("return stmt may not have an expression in void methods");
// 8.4.5
if(!returnType().isVoid() && !hasResult())
error("return stmt must have an expression in non void methods");
if(enclosingBodyDecl() instanceof InstanceInitializer || enclosingBodyDecl() instanceof StaticInitializer)
error("Initializers may not return");
}
public void GranuleDecl.typeCheck(){
boolean b=false;
for(int i = 0; i < getNumBodyDecl(); i++) {
if(b)
error(" granule can only have one GranuleBody exsit");
BodyDecl bodyDecl = getBodyDecl(i);
if(bodyDecl instanceof MethodDecl || bodyDecl instanceof StaticInitializer || bodyDecl instanceof ConstructorDecl )
error(" granule can not have method or initializer or constructor memeber.");
if(bodyDecl instanceof GranuleBody)
b=!b;
}
}
//==================================================================
//name Check to make sure shadowclass has its seedclass
public void ShadowClassDecl.nameCheck() {
if(!isTopLevelType())
error("ShadowClass can only be topLevelType");
TypeDecl seedClass = seedClass(getID());
if(seedClass==null)
error("No TypeDecl named " + getID() + " exsit as the seedclass of shadowclass,make sure seedclass is declared before shadowclass");
else if(!seedClass.isClassDecl())
error("SeedClass can only be ClassDecl");
TypeDecl granuleAccess =granuleAccess(((TypeAccess)getGranuleAccess()).name());
if(granuleAccess==null)
error("Can not find granule "+ ((TypeAccess)getGranuleAccess()).name());
}
//===============================================================
public boolean ShadowClassDecl.hasSuperclass() {
return !isObject();
}
public ClassDecl ShadowClassDecl.superclass() {
if(isObject())
return null;
if(hasSuperClassAccess() && !isCircular() && getSuperClassAccess().type().isClassDecl())
return (ClassDecl)getSuperClassAccess().type();
return (ClassDecl)typeObject();
}
public Iterator ShadowClassDecl.interfacesIterator() {
return new Iterator() {
public boolean hasNext() {
computeNextCurrent();
return current != null;
}
public Object next() {
return current;
}
public void remove() {
throw new UnsupportedOperationException();
}
private int index = 0;
private TypeDecl current = null;
private void computeNextCurrent() {
current = null;
if(isObject() || isCircular())
return;
while(index < getNumImplements()) {
TypeDecl typeDecl = getImplements(index++).type();
if(!typeDecl.isCircular() && typeDecl.isInterfaceDecl()) {
current = typeDecl;
return;
}
}
}
};
}
//================================================================
refine NameCheck public void VarAccess.nameCheck() {
if(decls().isEmpty() && (!isQualified() || !qualifier().type().isUnknown() || qualifier().isPackageAccess()))
error("no field named " + name());
if(decls().size() > 1) {
StringBuffer s = new StringBuffer();
s.append("several fields named " + name());
for(Iterator iter = decls().iterator(); iter.hasNext(); ) {
Variable v = (Variable)iter.next();
s.append("\n " + v.type().typeName() + "." + v.name() + " declared in " + v.hostType().typeName());
}
error(s.toString());
}
// 8.8.5.1
if(inExplicitConstructorInvocation() && !isQualified() && decl().isInstanceVariable() && hostType() == decl().hostType())
error("instance variable " + name() + " may not be accessed in an explicit constructor invocation");
Variable v = decl();
if(!v.isFinal() && !v.isClassVariable() && !v.isInstanceVariable() && v.hostType() != hostType() && !v.hostType().name().equals(hostType().name()) && ! v.hostType().isGranuleDecl() )
error("A parameter/variable used but not declared in an inner class must be declared final");
// 8.3.2.3
if((decl().isInstanceVariable() || decl().isClassVariable()) && !isQualified()) {
if(hostType() != null && !hostType().declaredBeforeUse(decl(), this)) {
if(inSameInitializer() && !simpleAssignment() && inDeclaringClass()) {
BodyDecl b = closestBodyDecl(hostType());
error("variable " + decl().name() + " is used in " + b + " before it is declared");
}
}
}
}
}
aspect GOPModifier{
syn lazy boolean Modifiers.isExternal() = numModifier("external") != 0;
syn boolean FieldDeclaration.isExternal() = getModifiers().isExternal();
refine Modifiers public void FieldDeclaration.checkModifiers() {
super.checkModifiers();
if(hostType().isInterfaceDecl()) {
if(isProtected())
error("an interface field may not be protected");
if(isPrivate())
error("an interface field may not be private");
if(isTransient())
error("an interface field may not be transient");
if(isVolatile())
error("an interface field may not be volatile");
}
if(!hostType().isGranuleDecl() && isExternal())
error("external field can only exsit in Granule");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -