diff options
Diffstat (limited to 'src/main/java/io/devnulllabs/openjava/ptree/util')
10 files changed, 4384 insertions, 0 deletions
diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/ClassLiteralReplacer.java b/src/main/java/io/devnulllabs/openjava/ptree/util/ClassLiteralReplacer.java new file mode 100644 index 0000000..372343d --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/ClassLiteralReplacer.java @@ -0,0 +1,57 @@ +/* + * ClassLiteralReplacer.java + * + * Make typenames qualified. + * <p> + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.ptree.util; + +import io.devnulllabs.openjava.mop.Environment; +import io.devnulllabs.openjava.mop.OJClass; +import io.devnulllabs.openjava.ptree.ClassLiteral; +import io.devnulllabs.openjava.ptree.Expression; +import io.devnulllabs.openjava.ptree.ExpressionList; +import io.devnulllabs.openjava.ptree.MethodCall; +import io.devnulllabs.openjava.ptree.ParseTreeException; +import io.devnulllabs.openjava.ptree.TypeName; + +/** + * The class <code>ClassLiteralReplacer</code> + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: ClassLiteralReplacer.java,v 1.2 2003/02/19 02:55:00 tatsubori Exp $ + * @see java.lang.Object + */ +public class ClassLiteralReplacer extends EvaluationShuttle { + public static final String OLDCLASS_PREFIX = "oldjavaclass."; + + public ClassLiteralReplacer(Environment env) { + super(env); + } + + public Expression evaluateDown(ClassLiteral ptree) + throws ParseTreeException { + TypeName type = ptree.getTypeName(); + + if (type.toString().startsWith(OLDCLASS_PREFIX)) { + String name = type.getName(); + name = name.substring(OLDCLASS_PREFIX.length()); + int dim = type.getDimension(); + return new ClassLiteral(new TypeName(name, dim)); + } + + ExpressionList args = new ExpressionList(new ClassLiteral(type)); + Expression result = + new MethodCall(OJClass.forClass(OJClass.class), "forClass", args); + return result; + } + +} diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/EvaluationShuttle.java b/src/main/java/io/devnulllabs/openjava/ptree/util/EvaluationShuttle.java new file mode 100644 index 0000000..b5f77bf --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/EvaluationShuttle.java @@ -0,0 +1,1138 @@ +/* + * EvaluationShuttle.java + * + * comments here. + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.ptree.util; + +import io.devnulllabs.openjava.mop.Environment; +import io.devnulllabs.openjava.ptree.AllocationExpression; +import io.devnulllabs.openjava.ptree.ArrayAccess; +import io.devnulllabs.openjava.ptree.ArrayAllocationExpression; +import io.devnulllabs.openjava.ptree.ArrayInitializer; +import io.devnulllabs.openjava.ptree.AssignmentExpression; +import io.devnulllabs.openjava.ptree.BinaryExpression; +import io.devnulllabs.openjava.ptree.Block; +import io.devnulllabs.openjava.ptree.BreakStatement; +import io.devnulllabs.openjava.ptree.CaseGroup; +import io.devnulllabs.openjava.ptree.CaseGroupList; +import io.devnulllabs.openjava.ptree.CaseLabel; +import io.devnulllabs.openjava.ptree.CaseLabelList; +import io.devnulllabs.openjava.ptree.CastExpression; +import io.devnulllabs.openjava.ptree.CatchBlock; +import io.devnulllabs.openjava.ptree.CatchList; +import io.devnulllabs.openjava.ptree.ClassDeclaration; +import io.devnulllabs.openjava.ptree.ClassDeclarationList; +import io.devnulllabs.openjava.ptree.ClassLiteral; +import io.devnulllabs.openjava.ptree.CompilationUnit; +import io.devnulllabs.openjava.ptree.ConditionalExpression; +import io.devnulllabs.openjava.ptree.ConstructorDeclaration; +import io.devnulllabs.openjava.ptree.ConstructorInvocation; +import io.devnulllabs.openjava.ptree.ContinueStatement; +import io.devnulllabs.openjava.ptree.DoWhileStatement; +import io.devnulllabs.openjava.ptree.EmptyStatement; +import io.devnulllabs.openjava.ptree.Expression; +import io.devnulllabs.openjava.ptree.ExpressionList; +import io.devnulllabs.openjava.ptree.ExpressionStatement; +import io.devnulllabs.openjava.ptree.FieldAccess; +import io.devnulllabs.openjava.ptree.FieldDeclaration; +import io.devnulllabs.openjava.ptree.ForStatement; +import io.devnulllabs.openjava.ptree.IfStatement; +import io.devnulllabs.openjava.ptree.InstanceofExpression; +import io.devnulllabs.openjava.ptree.LabeledStatement; +import io.devnulllabs.openjava.ptree.Literal; +import io.devnulllabs.openjava.ptree.MemberDeclaration; +import io.devnulllabs.openjava.ptree.MemberDeclarationList; +import io.devnulllabs.openjava.ptree.MemberInitializer; +import io.devnulllabs.openjava.ptree.MethodCall; +import io.devnulllabs.openjava.ptree.MethodDeclaration; +import io.devnulllabs.openjava.ptree.ModifierList; +import io.devnulllabs.openjava.ptree.Parameter; +import io.devnulllabs.openjava.ptree.ParameterList; +import io.devnulllabs.openjava.ptree.ParseTreeException; +import io.devnulllabs.openjava.ptree.ReturnStatement; +import io.devnulllabs.openjava.ptree.SelfAccess; +import io.devnulllabs.openjava.ptree.Statement; +import io.devnulllabs.openjava.ptree.StatementList; +import io.devnulllabs.openjava.ptree.SwitchStatement; +import io.devnulllabs.openjava.ptree.SynchronizedStatement; +import io.devnulllabs.openjava.ptree.ThrowStatement; +import io.devnulllabs.openjava.ptree.TryStatement; +import io.devnulllabs.openjava.ptree.TypeName; +import io.devnulllabs.openjava.ptree.UnaryExpression; +import io.devnulllabs.openjava.ptree.Variable; +import io.devnulllabs.openjava.ptree.VariableDeclaration; +import io.devnulllabs.openjava.ptree.VariableDeclarator; +import io.devnulllabs.openjava.ptree.VariableInitializer; +import io.devnulllabs.openjava.ptree.WhileStatement; + +/** + * The class <code>EvaluationShuttle</code> is a Visitor role + * in the Visitor pattern and this also visits each child + * <code>ParseTree</code> object from left to right. + * <p> + * The class <code>Evaluator</code> is an evaluator of each + * objects of <code>ParseTree</code> family. Each methods in + * this class is invoked from the class <code>EvaluationShuttle</code>. + * <p> + * The method <code>evaluateDown()</code> is invoked before evaluating + * the children of the parse tree object, and <code>evaluateUp()</code> + * is invoked after the evaluation. + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: EvaluationShuttle.java,v 1.2 2003/02/19 02:55:00 tatsubori Exp $ + * @see io.devnulllabs.openjava.ptree.ParseTree + * @see io.devnulllabs.openjava.ptree.util.ParseTreeVisitor + */ +public abstract class EvaluationShuttle extends ParseTreeVisitor { + private Environment env; + + public EvaluationShuttle(Environment env) { + this.env = env; + } + + protected Environment getEnvironment() { + return env; + } + + protected void setEnvironment(Environment env) { + this.env = env; + } + + public Expression evaluateDown(AllocationExpression p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(ArrayAccess p) throws ParseTreeException { + return p; + } + public Expression evaluateDown(ArrayAllocationExpression p) + throws ParseTreeException { + return p; + } + public VariableInitializer evaluateDown(ArrayInitializer p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(AssignmentExpression p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(BinaryExpression p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(Block p) throws ParseTreeException { + return p; + } + public Statement evaluateDown(BreakStatement p) throws ParseTreeException { + return p; + } + public CaseGroup evaluateDown(CaseGroup p) throws ParseTreeException { + return p; + } + public CaseGroupList evaluateDown(CaseGroupList p) + throws ParseTreeException { + return p; + } + public CaseLabel evaluateDown(CaseLabel p) throws ParseTreeException { + return p; + } + public CaseLabelList evaluateDown(CaseLabelList p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(CastExpression p) + throws ParseTreeException { + return p; + } + public CatchBlock evaluateDown(CatchBlock p) throws ParseTreeException { + return p; + } + public CatchList evaluateDown(CatchList p) throws ParseTreeException { + return p; + } + public ClassDeclaration evaluateDown(ClassDeclaration p) + throws ParseTreeException { + return p; + } + public ClassDeclarationList evaluateDown(ClassDeclarationList p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(ClassLiteral p) throws ParseTreeException { + return p; + } + public CompilationUnit evaluateDown(CompilationUnit p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(ConditionalExpression p) + throws ParseTreeException { + return p; + } + public MemberDeclaration evaluateDown(ConstructorDeclaration p) + throws ParseTreeException { + return p; + } + public ConstructorInvocation evaluateDown(ConstructorInvocation p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(ContinueStatement p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(DoWhileStatement p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(EmptyStatement p) throws ParseTreeException { + return p; + } + public ExpressionList evaluateDown(ExpressionList p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(ExpressionStatement p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(FieldAccess p) throws ParseTreeException { + return p; + } + public MemberDeclaration evaluateDown(FieldDeclaration p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(ForStatement p) throws ParseTreeException { + return p; + } + public Statement evaluateDown(IfStatement p) throws ParseTreeException { + return p; + } + public Expression evaluateDown(InstanceofExpression p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(LabeledStatement p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(Literal p) throws ParseTreeException { + return p; + } + public MemberDeclarationList evaluateDown(MemberDeclarationList p) + throws ParseTreeException { + return p; + } + public MemberDeclaration evaluateDown(MemberInitializer p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(MethodCall p) throws ParseTreeException { + return p; + } + public MemberDeclaration evaluateDown(MethodDeclaration p) + throws ParseTreeException { + return p; + } + public ModifierList evaluateDown(ModifierList p) + throws ParseTreeException { + return p; + } + public Parameter evaluateDown(Parameter p) throws ParseTreeException { + return p; + } + public ParameterList evaluateDown(ParameterList p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(ReturnStatement p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(SelfAccess p) throws ParseTreeException { + return p; + } + public StatementList evaluateDown(StatementList p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(SwitchStatement p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(SynchronizedStatement p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(ThrowStatement p) throws ParseTreeException { + return p; + } + public Statement evaluateDown(TryStatement p) throws ParseTreeException { + return p; + } + public TypeName evaluateDown(TypeName p) throws ParseTreeException { + return p; + } + public Expression evaluateDown(UnaryExpression p) + throws ParseTreeException { + return p; + } + public Expression evaluateDown(Variable p) throws ParseTreeException { + return p; + } + public Statement evaluateDown(VariableDeclaration p) + throws ParseTreeException { + return p; + } + public VariableDeclarator evaluateDown(VariableDeclarator p) + throws ParseTreeException { + return p; + } + public Statement evaluateDown(WhileStatement p) throws ParseTreeException { + return p; + } + + public Expression evaluateUp(AllocationExpression p) + throws ParseTreeException { + return p; + } + public Expression evaluateUp(ArrayAccess p) throws ParseTreeException { + return p; + } + public Expression evaluateUp(ArrayAllocationExpression p) + throws ParseTreeException { + return p; + } + public VariableInitializer evaluateUp(ArrayInitializer p) + throws ParseTreeException { + return p; + } + public Expression evaluateUp(AssignmentExpression p) + throws ParseTreeException { + return p; + } + public Expression evaluateUp(BinaryExpression p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(Block p) throws ParseTreeException { + return p; + } + public Statement evaluateUp(BreakStatement p) throws ParseTreeException { + return p; + } + public CaseGroup evaluateUp(CaseGroup p) throws ParseTreeException { + return p; + } + public CaseGroupList evaluateUp(CaseGroupList p) + throws ParseTreeException { + return p; + } + public CaseLabel evaluateUp(CaseLabel p) throws ParseTreeException { + return p; + } + public CaseLabelList evaluateUp(CaseLabelList p) + throws ParseTreeException { + return p; + } + public Expression evaluateUp(CastExpression p) throws ParseTreeException { + return p; + } + public CatchBlock evaluateUp(CatchBlock p) throws ParseTreeException { + return p; + } + public CatchList evaluateUp(CatchList p) throws ParseTreeException { + return p; + } + public ClassDeclaration evaluateUp(ClassDeclaration p) + throws ParseTreeException { + return p; + } + public ClassDeclarationList evaluateUp(ClassDeclarationList p) + throws ParseTreeException { + return p; + } + public Expression evaluateUp(ClassLiteral p) throws ParseTreeException { + return p; + } + public CompilationUnit evaluateUp(CompilationUnit p) + throws ParseTreeException { + return p; + } + public Expression evaluateUp(ConditionalExpression p) + throws ParseTreeException { + return p; + } + public MemberDeclaration evaluateUp(ConstructorDeclaration p) + throws ParseTreeException { + return p; + } + public ConstructorInvocation evaluateUp(ConstructorInvocation p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(ContinueStatement p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(DoWhileStatement p) throws ParseTreeException { + return p; + } + public Statement evaluateUp(EmptyStatement p) throws ParseTreeException { + return p; + } + public ExpressionList evaluateUp(ExpressionList p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(ExpressionStatement p) + throws ParseTreeException { + return p; + } + public Expression evaluateUp(FieldAccess p) throws ParseTreeException { + return p; + } + public MemberDeclaration evaluateUp(FieldDeclaration p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(ForStatement p) throws ParseTreeException { + return p; + } + public Statement evaluateUp(IfStatement p) throws ParseTreeException { + return p; + } + public Expression evaluateUp(InstanceofExpression p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(LabeledStatement p) throws ParseTreeException { + return p; + } + public Expression evaluateUp(Literal p) throws ParseTreeException { + return p; + } + public MemberDeclarationList evaluateUp(MemberDeclarationList p) + throws ParseTreeException { + return p; + } + public MemberDeclaration evaluateUp(MemberInitializer p) + throws ParseTreeException { + return p; + } + public Expression evaluateUp(MethodCall p) throws ParseTreeException { + return p; + } + public MemberDeclaration evaluateUp(MethodDeclaration p) + throws ParseTreeException { + return p; + } + public ModifierList evaluateUp(ModifierList p) throws ParseTreeException { + return p; + } + public Parameter evaluateUp(Parameter p) throws ParseTreeException { + return p; + } + public ParameterList evaluateUp(ParameterList p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(ReturnStatement p) throws ParseTreeException { + return p; + } + public Expression evaluateUp(SelfAccess p) throws ParseTreeException { + return p; + } + public StatementList evaluateUp(StatementList p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(SwitchStatement p) throws ParseTreeException { + return p; + } + public Statement evaluateUp(SynchronizedStatement p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(ThrowStatement p) throws ParseTreeException { + return p; + } + public Statement evaluateUp(TryStatement p) throws ParseTreeException { + return p; + } + public TypeName evaluateUp(TypeName p) throws ParseTreeException { + return p; + } + public Expression evaluateUp(UnaryExpression p) throws ParseTreeException { + return p; + } + public Expression evaluateUp(Variable p) throws ParseTreeException { + return p; + } + public Statement evaluateUp(VariableDeclaration p) + throws ParseTreeException { + return p; + } + public VariableDeclarator evaluateUp(VariableDeclarator p) + throws ParseTreeException { + return p; + } + public Statement evaluateUp(WhileStatement p) throws ParseTreeException { + return p; + } + + public void visit(AllocationExpression p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ArrayAccess p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ArrayAllocationExpression p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ArrayInitializer p) throws ParseTreeException { + VariableInitializer newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(AssignmentExpression p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(BinaryExpression p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(Block p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(BreakStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(CaseGroup p) throws ParseTreeException { + this.evaluateDown(p); + p.childrenAccept(this); + this.evaluateUp(p); + } + + public void visit(CaseGroupList p) throws ParseTreeException { + CaseGroupList newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(CaseLabel p) throws ParseTreeException { + CaseLabel newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(CaseLabelList p) throws ParseTreeException { + CaseLabelList newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(CastExpression p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(CatchBlock p) throws ParseTreeException { + CatchBlock newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(CatchList p) throws ParseTreeException { + CatchList newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ClassDeclaration p) throws ParseTreeException { + ClassDeclaration newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ClassDeclarationList p) throws ParseTreeException { + ClassDeclarationList newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ClassLiteral p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(CompilationUnit p) throws ParseTreeException { + CompilationUnit newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ConditionalExpression p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ConstructorDeclaration p) throws ParseTreeException { + MemberDeclaration newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ConstructorInvocation p) throws ParseTreeException { + ConstructorInvocation newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ContinueStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(DoWhileStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(EmptyStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ExpressionList p) throws ParseTreeException { + ExpressionList newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ExpressionStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + /* if not same as original, do not continue */ + public void visit(FieldAccess p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(FieldDeclaration p) throws ParseTreeException { + MemberDeclaration newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ForStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(IfStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(InstanceofExpression p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(LabeledStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(Literal p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(MemberDeclarationList p) throws ParseTreeException { + MemberDeclarationList newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(MemberInitializer p) throws ParseTreeException { + MemberDeclaration newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(MethodCall p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(MethodDeclaration p) throws ParseTreeException { + MemberDeclaration newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ModifierList p) throws ParseTreeException { + ModifierList newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(Parameter p) throws ParseTreeException { + Parameter newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ParameterList p) throws ParseTreeException { + ParameterList newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ReturnStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(SelfAccess p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(StatementList p) throws ParseTreeException { + StatementList newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(SwitchStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(SynchronizedStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(ThrowStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(TryStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(TypeName p) throws ParseTreeException { + TypeName newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(UnaryExpression p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(Variable p) throws ParseTreeException { + Expression newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(VariableDeclaration p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(VariableDeclarator p) throws ParseTreeException { + VariableDeclarator newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + public void visit(WhileStatement p) throws ParseTreeException { + Statement newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + return; + } + p.childrenAccept(this); + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + +} diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/ExpansionApplier.java b/src/main/java/io/devnulllabs/openjava/ptree/util/ExpansionApplier.java new file mode 100644 index 0000000..811cc32 --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/ExpansionApplier.java @@ -0,0 +1,457 @@ +/* + * ExpansionApplier.java + * + * comments here. + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.ptree.util; + +import io.devnulllabs.openjava.mop.Environment; +import io.devnulllabs.openjava.mop.OJClass; +import io.devnulllabs.openjava.ptree.AllocationExpression; +import io.devnulllabs.openjava.ptree.ArrayAccess; +import io.devnulllabs.openjava.ptree.ArrayAllocationExpression; +import io.devnulllabs.openjava.ptree.AssignmentExpression; +import io.devnulllabs.openjava.ptree.BinaryExpression; +import io.devnulllabs.openjava.ptree.CastExpression; +import io.devnulllabs.openjava.ptree.ClassLiteral; +import io.devnulllabs.openjava.ptree.ConditionalExpression; +import io.devnulllabs.openjava.ptree.Expression; +import io.devnulllabs.openjava.ptree.FieldAccess; +import io.devnulllabs.openjava.ptree.InstanceofExpression; +import io.devnulllabs.openjava.ptree.Literal; +import io.devnulllabs.openjava.ptree.MethodCall; +import io.devnulllabs.openjava.ptree.ParseTreeException; +import io.devnulllabs.openjava.ptree.SelfAccess; +import io.devnulllabs.openjava.ptree.Statement; +import io.devnulllabs.openjava.ptree.TypeName; +import io.devnulllabs.openjava.ptree.UnaryExpression; +import io.devnulllabs.openjava.ptree.Variable; +import io.devnulllabs.openjava.ptree.VariableDeclaration; +import io.devnulllabs.openjava.tools.DebugOut; + +/** + * The class <code>ExpansionApplier</code> is an evaluator of each + * objects of <code>ParseTree</code> family. Each methods in + * this class is invoked from the class <code>EvaluationShuttle</code>. + * <p> + * The method <code>evaluateDown()</code> is invoked before evaluating + * the children of the parse tree object, and <code>evaluateUp()</code> + * is invoked after the evaluation. + * <p> + * For a class <code>P</code> and a object <code>p</code> statically + * typed as P, the parts in source code each expantion will be applied + * are: + * <ul> + * <li>Allocation <code>new P()</code> + * <li>ArrayAllocation <code>new P[expr]</code> + * <li>MethodCall <code>P.m()</code>, <code>p.m()</code> + * <li>FieldRead <code>P.f</code>, <code>p.f</code> as a right side value + * <li>FieldWrite <code>P.f = expr</code>, <code>p.f = expr</code> + * <li>ArrayAccess <code>ap[expr]</code> for <code>P[] ap;</code> + * <li>Expression <code>p</code> + * </ul> + * in feature version: + * <ul> + * <li>CastExpression <code>(P) expr</code> including implicit cast + * <li>CastedExpression <code>(Q) p</code> including implicit cast + * </ul> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: ExpansionApplier.java,v 1.2 2003/02/19 02:55:00 tatsubori Exp $ + * @see io.devnulllabs.openjava.ptree.ParseTree + * @see io.devnulllabs.openjava.ptree.util.EvaluationShuttle + */ +public class ExpansionApplier extends VariableBinder { + public ExpansionApplier(Environment env) { + super(env); + } + + private OJClass getType(Expression p) throws ParseTreeException { + OJClass result = null; + try { + result = p.getType(getEnvironment()); + } catch (Exception e) { + e.printStackTrace(); + throw new ParseTreeException(e); + } + DebugOut.println("type eval - " + p + "\t: " + result); + if (result == null) { + System.err.println("cannot resolve the type of expression"); + System.err.println(p.getClass() + " : " + p); + System.err.println(getEnvironment()); + /*****DebugOut.println(getEnvironment().toString());*/ + if (p instanceof ArrayAccess) { + ArrayAccess aaexpr = (ArrayAccess) p; + Expression refexpr = aaexpr.getReferenceExpr(); + OJClass refexprtype = null; + OJClass comptype = null; + try { + refexprtype = refexpr.getType(getEnvironment()); + comptype = refexprtype.getComponentType(); + } catch (Exception ex) { + } + System.err.println( + refexpr + " : " + refexprtype + " : " + comptype); + } + } + return result; + } + + private OJClass getSelfType() throws ParseTreeException { + OJClass result; + try { + Environment env = getEnvironment(); + String selfname = env.currentClassName(); + result = env.lookupClass(selfname); + } catch (Exception ex) { + throw new ParseTreeException(ex); + } + return result; + } + + private OJClass getType(TypeName typename) throws ParseTreeException { + OJClass result = null; + try { + Environment env = getEnvironment(); + String qname = env.toQualifiedName(typename.toString()); + result = env.lookupClass(qname); + } catch (Exception ex) { + throw new ParseTreeException(ex); + } + DebugOut.println("type eval - class access : " + result); + if (result == null) { + System.err.println("unknown type for a type name : " + typename); + } + return result; + } + + private OJClass computeRefType(TypeName typename, Expression expr) + throws ParseTreeException { + if (typename != null) + return getType(typename); + if (expr != null) + return getType(expr); + return getSelfType(); + } + + public void visit(AssignmentExpression p) throws ParseTreeException { + Expression left = p.getLeft(); + if (!(left instanceof FieldAccess)) { + super.visit(p); + return; + } + FieldAccess fldac = (FieldAccess) left; + Expression refexpr = fldac.getReferenceExpr(); + TypeName reftype = fldac.getReferenceType(); + Expression value = p.getRight(); + /* custom version of visit() skipping the field */ + Expression newp; + newp = this.evaluateDown(p); + if (newp != p) { + p.replace(newp); + newp.accept(this); + return; + } + + if (refexpr != null) { + refexpr.accept(this); + } else if (reftype != null) { + reftype.accept(this); + } + value.accept(this); + + newp = this.evaluateUp(p); + if (newp != p) + p.replace(newp); + } + + /** + * Includes expandAllocation() and expandExpression(). + */ + public Expression evaluateUp(AllocationExpression p) + throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandAllocation(getEnvironment(), p); + if (newp != p) + return newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandArrayAccess() and expandExpression(). + */ + public Expression evaluateUp(ArrayAccess p) throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandArrayAccess(getEnvironment(), p); + if (newp != p) + return newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandArrayAllocation() and expandExpression(). + */ + public Expression evaluateUp(ArrayAllocationExpression p) + throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandArrayAllocation(getEnvironment(), p); + if (newp != p) + return newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandFieldWrite(), expandAssignmentExpression() + * and expandExpression(). + */ + public Expression evaluateUp(AssignmentExpression p) + throws ParseTreeException { + Expression left = p.getLeft(); + if (left instanceof FieldAccess) { + FieldAccess fldac = (FieldAccess) left; + OJClass reftype = + computeRefType( + fldac.getReferenceType(), + fldac.getReferenceExpr()); + if (reftype != getSelfType()) { + Expression newp = reftype.expandFieldWrite(getEnvironment(), p); + if (!(newp instanceof AssignmentExpression)) + return newp; + p = (AssignmentExpression) newp; + } + } + + OJClass type = getType(p); + if (type != getSelfType()) { + Expression newp = + type.expandAssignmentExpression(getEnvironment(), p); + if (!(newp instanceof AssignmentExpression)) + return newp; + p = (AssignmentExpression) newp; + type = getType(p); + } + if (type != getSelfType()) { + Expression newp = type.expandExpression(getEnvironment(), p); + if (!(newp instanceof AssignmentExpression)) + return newp; + p = (AssignmentExpression) newp; + } + return super.evaluateUp(p); + } + + /** + * Includes expandExpression(). + */ + public Expression evaluateUp(BinaryExpression p) + throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandCastExpression(), expandCastedExpression() and + * expandExpression(). + */ + public Expression evaluateUp(CastExpression p) throws ParseTreeException { + OJClass type = getType(p); + OJClass orgType = getType(p.getExpression()); + Expression newp; + newp = orgType.expandCastedExpression(getEnvironment(), p); + if (newp != p) + return newp; + newp = type.expandCastExpression(getEnvironment(), p); + if (newp != p) + return newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandExpression(). + */ + public Expression evaluateUp(ClassLiteral p) throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandExpression(). + */ + public Expression evaluateUp(ConditionalExpression p) + throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandFieldRead() and expandExpression(). + * Not to be applied for itself. + */ + public Expression evaluateUp(FieldAccess p) throws ParseTreeException { + { + OJClass reftype = + computeRefType(p.getReferenceType(), p.getReferenceExpr()); + if (reftype != getSelfType()) { + Expression newp = reftype.expandFieldRead(getEnvironment(), p); + if (newp != p) + return newp; + } + } + { + OJClass type = getType(p); + Expression newp = type.expandExpression(getEnvironment(), p); + if (!(newp instanceof FieldAccess)) + return newp; + p = (FieldAccess) newp; + } + return super.evaluateUp(p); + } + + /** + * Includes expandExpression(). + */ + public Expression evaluateUp(InstanceofExpression p) + throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandExpression(). + */ + public Expression evaluateUp(Literal p) throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandMethodCall() and expandExpression(). + */ + public Expression evaluateUp(MethodCall p) throws ParseTreeException { + { + OJClass reftype = + computeRefType(p.getReferenceType(), p.getReferenceExpr()); + if (reftype != getSelfType()) { + Expression newp = reftype.expandMethodCall(getEnvironment(), p); + if (newp != p) + return newp; + } + } + { + OJClass type = getType(p); + Expression newp = type.expandExpression(getEnvironment(), p); + if (!(newp instanceof MethodCall)) + return newp; + p = (MethodCall) newp; + } + return super.evaluateUp(p); + } + + /** + * Includes expandExpression(). + */ + public Expression evaluateUp(SelfAccess p) throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandTypeName(). + */ + public TypeName evaluateUp(TypeName p) throws ParseTreeException { + OJClass type = getType(p); + TypeName newp; + newp = type.expandTypeName(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandExpression(). + */ + public Expression evaluateUp(UnaryExpression p) throws ParseTreeException { + OJClass type = getType(p); + Expression newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandExpression(). + */ + public Expression evaluateUp(Variable p) throws ParseTreeException { + OJClass type = getType(p); + + /* special ignorance for variable ? */ + if (type == null) + return p; + + Expression newp; + newp = type.expandExpression(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + + /** + * Includes expandVariableDeclaration(). + */ + public Statement evaluateUp(VariableDeclaration p) + throws ParseTreeException { + OJClass type = getType(p.getTypeSpecifier()); + Statement newp; + newp = type.expandVariableDeclaration(getEnvironment(), p); + if (newp != p) + return newp; + return super.evaluateUp(p); + } + +} diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/MemberAccessCorrector.java b/src/main/java/io/devnulllabs/openjava/ptree/util/MemberAccessCorrector.java new file mode 100644 index 0000000..8c1a2d8 --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/MemberAccessCorrector.java @@ -0,0 +1,222 @@ +/* + * MemberAccessCorrector.java + * + * Firstly, the parser generates a temporal parse tree. + * This class correct them. + * <p> + * + * All the continuous field access are stored in a single Variable ptree + * object. + * [p.p.f.f].f [p.f].m() ([] a single Variable object) + * FieldAccess := Variable name + * MemberAccess := Variable name "(" .. ")" + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.ptree.util; + +import io.devnulllabs.openjava.mop.Environment; +import io.devnulllabs.openjava.mop.NoSuchMemberException; +import io.devnulllabs.openjava.mop.OJClass; +import io.devnulllabs.openjava.mop.OJClassNotFoundException; +import io.devnulllabs.openjava.mop.OJField; +import io.devnulllabs.openjava.ptree.Expression; +import io.devnulllabs.openjava.ptree.FieldAccess; +import io.devnulllabs.openjava.ptree.MethodCall; +import io.devnulllabs.openjava.ptree.ParseTreeException; +import io.devnulllabs.openjava.ptree.TypeName; +import io.devnulllabs.openjava.ptree.Variable; +import io.devnulllabs.openjava.tools.DebugOut; + +/** + * The class <code>MemberAccessCorrector</code> + * <p> + * For example + * <pre> + * </pre> + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: MemberAccessCorrector.java,v 1.2 2003/02/19 02:55:00 tatsubori Exp $ + * @see java.lang.Object + */ +public class MemberAccessCorrector extends VariableBinder { + + private String errorState = null; + + public MemberAccessCorrector(Environment env) { + super(env); + } + + public String getErrorState() { + return errorState; + } + + public Expression evaluateDown(FieldAccess ptree) + throws ParseTreeException { + super.evaluateDown(ptree); + + if (ptree.getReferenceType() != null) + return ptree; + + Expression ref = ptree.getReferenceExpr(); + String field_name = ptree.getName(); + + if (ref == null) { + if (isVariable(field_name)) { + /* this is a variable. */ + DebugOut.println("MC variable - " + field_name); + return new Variable(field_name); + } else if (isField(field_name)) { + /* this is a field access */ + DebugOut.println("MC field access - " + field_name); + } else { + /* unknown variable or field */ + System.err.println("unknown field or variable : " + field_name); + System.err.println(getEnvironment()); + } + } else if (ref instanceof Variable) { + FieldAccess fa = name2fieldaccess(ref.toString(), field_name); + TypeName typename = fa.getReferenceType(); + Expression refexpr = fa.getReferenceExpr(); + if (typename != null) { + ptree.setReferenceType(typename); + } else { + ptree.setReferenceExpr(refexpr); + } + } + + return ptree; + } + + public Expression evaluateDown(MethodCall ptree) + throws ParseTreeException { + super.evaluateDown(ptree); + + if (ptree.getReferenceType() != null) + return ptree; + + Expression ref = ptree.getReferenceExpr(); + if (ref == null || !(ref instanceof Variable)) + return ptree; + String method_name = ptree.getName(); + + if (ref instanceof Variable) { + FieldAccess fa = name2fieldaccess(ref.toString(), method_name); + TypeName typename = fa.getReferenceType(); + Expression refexpr = fa.getReferenceExpr(); + if (typename != null) { + ptree.setReferenceType(typename); + } else { + ptree.setReferenceExpr(refexpr); + } + } + return ptree; + } + + private FieldAccess name2fieldaccess(String names, String field) { + Expression result_expr; + String first = getFirst(names); + String rest = getRest(names); + + if (isVariable(first)) { + /* this is a variable */ + DebugOut.println("MC variable - " + first); + result_expr = new Variable(first); + } else if (isField(first)) { + /* this is a field */ + DebugOut.println("MC field - " + first); + result_expr = new FieldAccess((Variable) null, first); + } else { + /* this is a class */ + while (rest != null && !isClass(first)) { + first = first + "." + getFirst(rest); + rest = getRest(rest); + } + while (isClass(first + "." + getFirst(rest))) { + first = first + "." + getFirst(rest); + rest = getRest(rest); + } + if (isClass(first)) { + DebugOut.println("MC class - " + first); + } else { + System.err.println("unknown class : " + first); + } + + TypeName type = new TypeName(first); + if (rest == null) { + /* ref is a typename */ + return new FieldAccess(type, field); + } + first = getFirst(rest); + rest = getRest(rest); + result_expr = new FieldAccess(type, first); + } + + /* remainder is field access */ + while (rest != null) { + first = getFirst(rest); + rest = getRest(rest); + result_expr = new FieldAccess(result_expr, first); + } + + return new FieldAccess(result_expr, field); + } + + private boolean isVariable(String name) { + Environment env = getEnvironment(); + OJClass bindedtype = env.lookupBind(name); + return (bindedtype != null); + } + + private boolean isField(String name) { + Environment env = getEnvironment(); + String qcname = env.toQualifiedName(env.currentClassName()); + OJClass declarer = env.lookupClass(qcname); + OJField field = null; + while (declarer != null && field == null) { + try { + field = declarer.getField(name, declarer); + } catch (NoSuchMemberException e) { + } + declarer = declarer.getDeclaringClass(); + } + return (field != null); + } + + private boolean isClass(String name) { + Environment env = getEnvironment(); + String qname = env.toQualifiedName(name); + try { + OJClass.forName(qname); + return true; + } catch (OJClassNotFoundException e) { + } + OJClass clazz = env.lookupClass(qname); + return (clazz != null); + } + + private static final String getFirst(String qname) { + if (qname == null) + return null; + int dot = qname.indexOf('.'); + if (dot == -1) + return qname; + return qname.substring(0, dot); + } + + private static final String getRest(String qname) { + if (qname == null) + return null; + int dot = qname.indexOf('.'); + if (dot == -1) + return null; + return qname.substring(dot + 1); + } + +} diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/ParseTreeVisitor.java b/src/main/java/io/devnulllabs/openjava/ptree/util/ParseTreeVisitor.java new file mode 100644 index 0000000..88a609f --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/ParseTreeVisitor.java @@ -0,0 +1,191 @@ +/* + * ParseTreeVisitor.java + * + * comments here. + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.ptree.util; + +import io.devnulllabs.openjava.ptree.AllocationExpression; +import io.devnulllabs.openjava.ptree.ArrayAccess; +import io.devnulllabs.openjava.ptree.ArrayAllocationExpression; +import io.devnulllabs.openjava.ptree.ArrayInitializer; +import io.devnulllabs.openjava.ptree.AssignmentExpression; +import io.devnulllabs.openjava.ptree.BinaryExpression; +import io.devnulllabs.openjava.ptree.Block; +import io.devnulllabs.openjava.ptree.BreakStatement; +import io.devnulllabs.openjava.ptree.CaseGroup; +import io.devnulllabs.openjava.ptree.CaseGroupList; +import io.devnulllabs.openjava.ptree.CaseLabel; +import io.devnulllabs.openjava.ptree.CaseLabelList; +import io.devnulllabs.openjava.ptree.CastExpression; +import io.devnulllabs.openjava.ptree.CatchBlock; +import io.devnulllabs.openjava.ptree.CatchList; +import io.devnulllabs.openjava.ptree.ClassDeclaration; +import io.devnulllabs.openjava.ptree.ClassDeclarationList; +import io.devnulllabs.openjava.ptree.ClassLiteral; +import io.devnulllabs.openjava.ptree.CompilationUnit; +import io.devnulllabs.openjava.ptree.ConditionalExpression; +import io.devnulllabs.openjava.ptree.ConstructorDeclaration; +import io.devnulllabs.openjava.ptree.ConstructorInvocation; +import io.devnulllabs.openjava.ptree.ContinueStatement; +import io.devnulllabs.openjava.ptree.DoWhileStatement; +import io.devnulllabs.openjava.ptree.EmptyStatement; +import io.devnulllabs.openjava.ptree.Expression; +import io.devnulllabs.openjava.ptree.ExpressionList; +import io.devnulllabs.openjava.ptree.ExpressionStatement; +import io.devnulllabs.openjava.ptree.FieldAccess; +import io.devnulllabs.openjava.ptree.FieldDeclaration; +import io.devnulllabs.openjava.ptree.ForStatement; +import io.devnulllabs.openjava.ptree.IfStatement; +import io.devnulllabs.openjava.ptree.InstanceofExpression; +import io.devnulllabs.openjava.ptree.LabeledStatement; +import io.devnulllabs.openjava.ptree.Leaf; +import io.devnulllabs.openjava.ptree.List; +import io.devnulllabs.openjava.ptree.Literal; +import io.devnulllabs.openjava.ptree.MemberDeclaration; +import io.devnulllabs.openjava.ptree.MemberDeclarationList; +import io.devnulllabs.openjava.ptree.MemberInitializer; +import io.devnulllabs.openjava.ptree.MethodCall; +import io.devnulllabs.openjava.ptree.MethodDeclaration; +import io.devnulllabs.openjava.ptree.ModifierList; +import io.devnulllabs.openjava.ptree.NonLeaf; +import io.devnulllabs.openjava.ptree.Parameter; +import io.devnulllabs.openjava.ptree.ParameterList; +import io.devnulllabs.openjava.ptree.ParseTree; +import io.devnulllabs.openjava.ptree.ParseTreeException; +import io.devnulllabs.openjava.ptree.ParseTreeObject; +import io.devnulllabs.openjava.ptree.ReturnStatement; +import io.devnulllabs.openjava.ptree.SelfAccess; +import io.devnulllabs.openjava.ptree.Statement; +import io.devnulllabs.openjava.ptree.StatementList; +import io.devnulllabs.openjava.ptree.SwitchStatement; +import io.devnulllabs.openjava.ptree.SynchronizedStatement; +import io.devnulllabs.openjava.ptree.ThrowStatement; +import io.devnulllabs.openjava.ptree.TryStatement; +import io.devnulllabs.openjava.ptree.TypeName; +import io.devnulllabs.openjava.ptree.UnaryExpression; +import io.devnulllabs.openjava.ptree.Variable; +import io.devnulllabs.openjava.ptree.VariableDeclaration; +import io.devnulllabs.openjava.ptree.VariableDeclarator; +import io.devnulllabs.openjava.ptree.VariableInitializer; +import io.devnulllabs.openjava.ptree.WhileStatement; + +/** + * The class <code>ParseTreeVisitor</code> is a Visitor role + * in the Visitor pattern and visits <code>ParseTree</code> objects + * as the role of Element. + * <p> + * For example + * <pre> + * </pre> + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: ParseTreeVisitor.java,v 1.2 2003/02/19 02:55:00 tatsubori Exp $ + * @see io.devnulllabs.openjava.ptree.ParseTree + */ +public abstract class ParseTreeVisitor { + + public void visit(ParseTree p) throws ParseTreeException { + p.accept(this); + } + public void visit(ParseTreeObject p) throws ParseTreeException { + p.accept(this); + } + public void visit(NonLeaf p) throws ParseTreeException { + p.accept(this); + } + public void visit(Leaf p) throws ParseTreeException { + p.accept(this); + } + public void visit(MemberDeclaration p) throws ParseTreeException { + p.accept(this); + } + public void visit(Statement p) throws ParseTreeException { + p.accept(this); + } + public void visit(Expression p) throws ParseTreeException { + p.accept(this); + } + public void visit(VariableInitializer p) throws ParseTreeException { + p.accept(this); + } + public void visit(List p) throws ParseTreeException { + p.accept(this); + } + + public abstract void visit(AllocationExpression p) + throws ParseTreeException; + public abstract void visit(ArrayAccess p) throws ParseTreeException; + public abstract void visit(ArrayAllocationExpression p) + throws ParseTreeException; + public abstract void visit(ArrayInitializer p) throws ParseTreeException; + public abstract void visit(AssignmentExpression p) + throws ParseTreeException; + public abstract void visit(BinaryExpression p) throws ParseTreeException; + public abstract void visit(Block p) throws ParseTreeException; + public abstract void visit(BreakStatement p) throws ParseTreeException; + public abstract void visit(CaseGroup p) throws ParseTreeException; + public abstract void visit(CaseGroupList p) throws ParseTreeException; + public abstract void visit(CaseLabel p) throws ParseTreeException; + public abstract void visit(CaseLabelList p) throws ParseTreeException; + public abstract void visit(CastExpression p) throws ParseTreeException; + public abstract void visit(CatchBlock p) throws ParseTreeException; + public abstract void visit(CatchList p) throws ParseTreeException; + public abstract void visit(ClassDeclaration p) throws ParseTreeException; + public abstract void visit(ClassDeclarationList p) + throws ParseTreeException; + public abstract void visit(ClassLiteral p) throws ParseTreeException; + public abstract void visit(CompilationUnit p) throws ParseTreeException; + public abstract void visit(ConditionalExpression p) + throws ParseTreeException; + public abstract void visit(ConstructorDeclaration p) + throws ParseTreeException; + public abstract void visit(ConstructorInvocation p) + throws ParseTreeException; + public abstract void visit(ContinueStatement p) throws ParseTreeException; + public abstract void visit(DoWhileStatement p) throws ParseTreeException; + public abstract void visit(EmptyStatement p) throws ParseTreeException; + public abstract void visit(ExpressionList p) throws ParseTreeException; + public abstract void visit(ExpressionStatement p) + throws ParseTreeException; + public abstract void visit(FieldAccess p) throws ParseTreeException; + public abstract void visit(FieldDeclaration p) throws ParseTreeException; + public abstract void visit(ForStatement p) throws ParseTreeException; + public abstract void visit(IfStatement p) throws ParseTreeException; + public abstract void visit(InstanceofExpression p) + throws ParseTreeException; + public abstract void visit(LabeledStatement p) throws ParseTreeException; + public abstract void visit(Literal p) throws ParseTreeException; + public abstract void visit(MemberDeclarationList p) + throws ParseTreeException; + public abstract void visit(MemberInitializer p) throws ParseTreeException; + public abstract void visit(MethodCall p) throws ParseTreeException; + public abstract void visit(MethodDeclaration p) throws ParseTreeException; + public abstract void visit(ModifierList p) throws ParseTreeException; + public abstract void visit(Parameter p) throws ParseTreeException; + public abstract void visit(ParameterList p) throws ParseTreeException; + public abstract void visit(ReturnStatement p) throws ParseTreeException; + public abstract void visit(SelfAccess p) throws ParseTreeException; + public abstract void visit(StatementList p) throws ParseTreeException; + public abstract void visit(SwitchStatement p) throws ParseTreeException; + public abstract void visit(SynchronizedStatement p) + throws ParseTreeException; + public abstract void visit(ThrowStatement p) throws ParseTreeException; + public abstract void visit(TryStatement p) throws ParseTreeException; + public abstract void visit(TypeName p) throws ParseTreeException; + public abstract void visit(UnaryExpression p) throws ParseTreeException; + public abstract void visit(Variable p) throws ParseTreeException; + public abstract void visit(VariableDeclaration p) + throws ParseTreeException; + public abstract void visit(VariableDeclarator p) throws ParseTreeException; + public abstract void visit(WhileStatement p) throws ParseTreeException; + +} diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/PartialParser.java b/src/main/java/io/devnulllabs/openjava/ptree/util/PartialParser.java new file mode 100644 index 0000000..c6b2b28 --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/PartialParser.java @@ -0,0 +1,330 @@ +/* + * PartialParser.java 1.0 + * + * This class can be used to make ptree objects from string. + * + * Jun 11, 1997 mich + * Oct 17, 1997 mich + * + * @see io.devnulllabs.openjava.ptree.ParseTree + * @see io.devnulllabs.openjava.ptree.ParseTreeObject + * @version 1.0 last updated: Oct 17, 1997 + * @author Michiaki Tatsubori + */ +package io.devnulllabs.openjava.ptree.util; + +import java.io.StringReader; + +import io.devnulllabs.openjava.mop.ClosedEnvironment; +import io.devnulllabs.openjava.mop.Environment; +import io.devnulllabs.openjava.mop.MOPException; +import io.devnulllabs.openjava.ptree.Expression; +import io.devnulllabs.openjava.ptree.MemberDeclaration; +import io.devnulllabs.openjava.ptree.MemberDeclarationList; +import io.devnulllabs.openjava.ptree.ObjectList; +import io.devnulllabs.openjava.ptree.ParseTree; +import io.devnulllabs.openjava.ptree.ParseTreeException; +import io.devnulllabs.openjava.ptree.Statement; +import io.devnulllabs.openjava.ptree.StatementList; +import io.devnulllabs.openjava.tools.DebugOut; +import io.devnulllabs.openjava.tools.parser.Parser; + +/** + * The <code>PartialParser</code> class is + * an utilty class to make ptree objects from string. + * + * @see io.devnulllabs.openjava.ptree.Ptree + * @see io.devnulllabs.openjava.ptree.Expression + * @see io.devnulllabs.openjava.ptree.Statement + * @see io.devnulllabs.openjava.ptree.StatementList + * @see io.devnulllabs.openjava.ptree.MemberDeclaration + * @see io.devnulllabs.openjava.ptree.MemberDeclarationList + * @see io.devnulllabs.openjava.ptree.TypeDeclaration + * @see io.devnulllabs.openjava.ptree.CompilationUnit + */ +public final class PartialParser { + /** + * Constructor should not be called. + * + */ + protected PartialParser() { + } + + private static ParseTree initialize(Environment env, ParseTree p) + throws ParseTreeException { + /* dummy wrapper in case replacement occurs */ + ObjectList holder = new ObjectList(p); + p.accept(new MemberAccessCorrector(env)); + return (ParseTree) holder.get(0); + } + + /** + * Replaces "#s" "#EXPR" "#STMT" "#STMTS" in the given string with + * the given arguments. + * <p> + * "##" is replaced with "#". + * <p> + * <br><blockquote><pre> + * #s arg.toString() + * #EXPR ((Expression) arg).toString() + * #STMT ((Statement) arg).toString() + * #STMTS ((StatementList) arg).toString() + * </pre></blockquote><br> + * + */ + public static final String replace(String base, Object[] args) + throws MOPException { + try { + StringBuffer result = new StringBuffer(); + + int arg_i = 0, index = 0, found; + while ((found = base.indexOf('#', index)) != -1) { + result.append(base.substring(index, found)); + if (base.regionMatches(found, "#STMTS", 0, 6)) { + result.append((StatementList) args[arg_i++]); + index = found + 6; + } else if (base.regionMatches(found, "#STMT", 0, 5)) { + result.append((Statement) args[arg_i++]); + index = found + 5; + } else if (base.regionMatches(found, "#EXPR", 0, 5)) { + result.append((Expression) args[arg_i++]); + index = found + 5; + } else if (base.regionMatches(found, "#s", 0, 2)) { + result.append(args[arg_i++].toString()); + index = found + 2; + } else if (base.regionMatches(found, "##", 0, 2)) { + result.append('#'); + index = found + 2; + } else { + result.append('#'); + index = found + 1; + } + } + result.append(base.substring(index)); + + return result.toString(); + } catch (Exception e) { + /* special exception is better */ + throw new MOPException( + "PartialParser.replace() : " + + "illegal format for arguments : " + + base); + } + } + + public static final String replace(String base, Object a0) + throws MOPException { + return replace(base, new Object[] { a0 }); + } + + public static final String replace(String base, Object a0, Object a1) + throws MOPException { + return replace(base, new Object[] { a0, a1 }); + } + + public static final String replace( + String base, + Object a0, + Object a1, + Object a2) + throws MOPException { + return replace(base, new Object[] { a0, a1, a2 }); + } + + public static final String replace( + String base, + Object a0, + Object a1, + Object a2, + Object a3) + throws MOPException { + return replace(base, new Object[] { a0, a1, a2, a3 }); + } + + public static final String replace( + String base, + Object a0, + Object a1, + Object a2, + Object a3, + Object a4) + throws MOPException { + return replace(base, new Object[] { a0, a1, a2, a3, a4 }); + } + + public static final String replace( + String base, + Object a0, + Object a1, + Object a2, + Object a3, + Object a4, + Object a5) + throws MOPException { + return replace(base, new Object[] { a0, a1, a2, a3, a4, a5 }); + } + + public static final String replace( + String base, + Object a0, + Object a1, + Object a2, + Object a3, + Object a4, + Object a5, + Object a6) + throws MOPException { + return replace(base, new Object[] { a0, a1, a2, a3, a4, a5, a6 }); + } + + public static final String replace( + String base, + Object a0, + Object a1, + Object a2, + Object a3, + Object a4, + Object a5, + Object a6, + Object a7) + throws MOPException { + return replace(base, new Object[] { a0, a1, a2, a3, a4, a5, a6, a7 }); + } + + /** + * Makes a ptree node from the string like : + * <br><blockquote><pre> + * "i + 3" + * </pre></blockquote><br> + * or : + * <br><blockquote><pre> + * "f()" + * </pre></blockquote><br> + * + * @return the expression node which the specified string + * represents. + * @exception MOPException if any critical error occurs. + */ + public static Expression makeExpression(Environment env, String str) + throws MOPException { + DebugOut.println("PP makeExpression() : " + str); + Parser parser = new Parser(new StringReader(str)); + Expression result; + try { + result = parser.Expression(env); + result = (Expression) initialize(env, result); + } catch (Exception e) { + System.err.println("partial parsing failed for : " + str); + System.err.println(e); + System.err.println(env.toString()); + throw new MOPException(e); + } + return result; + } + + /** + * Makes a ptree node from the string like : + * <br><blockquote><pre> + * "i++;" + * </pre></blockquote><br> + * or : + * <br><blockquote><pre> + * "for(;;){ f(); }" + * </pre></blockquote><br> + * <p> + * But local variable declarations are not allowed. + * + * @return the statement node which the specified string + * represents. + * @exception MOPException if any critical error occurs. + */ + public static Statement makeStatement(Environment env, String str) + throws MOPException { + DebugOut.println("PP makeStatement() : " + str); + Parser parser = new Parser(new StringReader(str)); + Statement result; + try { + result = parser.Statement(env); + result = (Statement) initialize(env, result); + } catch (Exception e) { + System.err.println("partial parsing failed for : " + str); + System.err.println(e); + System.err.println(env.toString()); + throw new MOPException(e); + } + return result; + } + + /** + * Makes ptree node from the string like : + * <br><blockquote><pre> + * "i++; j = 3;" + * </pre></blockquote><br> + * <p> + * Local variable declarations like following can also be parsed. + * <br><blockquote><pre> + * "int n, m;" + * </pre></blockquote><br> + * + * @return the statements node which the specified string + * represents. + * @exception MOPException if any critical error occurs. + */ + public static StatementList makeStatementList(Environment env, String str) + throws MOPException { + DebugOut.println("PP makeStatementList() : " + str); + Parser parser = new Parser(new StringReader(str)); + env = new ClosedEnvironment(env); + StatementList result; + try { + result = parser.BlockOrStatementListOpt(env); + result = (StatementList) initialize(env, result); + } catch (Exception e) { + System.err.println("partial parsing failed for : " + str); + System.err.println(e); + System.err.println(env.toString()); + throw new MOPException(e); + } + return result; + } + + /** + * NOT IMPLEMENTED + * Makes ptree node from the string like : + * <br><blockquote><pre> + * "int f(){ return 1; }" + * </pre></blockquote><br> + * or : + * <br><blockquote><pre> + * "public String str;" + * </pre></blockquote><br> + * + * @return the statements node which the specified string + * represents. + * @exception MOPException if any critical error occurs. + */ + public static MemberDeclaration makeMemberDeclaration(String str) + throws MOPException { + MemberDeclaration ret = null; + return ret; + } + + /** + * NOT IMPLEMENTED + * Makes a ptree node from the string like : + * <br><blockquote><pre> + * "int f(){ return 1; }" + + * "public String str;" + * </pre></blockquote><br> + * + * @return the type declarations list node which the specified string + * represents. + * @exception MOPException if any critical error occurs. + */ + public static MemberDeclarationList makeMemberDeclarationList(String str) + throws MOPException { + MemberDeclarationList ret = null; + return ret; + } + +} diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/ScopeHandler.java b/src/main/java/io/devnulllabs/openjava/ptree/util/ScopeHandler.java new file mode 100644 index 0000000..0afb937 --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/ScopeHandler.java @@ -0,0 +1,305 @@ +/* + * ScopeHandler.java + * + * comments here. + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.ptree.util; + +import java.util.Stack; + +import io.devnulllabs.openjava.mop.AnonymousClassEnvironment; +import io.devnulllabs.openjava.mop.ClassEnvironment; +import io.devnulllabs.openjava.mop.ClosedEnvironment; +import io.devnulllabs.openjava.mop.Environment; +import io.devnulllabs.openjava.mop.FileEnvironment; +import io.devnulllabs.openjava.mop.OJClass; +import io.devnulllabs.openjava.ptree.AllocationExpression; +import io.devnulllabs.openjava.ptree.Block; +import io.devnulllabs.openjava.ptree.ClassDeclaration; +import io.devnulllabs.openjava.ptree.CompilationUnit; +import io.devnulllabs.openjava.ptree.ConstructorDeclaration; +import io.devnulllabs.openjava.ptree.DoWhileStatement; +import io.devnulllabs.openjava.ptree.Expression; +import io.devnulllabs.openjava.ptree.ForStatement; +import io.devnulllabs.openjava.ptree.IfStatement; +import io.devnulllabs.openjava.ptree.MemberDeclaration; +import io.devnulllabs.openjava.ptree.MemberDeclarationList; +import io.devnulllabs.openjava.ptree.MemberInitializer; +import io.devnulllabs.openjava.ptree.MethodDeclaration; +import io.devnulllabs.openjava.ptree.ParseTreeException; +import io.devnulllabs.openjava.ptree.Statement; +import io.devnulllabs.openjava.ptree.SwitchStatement; +import io.devnulllabs.openjava.ptree.SynchronizedStatement; +import io.devnulllabs.openjava.ptree.TryStatement; +import io.devnulllabs.openjava.ptree.WhileStatement; + +/** + * The class <code>ScopeHandler</code> + * <p> + * For example + * <pre> + * </pre> + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: ScopeHandler.java,v 1.2 2003/02/19 02:55:00 tatsubori Exp $ + * @see io.devnulllabs.openjava.ptree.ParseTree + * @see io.devnulllabs.openjava.ptree.util.ParseTreeVisitor + * @see io.devnulllabs.openjava.ptree.util.EvaluationShuttle + */ +public abstract class ScopeHandler extends EvaluationShuttle { + private Stack env_nest = new Stack(); + + public ScopeHandler(Environment base_env) { + super(base_env); + } + + protected final void pushClosedEnvironment() { + push(new ClosedEnvironment(getEnvironment())); + } + + protected final void push(Environment env) { + env_nest.push(getEnvironment()); + setEnvironment(env); + } + + protected final void pop() { + setEnvironment((Environment) env_nest.pop()); + } + + /* in walking down through parse tree */ + + /* compilation unit */ + public CompilationUnit evaluateDown(CompilationUnit ptree) + throws ParseTreeException { + ClassDeclaration pubclazz = ptree.getPublicClass(); + String name = + (pubclazz != null) ? pubclazz.getName() : "<no public class>"; + FileEnvironment fenv = + new FileEnvironment(getEnvironment(), ptree, name); + + push(fenv); + + return ptree; + } + + /* class declaration */ + public ClassDeclaration evaluateDown(ClassDeclaration ptree) + throws ParseTreeException { + /* records this class */ + if (getEnvironment() instanceof ClosedEnvironment) { + recordLocalClass(ptree); + } + + /* creates a new class environment */ + ClassEnvironment env = + new ClassEnvironment(getEnvironment(), ptree.getName()); + MemberDeclarationList mdecls = ptree.getBody(); + for (int i = 0; i < mdecls.size(); ++i) { + MemberDeclaration m = mdecls.get(i); + if (!(m instanceof ClassDeclaration)) + continue; + ClassDeclaration inner = (ClassDeclaration) m; + env.recordMemberClass(inner.getName()); + } + push(env); + + return ptree; + } + + private void recordLocalClass(ClassDeclaration ptree) { + String classname = ptree.getName(); + Environment outer_env = getEnvironment(); + String qname = outer_env.toQualifiedName(classname); + if (outer_env.lookupClass(qname) != null) + return; + try { + OJClass out_clazz = + outer_env.lookupClass(outer_env.currentClassName()); + /***** this will be recorded in global env */ + //OJClass clazz = OJClass.forParseTree(outer_env, out_clazz, ptree); + OJClass clazz = new OJClass(outer_env, out_clazz, ptree); + outer_env.record(classname, clazz); + } catch (Exception ex) { + System.err.println("unknown error: " + ex); + return; + } + } + + /* class body contents */ + public MemberDeclaration evaluateDown(MethodDeclaration ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public MemberDeclaration evaluateDown(ConstructorDeclaration ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public MemberDeclaration evaluateDown(MemberInitializer ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + /* statements */ + public Statement evaluateDown(Block ptree) throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public Statement evaluateDown(SwitchStatement ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public Statement evaluateDown(IfStatement ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public Statement evaluateDown(WhileStatement ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public Statement evaluateDown(DoWhileStatement ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public Statement evaluateDown(ForStatement ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public Statement evaluateDown(TryStatement ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public Statement evaluateDown(SynchronizedStatement ptree) + throws ParseTreeException { + pushClosedEnvironment(); + return ptree; + } + + public Expression evaluateDown(AllocationExpression ptree) + throws ParseTreeException { + MemberDeclarationList cbody = ptree.getClassBody(); + if (cbody != null) { + String baseName = ptree.getClassType().toString(); + push( + new AnonymousClassEnvironment( + getEnvironment(), + baseName, + cbody)); + } else { + pushClosedEnvironment(); + } + return ptree; + } + + /* in walking down through parse tree */ + + /* class declaration */ + public CompilationUnit evaluateUp(CompilationUnit ptree) + throws ParseTreeException { + pop(); + return ptree; + } + + /* class declaration */ + public ClassDeclaration evaluateUp(ClassDeclaration ptree) + throws ParseTreeException { + pop(); + return ptree; + } + + /* class body contents */ + public MemberDeclaration evaluateUp(MethodDeclaration ptree) + throws ParseTreeException { + pop(); + return ptree; + } + + public MemberDeclaration evaluateUp(ConstructorDeclaration ptree) + throws ParseTreeException { + pop(); + return ptree; + } + + public MemberDeclaration evaluateUp(MemberInitializer ptree) + throws ParseTreeException { + pop(); + return ptree; + } + + /* statements */ + public Statement evaluateUp(Block ptree) throws ParseTreeException { + pop(); + return ptree; + } + + public Statement evaluateUp(SwitchStatement ptree) + throws ParseTreeException { + pop(); + return ptree; + } + + public Statement evaluateUp(IfStatement ptree) throws ParseTreeException { + pop(); + return ptree; + } + + public Statement evaluateUp(WhileStatement ptree) + throws ParseTreeException { + pop(); + return ptree; + } + + public Statement evaluateUp(DoWhileStatement ptree) + throws ParseTreeException { + pop(); + return ptree; + } + + public Statement evaluateUp(ForStatement ptree) throws ParseTreeException { + pop(); + return ptree; + } + + public Statement evaluateUp(TryStatement ptree) throws ParseTreeException { + pop(); + return ptree; + } + + public Statement evaluateUp(SynchronizedStatement ptree) + throws ParseTreeException { + pop(); + return ptree; + } + + public Expression evaluateUp(AllocationExpression ptree) + throws ParseTreeException { + pop(); + return ptree; + } + +} diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/SourceCodeWriter.java b/src/main/java/io/devnulllabs/openjava/ptree/util/SourceCodeWriter.java new file mode 100644 index 0000000..571045e --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/SourceCodeWriter.java @@ -0,0 +1,1462 @@ +/* + * SourceCodeWriter.java + * + * comments here. + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.ptree.util; + +import java.io.PrintWriter; +import java.io.StringWriter; +import java.util.Enumeration; + +import io.devnulllabs.openjava.ptree.AllocationExpression; +import io.devnulllabs.openjava.ptree.ArrayAccess; +import io.devnulllabs.openjava.ptree.ArrayAllocationExpression; +import io.devnulllabs.openjava.ptree.ArrayInitializer; +import io.devnulllabs.openjava.ptree.AssignmentExpression; +import io.devnulllabs.openjava.ptree.BinaryExpression; +import io.devnulllabs.openjava.ptree.Block; +import io.devnulllabs.openjava.ptree.BreakStatement; +import io.devnulllabs.openjava.ptree.CaseGroup; +import io.devnulllabs.openjava.ptree.CaseGroupList; +import io.devnulllabs.openjava.ptree.CaseLabel; +import io.devnulllabs.openjava.ptree.CaseLabelList; +import io.devnulllabs.openjava.ptree.CastExpression; +import io.devnulllabs.openjava.ptree.CatchBlock; +import io.devnulllabs.openjava.ptree.CatchList; +import io.devnulllabs.openjava.ptree.ClassDeclaration; +import io.devnulllabs.openjava.ptree.ClassDeclarationList; +import io.devnulllabs.openjava.ptree.ClassLiteral; +import io.devnulllabs.openjava.ptree.CompilationUnit; +import io.devnulllabs.openjava.ptree.ConditionalExpression; +import io.devnulllabs.openjava.ptree.ConstructorDeclaration; +import io.devnulllabs.openjava.ptree.ConstructorInvocation; +import io.devnulllabs.openjava.ptree.ContinueStatement; +import io.devnulllabs.openjava.ptree.DoWhileStatement; +import io.devnulllabs.openjava.ptree.EmptyStatement; +import io.devnulllabs.openjava.ptree.Expression; +import io.devnulllabs.openjava.ptree.ExpressionList; +import io.devnulllabs.openjava.ptree.ExpressionStatement; +import io.devnulllabs.openjava.ptree.FieldAccess; +import io.devnulllabs.openjava.ptree.FieldDeclaration; +import io.devnulllabs.openjava.ptree.ForStatement; +import io.devnulllabs.openjava.ptree.IfStatement; +import io.devnulllabs.openjava.ptree.InstanceofExpression; +import io.devnulllabs.openjava.ptree.LabeledStatement; +import io.devnulllabs.openjava.ptree.Leaf; +import io.devnulllabs.openjava.ptree.List; +import io.devnulllabs.openjava.ptree.Literal; +import io.devnulllabs.openjava.ptree.MemberDeclarationList; +import io.devnulllabs.openjava.ptree.MemberInitializer; +import io.devnulllabs.openjava.ptree.MethodCall; +import io.devnulllabs.openjava.ptree.MethodDeclaration; +import io.devnulllabs.openjava.ptree.ModifierList; +import io.devnulllabs.openjava.ptree.NonLeaf; +import io.devnulllabs.openjava.ptree.Parameter; +import io.devnulllabs.openjava.ptree.ParameterList; +import io.devnulllabs.openjava.ptree.ParseTree; +import io.devnulllabs.openjava.ptree.ParseTreeException; +import io.devnulllabs.openjava.ptree.ReturnStatement; +import io.devnulllabs.openjava.ptree.SelfAccess; +import io.devnulllabs.openjava.ptree.Statement; +import io.devnulllabs.openjava.ptree.StatementList; +import io.devnulllabs.openjava.ptree.SwitchStatement; +import io.devnulllabs.openjava.ptree.SynchronizedStatement; +import io.devnulllabs.openjava.ptree.ThrowStatement; +import io.devnulllabs.openjava.ptree.TryStatement; +import io.devnulllabs.openjava.ptree.TypeName; +import io.devnulllabs.openjava.ptree.UnaryExpression; +import io.devnulllabs.openjava.ptree.Variable; +import io.devnulllabs.openjava.ptree.VariableDeclaration; +import io.devnulllabs.openjava.ptree.VariableDeclarator; +import io.devnulllabs.openjava.ptree.VariableInitializer; +import io.devnulllabs.openjava.ptree.WhileStatement; + +/** + * The class <code>SourceCodeWriter</code> is a Visitor role + * in the Visitor pattern and this also visits each child + * <code>ParseTree</code> object from left to right. + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: SourceCodeWriter.java,v 1.2 2003/02/19 02:55:00 tatsubori Exp $ + * @see io.devnulllabs.openjava.ptree.ParseTree + * @see io.devnulllabs.openjava.ptree.util.ParseTreeVisitor + */ +public class SourceCodeWriter extends ParseTreeVisitor { + + protected PrintWriter out; + + /** + * Why this modifier is not final ? + * - Because of javac bug in excuting it with -O option. + */ + public static String NEWLINE; + static { + StringWriter strw = new StringWriter(); + PrintWriter pw = new PrintWriter(strw); + pw.println(); + pw.close(); + NEWLINE = strw.toString(); + } + + /** to write debugging code */ + private int debugLevel = 0; + + /** to write debugging code */ + private String tab = " "; + private int nest = 0; + + /** to write debugging code */ + public void setDebugLevel(int n) { + debugLevel = n; + } + public int getDebugLevel() { + return debugLevel; + } + public void setTab(String str) { + tab = str; + } + public String getTab() { + return tab; + } + public void setNest(int i) { + nest = i; + } + public int getNest() { + return nest; + } + public void pushNest() { + setNest(getNest() + 1); + } + public void popNest() { + setNest(getNest() - 1); + } + + private final void writeDebugL(ParseTree ptree) { + if (getDebugLevel() > 0) { + out.print("["); + if (debugLevel > 1) { + String qname = ptree.getClass().getName(); + String sname = qname.substring(qname.lastIndexOf('.') + 1); + out.print(sname + "#"); + if (debugLevel > 2) { + out.print(ptree.getObjectID()); + } + out.print(" "); + } + } + } + + private final void writeDebugR() { + if (getDebugLevel() > 0) + out.print("]"); + } + + private final void writeDebugLR() { + if (getDebugLevel() > 0) + out.print("[]"); + } + + private final void writeDebugLln() { + if (getDebugLevel() > 0) + out.println("["); + } + + private final void writeDebugRln() { + if (getDebugLevel() > 0) + out.println("]"); + } + + private final void writeDebugln() { + if (getDebugLevel() > 0) + out.println(); + } + + private final void writeDebug(String str) { + if (getDebugLevel() > 0) + out.print(str); + } + + private final void writeTab() { + for (int i = 0; i < nest; i++) + out.print(getTab()); + } + + /** + * Allocates a source code writer. + * + */ + public SourceCodeWriter(PrintWriter out) { + super(); + this.out = out; + } + + public void visit(AllocationExpression p) throws ParseTreeException { + writeDebugL(p); + + Expression encloser = p.getEncloser(); + if (encloser != null) { + encloser.accept(this); + out.print(" . "); + } + + out.print("new "); + + TypeName tn = p.getClassType(); + tn.accept(this); + + ExpressionList args = p.getArguments(); + writeArguments(args); + + MemberDeclarationList mdlst = p.getClassBody(); + if (mdlst != null) { + out.println("{"); + pushNest(); + mdlst.accept(this); + popNest(); + writeTab(); + out.print("}"); + } + + writeDebugR(); + } + + public void visit(ArrayAccess p) throws ParseTreeException { + writeDebugL(p); + + Expression expr = p.getReferenceExpr(); + if (expr instanceof Leaf + || expr instanceof ArrayAccess + || expr instanceof FieldAccess + || expr instanceof MethodCall + || expr instanceof Variable) { + expr.accept(this); + } else { + writeParenthesis(expr); + } + + Expression index_expr = p.getIndexExpr(); + out.print("["); + index_expr.accept(this); + out.print("]"); + + writeDebugR(); + } + + public void visit(ArrayAllocationExpression p) throws ParseTreeException { + writeDebugL(p); + + out.print("new "); + + TypeName tn = p.getTypeName(); + tn.accept(this); + + ExpressionList dl = p.getDimExprList(); + for (int i = 0; i < dl.size(); ++i) { + Expression expr = dl.get(i); + out.print("["); + if (expr != null) { + expr.accept(this); + } + out.print("]"); + } + + ArrayInitializer ainit = p.getInitializer(); + if (ainit != null) + ainit.accept(this); + writeDebugR(); + } + + public void visit(ArrayInitializer p) throws ParseTreeException { + writeDebugL(p); + + out.print("{ "); + writeListWithDelimiter(p, ", "); + if (p.isRemainderOmitted()) + out.print(","); + out.print(" }"); + + writeDebugR(); + } + + public void visit(AssignmentExpression p) throws ParseTreeException { + writeDebugL(p); + + Expression lexpr = p.getLeft(); + if (lexpr instanceof AssignmentExpression) { + writeParenthesis(lexpr); + } else { + lexpr.accept(this); + } + + String operator = p.operatorString(); + out.print(" " + operator + " "); + + Expression rexp = p.getRight(); + rexp.accept(this); + + writeDebugR(); + } + + public void visit(BinaryExpression p) throws ParseTreeException { + writeDebugL(p); + + Expression lexpr = p.getLeft(); + if (isOperatorNeededLeftPar(p.getOperator(), lexpr)) { + writeParenthesis(lexpr); + } else { + lexpr.accept(this); + } + + String operator = p.operatorString(); + out.print(" " + operator + " "); + + Expression rexpr = p.getRight(); + if (isOperatorNeededRightPar(p.getOperator(), rexpr)) { + writeParenthesis(rexpr); + } else { + rexpr.accept(this); + } + + writeDebugR(); + } + + public void visit(Block p) throws ParseTreeException { + StatementList stmts = p.getStatements(); + writeTab(); + writeDebugL(p); + writeStatementsBlock(stmts); + writeDebugR(); + out.println(); + } + + public void visit(BreakStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("break"); + + String label = p.getLabel(); + if (label != null) { + out.print(" "); + out.print(label); + } + + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(CaseGroup p) throws ParseTreeException { + ExpressionList labels = p.getLabels(); + for (int i = 0; i < labels.size(); ++i) { + writeTab(); + Expression label = labels.get(i); + if (label == null) { + out.print("default "); + } else { + out.print("case "); + label.accept(this); + } + out.println(" :"); + } + + pushNest(); + StatementList stmts = p.getStatements(); + stmts.accept(this); + popNest(); + } + + public void visit(CaseGroupList p) throws ParseTreeException { + writeListWithSuffix(p, NEWLINE); + } + + public void visit(CaseLabel p) throws ParseTreeException { + Expression expr = p.getExpression(); + if (expr != null) { + out.print("case "); + expr.accept(this); + } else { + out.print("default"); + } + out.print(":"); + } + + public void visit(CaseLabelList p) throws ParseTreeException { + writeListWithSuffix(p, NEWLINE); + } + + public void visit(CastExpression p) throws ParseTreeException { + writeDebugL(p); + + out.print("("); + TypeName ts = p.getTypeSpecifier(); + ts.accept(this); + out.print(")"); + + out.print(" "); + + Expression expr = p.getExpression(); + if (expr instanceof AssignmentExpression + || expr instanceof ConditionalExpression + || expr instanceof BinaryExpression + || expr instanceof InstanceofExpression + || expr instanceof UnaryExpression) { + writeParenthesis(expr); + } else { + expr.accept(this); + } + + writeDebugR(); + } + + public void visit(CatchBlock p) throws ParseTreeException { + out.print(" catch "); + + out.print("( "); + + Parameter param = p.getParameter(); + param.accept(this); + + out.print(" ) "); + + StatementList stmts = p.getBody(); + writeStatementsBlock(stmts); + } + + public void visit(CatchList p) throws ParseTreeException { + writeList(p); + } + + public void visit(ClassDeclaration p) throws ParseTreeException { + printComment(p); + + writeTab(); + writeDebugL(p); + + /*ModifierList*/ + ModifierList modifs = p.getModifiers(); + if (modifs != null) { + modifs.accept(this); + if (!modifs.isEmptyAsRegular()) + out.print(" "); + } + + /*"class"*/ + if (p.isInterface()) { + out.print("interface "); + } else { + out.print("class "); + } + + String name = p.getName(); + out.print(name); + + /*"extends" TypeName*/ + TypeName[] zuper = p.getBaseclasses(); + if (zuper.length != 0) { + out.print(" extends "); + zuper[0].accept(this); + for (int i = 1; i < zuper.length; ++i) { + out.print(", "); + zuper[i].accept(this); + } + } else { + writeDebug(" "); + writeDebugLR(); + } + + /* "implements" ClassTypeList */ + TypeName[] impl = p.getInterfaces(); + if (impl.length != 0) { + out.print(" implements "); + impl[0].accept(this); + for (int i = 1; i < impl.length; ++i) { + out.print(", "); + impl[i].accept(this); + } + } else { + writeDebug(" "); + writeDebugLR(); + } + + out.println(); + + /* MemberDeclarationList */ + MemberDeclarationList classbody = p.getBody(); + writeTab(); + out.println("{"); + if (classbody.isEmpty()) { + classbody.accept(this); + } else { + out.println(); + pushNest(); + classbody.accept(this); + popNest(); + out.println(); + } + writeTab(); + out.print("}"); + + writeDebugR(); + out.println(); + } + + public void visit(ClassDeclarationList p) throws ParseTreeException { + writeListWithDelimiter(p, NEWLINE + NEWLINE); + } + + public void visit(ClassLiteral p) throws ParseTreeException { + writeDebugL(p); + TypeName type = p.getTypeName(); + type.accept(this); + out.print(".class"); + writeDebugR(); + } + + public void visit(CompilationUnit p) throws ParseTreeException { + out.println("/*"); + out.println(" * This code was generated by ojc."); + out.println(" */"); + + printComment(p); + + /* package statement */ + String qn = p.getPackage(); + if (qn != null) { + writeDebugL(p); + out.print("package " + qn + ";"); + writeDebugR(); + out.println(); + + out.println(); + out.println(); + } + + /* import statement list */ + String[] islst = p.getDeclaredImports(); + if (islst.length != 0) { + for (int i = 0; i < islst.length; ++i) { + out.println("import " + islst[i] + ";"); + } + out.println(); + out.println(); + } + + /* type declaration list */ + ClassDeclarationList tdlst = p.getClassDeclarations(); + tdlst.accept(this); + } + + public void visit(ConditionalExpression p) throws ParseTreeException { + writeDebugL(p); + + Expression condition = p.getCondition(); + if (condition instanceof AssignmentExpression + || condition instanceof ConditionalExpression) { + writeParenthesis(condition); + } else { + condition.accept(this); + } + + out.print(" ? "); + + Expression truecase = p.getTrueCase(); + if (truecase instanceof AssignmentExpression) { + writeParenthesis(truecase); + } else { + truecase.accept(this); + } + + out.print(" : "); + + Expression falsecase = p.getFalseCase(); + if (falsecase instanceof AssignmentExpression) { + writeParenthesis(falsecase); + } else { + falsecase.accept(this); + } + + writeDebugR(); + } + + public void visit(ConstructorDeclaration p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + /*ModifierList*/ + ModifierList modifs = p.getModifiers(); + if (modifs != null) { + modifs.accept(this); + if (!modifs.isEmptyAsRegular()) + out.print(" "); + } + + String name = p.getName(); + out.print(name); + + ParameterList params = p.getParameters(); + out.print("("); + if (params.size() != 0) { + out.print(" "); + params.accept(this); + out.print(" "); + } + out.print(")"); + + TypeName[] tnl = p.getThrows(); + if (tnl.length != 0) { + out.println(); + writeTab(); + writeTab(); + out.print("throws "); + tnl[0].accept(this); + for (int i = 1; i < tnl.length; ++i) { + out.print(", "); + tnl[i].accept(this); + } + } + + ConstructorInvocation sc = p.getConstructorInvocation(); + StatementList body = p.getBody(); + if (body == null && sc == null) { + out.println(";"); + } else { + out.println(); + + writeTab(); + out.println("{"); + pushNest(); + + if (sc != null) + sc.accept(this); + if (body != null) + body.accept(this); + + popNest(); + writeTab(); + out.print("}"); + } + + writeDebugR(); + out.println(); + } + + public void visit(ConstructorInvocation p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + if (p.isSelfInvocation()) { + out.print("this"); + } else { + Expression enclosing = p.getEnclosing(); + if (enclosing != null) { + enclosing.accept(this); + out.print(" . "); + } + out.print("super"); + } + + ExpressionList exprs = p.getArguments(); + writeArguments(exprs); + + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(ContinueStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("continue"); + + String label = p.getLabel(); + if (label != null) { + out.print(" " + label); + } + + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(DoWhileStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("do "); + + StatementList stmts = p.getStatements(); + + if (stmts.isEmpty()) { + out.print(" ; "); + } else { + writeStatementsBlock(stmts); + } + + out.print(" while "); + + out.print("("); + Expression expr = p.getExpression(); + expr.accept(this); + out.print(")"); + + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(EmptyStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(ExpressionList p) throws ParseTreeException { + writeListWithDelimiter(p, ", "); + } + + public void visit(ExpressionStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + Expression expr = p.getExpression(); + expr.accept(this); + + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(FieldAccess p) throws ParseTreeException { + writeDebugL(p); + + Expression expr = p.getReferenceExpr(); + TypeName typename = p.getReferenceType(); + if (expr != null) { + if (expr instanceof Leaf + || expr instanceof ArrayAccess + || expr instanceof FieldAccess + || expr instanceof MethodCall + || expr instanceof Variable) { + expr.accept(this); + } else { + out.print("("); + expr.accept(this); + out.print(")"); + } + out.print("."); + } else if (typename != null) { + typename.accept(this); + out.print("."); + } + + String name = p.getName(); + out.print(name); + + writeDebugR(); + } + + public void visit(FieldDeclaration p) throws ParseTreeException { + printComment(p); + + writeTab(); + writeDebugL(p); + + /*ModifierList*/ + ModifierList modifs = p.getModifiers(); + if (modifs != null) { + modifs.accept(this); + if (!modifs.isEmptyAsRegular()) + out.print(" "); + } + + /*TypeName*/ + TypeName ts = p.getTypeSpecifier(); + ts.accept(this); + + out.print(" "); + + /*Variable*/ + String variable = p.getVariable(); + out.print(variable); + + /*"=" VariableInitializer*/ + VariableInitializer initializer = p.getInitializer(); + if (initializer != null) { + out.print(" = "); + initializer.accept(this); + } + /*";"*/ + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(ForStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("for "); + + out.print("("); + + ExpressionList init = p.getInit(); + TypeName tspec = p.getInitDeclType(); + VariableDeclarator[] vdecls = p.getInitDecls(); + if (init != null && (!init.isEmpty())) { + init.get(0).accept(this); + for (int i = 1; i < init.size(); ++i) { + out.print(", "); + init.get(i).accept(this); + } + } else if (tspec != null && vdecls != null && vdecls.length != 0) { + tspec.accept(this); + out.print(" "); + vdecls[0].accept(this); + for (int i = 1; i < vdecls.length; ++i) { + out.print(", "); + vdecls[i].accept(this); + } + } + + out.print(";"); + + Expression expr = p.getCondition(); + if (expr != null) { + out.print(" "); + expr.accept(this); + } + + out.print(";"); + + ExpressionList incr = p.getIncrement(); + if (incr != null && (!incr.isEmpty())) { + out.print(" "); + incr.get(0).accept(this); + for (int i = 1; i < incr.size(); ++i) { + out.print(", "); + incr.get(i).accept(this); + } + } + + out.print(") "); + + StatementList stmts = p.getStatements(); + if (stmts.isEmpty()) { + out.print(";"); + } else { + writeStatementsBlock(stmts); + } + + writeDebugR(); + out.println(); + } + + public void visit(IfStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("if "); + + out.print("("); + Expression expr = p.getExpression(); + expr.accept(this); + out.print(") "); + + /* then part */ + StatementList stmts = p.getStatements(); + writeStatementsBlock(stmts); + + /* else part */ + StatementList elsestmts = p.getElseStatements(); + if (!elsestmts.isEmpty()) { + out.print(" else "); + writeStatementsBlock(elsestmts); + } + + writeDebugR(); + out.println(); + } + + public void visit(InstanceofExpression p) throws ParseTreeException { + writeDebugL(p); + + /* this is too strict for + or - */ + Expression lexpr = p.getExpression(); + if (lexpr instanceof AssignmentExpression + || lexpr instanceof ConditionalExpression + || lexpr instanceof BinaryExpression) { + writeParenthesis(lexpr); + } else { + lexpr.accept(this); + } + + out.print(" instanceof "); + + TypeName tspec = p.getTypeSpecifier(); + tspec.accept(this); + + writeDebugR(); + } + + public void visit(LabeledStatement p) throws ParseTreeException { + writeTab(); + + String name = p.getLabel(); + out.print(name); + + out.println(" : "); + + Statement statement = p.getStatement(); + statement.accept(this); + } + + public void visit(Literal p) throws ParseTreeException { + out.print(p.toString()); + } + + public void visit(MemberDeclarationList p) throws ParseTreeException { + writeListWithDelimiter(p, NEWLINE); + } + + public void visit(MemberInitializer p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + if (p.isStatic()) { + out.print("static "); + } + + StatementList stmts = p.getBody(); + writeStatementsBlock(stmts); + + writeDebugR(); + out.println(); + } + + public void visit(MethodCall p) throws ParseTreeException { + writeDebugL(p); + + Expression expr = p.getReferenceExpr(); + TypeName reftype = p.getReferenceType(); + if (expr != null) { + if (expr instanceof Leaf + || expr instanceof ArrayAccess + || expr instanceof FieldAccess + || expr instanceof MethodCall + || expr instanceof Variable) { + expr.accept(this); + } else { + writeParenthesis(expr); + } + out.print("."); + } else if (reftype != null) { + reftype.accept(this); + out.print("."); + } + + String name = p.getName(); + out.print(name); + + ExpressionList args = p.getArguments(); + writeArguments(args); + + writeDebugR(); + } + + public void visit(MethodDeclaration p) throws ParseTreeException { + printComment(p); + + writeTab(); + writeDebugL(p); + + /*ModifierList*/ + ModifierList modifs = p.getModifiers(); + if (modifs != null) { + modifs.accept(this); + if (!modifs.isEmptyAsRegular()) + out.print(" "); + } + + TypeName ts = p.getReturnType(); + ts.accept(this); + + out.print(" "); + + String name = p.getName(); + out.print(name); + + ParameterList params = p.getParameters(); + out.print("("); + if (!params.isEmpty()) { + out.print(" "); + params.accept(this); + out.print(" "); + } else { + params.accept(this); + } + out.print(")"); + + TypeName[] tnl = p.getThrows(); + if (tnl.length != 0) { + out.println(); + writeTab(); + writeTab(); + out.print("throws "); + tnl[0].accept(this); + for (int i = 1; i < tnl.length; ++i) { + out.print(", "); + tnl[i].accept(this); + } + } + + StatementList bl = p.getBody(); + if (bl == null) { + out.print(";"); + } else { + out.println(); + writeTab(); + out.print("{"); + if (bl.isEmpty()) { + bl.accept(this); + } else { + out.println(); + pushNest(); + bl.accept(this); + popNest(); + writeTab(); + } + out.print("}"); + } + + writeDebugR(); + out.println(); + } + + public void visit(ModifierList p) throws ParseTreeException { + writeDebugL(p); + + out.print(ModifierList.toString(p.getRegular())); + + writeDebugR(); + } + + public void visit(Parameter p) throws ParseTreeException { + writeDebugL(p); + + ModifierList modifs = p.getModifiers(); + modifs.accept(this); + if (!modifs.isEmptyAsRegular()) + out.print(" "); + + TypeName typespec = p.getTypeSpecifier(); + typespec.accept(this); + + out.print(" "); + + String declname = p.getVariable(); + out.print(declname); + + writeDebugR(); + } + + public void visit(ParameterList p) throws ParseTreeException { + writeListWithDelimiter(p, ", "); + } + + public void visit(ReturnStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("return"); + + Expression expr = p.getExpression(); + if (expr != null) { + out.print(" "); + expr.accept(this); + } + + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(SelfAccess p) throws ParseTreeException { + out.print(p.toString()); + } + + public void visit(StatementList p) throws ParseTreeException { + writeList(p); + } + + public void visit(SwitchStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("switch "); + out.print("("); + Expression expr = p.getExpression(); + expr.accept(this); + out.print(")"); + + out.println(" {"); + + CaseGroupList casegrouplist = p.getCaseGroupList(); + casegrouplist.accept(this); + + writeTab(); + out.print("}"); + writeDebugR(); + out.println(); + } + + public void visit(SynchronizedStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("synchronized "); + + out.print("("); + Expression expr = p.getExpression(); + expr.accept(this); + out.println(")"); + + StatementList stmts = p.getStatements(); + writeStatementsBlock(stmts); + + writeDebugR(); + out.println(); + } + + public void visit(ThrowStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("throw "); + + Expression expr = p.getExpression(); + expr.accept(this); + + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(TryStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("try "); + + StatementList stmts = p.getBody(); + writeStatementsBlock(stmts); + + CatchList catchlist = p.getCatchList(); + if (!catchlist.isEmpty()) { + catchlist.accept(this); + } + + StatementList finstmts = p.getFinallyBody(); + if (!finstmts.isEmpty()) { + out.println(" finally "); + writeStatementsBlock(finstmts); + } + + writeDebugR(); + out.println(); + } + + /******rough around innerclass********/ + public void visit(TypeName p) throws ParseTreeException { + writeDebugL(p); + + String typename = p.getName().replace('$', '.'); + out.print(typename); + + int dims = p.getDimension(); + out.print(TypeName.stringFromDimension(dims)); + + writeDebugR(); + } + + public void visit(UnaryExpression p) throws ParseTreeException { + writeDebugL(p); + + if (p.isPrefix()) { + String operator = p.operatorString(); + out.print(operator); + } + + Expression expr = p.getExpression(); + if (expr instanceof AssignmentExpression + || expr instanceof ConditionalExpression + || expr instanceof BinaryExpression + || expr instanceof InstanceofExpression + || expr instanceof CastExpression + || expr instanceof UnaryExpression) { + writeParenthesis(expr); + } else { + expr.accept(this); + } + + if (p.isPostfix()) { + String operator = p.operatorString(); + out.print(operator); + } + + writeDebugR(); + } + + public void visit(Variable p) throws ParseTreeException { + out.print(p.toString()); + } + + public void visit(VariableDeclaration p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + ModifierList modifs = p.getModifiers(); + modifs.accept(this); + if (!modifs.isEmptyAsRegular()) + out.print(" "); + + TypeName typespec = p.getTypeSpecifier(); + typespec.accept(this); + + out.print(" "); + + VariableDeclarator vd = p.getVariableDeclarator(); + vd.accept(this); + + out.print(";"); + + writeDebugR(); + out.println(); + } + + public void visit(VariableDeclarator p) throws ParseTreeException { + String declname = p.getVariable(); + out.print(declname); + for (int i = 0; i < p.getDimension(); ++i) { + out.print("[]"); + } + + VariableInitializer varinit = p.getInitializer(); + if (varinit != null) { + out.print(" = "); + varinit.accept(this); + } + } + + public void visit(WhileStatement p) throws ParseTreeException { + writeTab(); + writeDebugL(p); + + out.print("while "); + + out.print("("); + Expression expr = p.getExpression(); + expr.accept(this); + out.print(") "); + + StatementList stmts = p.getStatements(); + if (stmts.isEmpty()) { + out.print(" ;"); + } else { + writeStatementsBlock(stmts); + } + + writeDebugR(); + out.println(); + } + + private final void writeArguments(ExpressionList args) + throws ParseTreeException { + out.print("("); + if (!args.isEmpty()) { + out.print(" "); + args.accept(this); + out.print(" "); + } else { + args.accept(this); + } + out.print(")"); + } + + private final void writeAnonymous(Object obj) throws ParseTreeException { + if (obj == null) { + writeDebug("#null"); + } else if (obj instanceof ParseTree) { + ((ParseTree) obj).accept(this); + } else { + out.print(obj.toString()); + } + } + + private final void writeList(List list) throws ParseTreeException { + Enumeration it = list.elements(); + + while (it.hasMoreElements()) { + Object elem = it.nextElement(); + writeAnonymous(elem); + } + } + + private final void writeListWithDelimiter(List list, String delimiter) + throws ParseTreeException { + Enumeration it = list.elements(); + + if (!it.hasMoreElements()) + return; + + writeAnonymous(it.nextElement()); + while (it.hasMoreElements()) { + out.print(delimiter); + writeAnonymous(it.nextElement()); + } + } + + private final void writeListWithSuffix(List list, String suffix) + throws ParseTreeException { + Enumeration it = list.elements(); + + while (it.hasMoreElements()) { + writeAnonymous(it.nextElement()); + out.print(suffix); + } + } + + private final void writeParenthesis(Expression expr) + throws ParseTreeException { + out.print("("); + expr.accept(this); + out.print(")"); + } + + private final void writeStatementsBlock(StatementList stmts) + throws ParseTreeException { + out.println("{"); + pushNest(); + + stmts.accept(this); + + popNest(); + writeTab(); + out.print("}"); + } + + private static final boolean isOperatorNeededLeftPar( + int operator, + Expression leftexpr) { + if (leftexpr instanceof AssignmentExpression + || leftexpr instanceof ConditionalExpression) { + return true; + } + + int op = operatorStrength(operator); + + if (leftexpr instanceof InstanceofExpression) { + return (op > operatorStrength(BinaryExpression.INSTANCEOF)); + } + + if (!(leftexpr instanceof BinaryExpression)) + return false; + + BinaryExpression lbexpr = (BinaryExpression) leftexpr; + return (op > operatorStrength(lbexpr.getOperator())); + } + + private static final boolean isOperatorNeededRightPar( + int operator, + Expression rightexpr) { + if (rightexpr instanceof AssignmentExpression + || rightexpr instanceof ConditionalExpression) { + return true; + } + + int op = operatorStrength(operator); + + if (rightexpr instanceof InstanceofExpression) { + return (op >= operatorStrength(BinaryExpression.INSTANCEOF)); + } + + if (!(rightexpr instanceof BinaryExpression)) + return false; + + BinaryExpression lbexpr = (BinaryExpression) rightexpr; + return (op >= operatorStrength(lbexpr.getOperator())); + } + + /** + * Returns the strength of the union of the operator. + * + * @param op the id number of operator. + * @return the strength of the union. + */ + private static final int operatorStrength(int op) { + switch (op) { + case BinaryExpression.TIMES : + case BinaryExpression.DIVIDE : + case BinaryExpression.MOD : + return 40; + case BinaryExpression.PLUS : + case BinaryExpression.MINUS : + return 35; + case BinaryExpression.SHIFT_L : + case BinaryExpression.SHIFT_R : + case BinaryExpression.SHIFT_RR : + return 30; + case BinaryExpression.LESS : + case BinaryExpression.GREATER : + case BinaryExpression.LESSEQUAL : + case BinaryExpression.GREATEREQUAL : + case BinaryExpression.INSTANCEOF : + return 25; + case BinaryExpression.EQUAL : + case BinaryExpression.NOTEQUAL : + return 20; + case BinaryExpression.BITAND : + return 16; + case BinaryExpression.XOR : + return 14; + case BinaryExpression.BITOR : + return 12; + case BinaryExpression.LOGICAL_AND : + return 10; + case BinaryExpression.LOGICAL_OR : + return 8; + } + return 100; + } + + private final void printComment(NonLeaf p) { + String comment = p.getComment(); + if (comment != null) { + writeTab(); + out.println(comment); + } + } + +} diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/TypeNameQualifier.java b/src/main/java/io/devnulllabs/openjava/ptree/util/TypeNameQualifier.java new file mode 100644 index 0000000..19db286 --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/TypeNameQualifier.java @@ -0,0 +1,107 @@ +/* + * TypeNameQualifier.java + * + * Make typenames qualified. + * <p> + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.ptree.util; + +import io.devnulllabs.openjava.mop.Environment; +import io.devnulllabs.openjava.ptree.ClassDeclaration; +import io.devnulllabs.openjava.ptree.ClassLiteral; +import io.devnulllabs.openjava.ptree.ConstructorDeclaration; +import io.devnulllabs.openjava.ptree.MethodDeclaration; +import io.devnulllabs.openjava.ptree.TypeName; + +/** + * The class <code>TypeNameQualifier</code> is a utility class + * to be usede for making a copy of ptree work well without import + * statements. + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: TypeNameQualifier.java,v 1.2 2003/02/19 02:55:00 tatsubori Exp $ + * @see java.lang.Object + */ +public class TypeNameQualifier extends ScopeHandler { + private String newName = null; + + /** + * Constructs a new visitor for parse tree for qualifying + * each type name appearing there. + * + * @param env environment for qualifying type names. + */ + public TypeNameQualifier(Environment env) { + this(env, null); + } + + /** + * Constructs a new visitor for parse tree for qualifying + * each type name appearing there. + * + * @param env environment for qualifying type names. + * @param newClassName the class name for constructors. + * If <code>null</code> is specified, the name for constructors + * will remain as is. + */ + public TypeNameQualifier(Environment env, String newClassName) { + super(env); + this.newName = newClassName; + } + + private String qualify(String org) { + return getEnvironment().toQualifiedName(org); + } + + private TypeName qualifyName(TypeName name) { + return new TypeName(qualify(name.getName()), name.getDimension()); + } + + private TypeName[] qualifyNames(TypeName[] names) { + for (int i = 0; i < names.length; ++i) { + names[i] = qualifyName(names[i]); + } + return names; + } + + public void visit(ClassDeclaration c) + throws io.devnulllabs.openjava.ptree.ParseTreeException { + c.setBaseclasses(qualifyNames(c.getBaseclasses())); + c.setInterfaces(qualifyNames(c.getInterfaces())); + super.visit(c); + } + + public void visit(ConstructorDeclaration c) + throws io.devnulllabs.openjava.ptree.ParseTreeException { + if (newName != null) + c.setName(newName); + c.setThrows(qualifyNames(c.getThrows())); + super.visit(c); + } + + public void visit(MethodDeclaration m) + throws io.devnulllabs.openjava.ptree.ParseTreeException { + m.setThrows(qualifyNames(m.getThrows())); + super.visit(m); + } + + public void visit(TypeName tname) + throws io.devnulllabs.openjava.ptree.ParseTreeException { + tname.setName(qualify(tname.getName())); + super.visit(tname); + } + + /** This is needed because of a bug. */ + public void visit(ClassLiteral clit) + throws io.devnulllabs.openjava.ptree.ParseTreeException { + clit.replace(new ClassLiteral(qualifyName(clit.getTypeName()))); + } +} diff --git a/src/main/java/io/devnulllabs/openjava/ptree/util/VariableBinder.java b/src/main/java/io/devnulllabs/openjava/ptree/util/VariableBinder.java new file mode 100644 index 0000000..05f03d7 --- /dev/null +++ b/src/main/java/io/devnulllabs/openjava/ptree/util/VariableBinder.java @@ -0,0 +1,115 @@ +/* + * VariableBinder.java + * + * comments here. + * + * @author Michiaki Tatsubori + * @version %VERSION% %DATE% + * @see java.lang.Object + * + * COPYRIGHT 1998 by Michiaki Tatsubori, ALL RIGHTS RESERVED. + */ +package io.devnulllabs.openjava.ptree.util; + +import io.devnulllabs.openjava.mop.Environment; +import io.devnulllabs.openjava.mop.OJClass; +import io.devnulllabs.openjava.mop.OJClassNotFoundException; +import io.devnulllabs.openjava.ptree.ForStatement; +import io.devnulllabs.openjava.ptree.Parameter; +import io.devnulllabs.openjava.ptree.ParseTreeException; +import io.devnulllabs.openjava.ptree.Statement; +import io.devnulllabs.openjava.ptree.TypeName; +import io.devnulllabs.openjava.ptree.VariableDeclaration; +import io.devnulllabs.openjava.ptree.VariableDeclarator; +import io.devnulllabs.openjava.tools.DebugOut; + +/** + * The class <code>VariableBinder</code> + * <p> + * For example + * <pre> + * </pre> + * <p> + * + * @author Michiaki Tatsubori + * @version 1.0 + * @since $Id: VariableBinder.java,v 1.2 2003/02/19 02:55:00 tatsubori Exp $ + * @see java.lang.Object + */ +public class VariableBinder extends ScopeHandler { + + public VariableBinder(Environment env) { + super(env); + } + + public Statement evaluateDown(VariableDeclaration ptree) + throws ParseTreeException { + super.evaluateDown(ptree); + bindLocalVariable(ptree, getEnvironment()); + + return ptree; + } + + public Statement evaluateDown(ForStatement ptree) + throws ParseTreeException { + super.evaluateDown(ptree); + TypeName tspec = ptree.getInitDeclType(); + if (tspec == null) + return ptree; + VariableDeclarator[] vdecls = ptree.getInitDecls(); + bindForInit(tspec, vdecls, getEnvironment()); + + return ptree; + } + + public Parameter evaluateDown(Parameter ptree) throws ParseTreeException { + super.evaluateDown(ptree); + bindParameter(ptree, getEnvironment()); + + return ptree; + } + + private static void bindLocalVariable( + VariableDeclaration var_decl, + Environment env) { + String type = var_decl.getTypeSpecifier().toString(); + String name = var_decl.getVariable(); + bindName(env, type, name); + } + + private static void bindForInit( + TypeName tspec, + VariableDeclarator[] vdecls, + Environment env) { + for (int i = 0; i < vdecls.length; ++i) { + String type = tspec.toString() + vdecls[i].dimensionString(); + String name = vdecls[i].getVariable(); + bindName(env, type, name); + } + } + + private static void bindParameter(Parameter param, Environment env) { + String type = param.getTypeSpecifier().toString(); + String name = param.getVariable(); + bindName(env, type, name); + } + + private static void bindName(Environment env, String type, String name) { + String qtypename = env.toQualifiedName(type); + try { + OJClass clazz = env.lookupClass(qtypename); + if (clazz == null) + clazz = OJClass.forName(qtypename); + env.bindVariable(name, clazz); + DebugOut.println("binds variable\t" + name + "\t: " + qtypename); + } catch (OJClassNotFoundException e) { + System.err.println( + "VariableBinder.bindName() " + + e.toString() + + " : " + + qtypename); + System.err.println(env); + } + } + +} |