/* * ParseTreeTranslator.java * * under construction * * @author Michiaki Tatsubori * @version %VERSION% %DATE% * @see java.lang.Object * * COPYRIGHT 1999 by Michiaki Tatsubori, ALL RIGHTS RESERVED. */ package openjava.ptree.util; import openjava.mop.*; import openjava.ptree.*; /** * The class <code>ParseTreeTranslator</code> can be inherited by * subclasses to make traditonal translation. * <p> * UNDER CONSTRUCTION * <pre> * </pre> * <p> * * @author Michiaki Tatsubori * @version 1.0 * @since %SOFTWARE% 1.0 * @see java.lang.Object */ public class ParseTreeTranslator { /** * Main method to be called. * * @exception MOPException when exception occurs */ public final void translate( OJClass clazz ) throws MOPException { try { translateClassDeclaration( clazz.getSourceCode() ); } catch ( ParseTreeException e ) { throw new MOPException( e ); } } /** * Tests if the ptree object is an instance of iteration statement * classes. * <br> * Iteration statement classes are : * <br><blockquote> * DoWhileStatement * ForStatement * WhileStatement * </blockquote><br> * This is an utility method. * * @param p ptree-node object to test * @return true if the specified ptree object is an instance of * iteration statement classes. */ public static final boolean isIterationStatement( ParseTree p ) { if(p instanceof DoWhileStatement) return true; if(p instanceof ForStatement) return true; if(p instanceof WhileStatement) return true; return false; } /** * Tests if the ptree object is an instance of selection statement * classes. * <br> * Selection statement classes are : * <br><blockquote> * IfStatement * SwitchStatement * </blockquote><br> * This is an utility method. * * @param p ptree-node object to test * @return true if the specified ptree object is an instance of * selection statement classes. */ public static final boolean isSelectionStatement( ParseTree p ) { if(p instanceof IfStatement) return true; if(p instanceof SwitchStatement) return true; return false; } /** * Tests if the ptree object is an instance of jump statement * classes. * <br> * Jump statement classes are : * <br><blockquote> * BreakStatement * ContinueStatement * ReturnStatement * ThrowStatement * </blockquote><br> * This is an utility method. * * @param p ptree-node object to test * @return true if the specified ptree object is an instance of * jump statement classes. */ public static final boolean isJumpStatement( ParseTree p ) { if(p instanceof BreakStatement) return true; if(p instanceof ContinueStatement) return true; if(p instanceof ReturnStatement) return true; if(p instanceof ThrowStatement) return true; return false; } /** * Tests if the ptree object is an instance of guarding statement * classes. * <br> * Guarding statement classes are : * <br><blockquote> * SynchronizedStatement * TryStatement * </blockquote><br> * This is an utility method. * * @param p ptree-node object to test * @return true if the specified ptree object is an instance of * guarding statement classes. */ public static final boolean isGuardingStatement( ParseTree p ) { if(p instanceof SynchronizedStatement) return true; if(p instanceof TryStatement) return true; if(p instanceof Block) return true; return false; } /** * Tests if the ptree object is an instance of allocation expression * classes. * <br> * Allocation expression classes are : * <br><blockquote> * AllocationExpression * ArrayAllocationExpression * </blockquote><br> * This is an utility method. * * @param p ptree-node object to test * @return true if the specified ptree object is an instance of * allocation expression classes. */ public static final boolean isAllocationExpression( ParseTree p ) { if(p instanceof AllocationExpression) return true; if(p instanceof ArrayAllocationExpression) return true; return false; } /** * Translates class declaration * * @return translated node * @param classdecl translating node * @exception ParseTreeException when exception occurs */ protected ClassDeclaration translateClassDeclaration( ClassDeclaration classdecl ) throws ParseTreeException { ModifierList modifs = classdecl.getModifiers(); classdecl.setModifiers( translateModifierList( modifs ) ); Identifier name = classdecl.getName(); classdecl.setName( translateIdentifier( name ) ); ClassType zuper = classdecl.getSuper(); zuper = (zuper == null) ? null : translateClassType( zuper ); classdecl.setSuper( zuper ); ClassTypeList ifacelist = classdecl.getInterfaces(); classdecl.setInterfaces( translateClassTypeList( ifacelist ) ); MemberDeclarationList body = classdecl.getBody(); classdecl.setBody( translateMemberDeclarationList( body ) ); return classdecl; } /** * Translates field declaration list * * @return translated node * @param fieldlist translating list * @exception ParseTreeException when exception occurs */ protected MemberDeclarationList translateMemberDeclarationList( MemberDeclarationList memlist ) throws ParseTreeException { boolean same = true; MemberDeclarationList ret = new MemberDeclarationList(); for( int i = 0; i < memlist.size(); i++ ){ MemberDeclaration old_mdecl = memlist.get( i ); MemberDeclaration mdecl = translateMemberDeclaration( old_mdecl ); if(! old_mdecl.eq( mdecl )) same = false; ret.add( mdecl ); } if(same){ return memlist; } return ret; } /** * Translates member declaration list node * * @return translated node * @param mem translating node * @exception ParseTreeException when exception occurs */ protected MemberDeclaration translateMemberDeclaration( MemberDeclaration mem ) throws ParseTreeException { if(mem instanceof FieldDeclaration){ return translateFieldDeclaration( (FieldDeclaration) mem ); } if(mem instanceof MethodDeclaration){ return translateMethodDeclaration( (MethodDeclaration) mem ); } if(mem instanceof ConstructorDeclaration){ return translateConstructorDeclaration( (ConstructorDeclaration) mem ); } if(mem instanceof MemberInitializer){ return translateMemberInitializer( (MemberInitializer) mem ); } if(mem instanceof ClassDeclaration){ return translateClassDeclaration( (ClassDeclaration) mem ); } throw new ParseTreeException( "translateMemberDeclaration() : " + "illegal MemberDeclaration object." ); } /** * Translates field variable declaration node * * @return translated node * @param fieldvardecl translating node * @exception ParseTreeException when exception occurs */ protected MemberDeclaration translateFieldDeclaration( FieldDeclaration fielddecl ) throws ParseTreeException { ModifierList old_modiflist = fielddecl.getModifiers(); ModifierList modiflist = translateModifierList( old_modiflist ); fielddecl.setModifiers( modiflist ); TypeSpecifier old_type = fielddecl.getTypeSpecifier(); TypeSpecifier type = translateTypeSpecifier( old_type ); fielddecl.setTypeSpecifier( type ); VariableDeclarator old_decl = fielddecl.getVariableDeclarator(); VariableDeclarator decl = translateVariableDeclarator( old_decl ); fielddecl.setVariableDeclarator( decl ); return fielddecl; } /** * Translates method declaration node * * @return translated node * @param methoddecl translating node * @exception ParseTreeException when exception occurs */ protected MemberDeclaration translateMethodDeclaration( MethodDeclaration methoddecl ) throws ParseTreeException { ModifierList old_modiflist = methoddecl.getModifiers(); ModifierList modiflist = translateModifierList( old_modiflist); methoddecl.setModifiers( modiflist ); TypeSpecifier old_typespec = methoddecl.getTypeSpecifier(); TypeSpecifier typespec = translateTypeSpecifier( old_typespec ); methoddecl.setTypeSpecifier( typespec ); Identifier old_name = methoddecl.getName(); Identifier name = translateIdentifier( old_name ); methoddecl.setName( name ); ParameterList old_params = methoddecl.getParameters(); ParameterList params = translateParameterList( old_params ); methoddecl.setParameters( params ); ClassTypeList old_throwlist = methoddecl.getThrows(); ClassTypeList throwlist = translateClassTypeList( old_throwlist ); methoddecl.setThrows( throwlist ); StatementList old_body = methoddecl.getBody(); StatementList body = null; if(old_body != null){ body = translateStatementList( old_body ); } methoddecl.setBody( body ); return methoddecl; } /** * Translates constructor declaration node * * @return translated node * @param constdecl translating node * @exception ParseTreeException when exception occurs */ protected MemberDeclaration translateConstructorDeclaration( ConstructorDeclaration constdecl ) throws ParseTreeException { ModifierList old_modiflist = constdecl.getModifiers(); ModifierList modiflist = translateModifierList( old_modiflist ); constdecl.setModifiers( modiflist ); Identifier old_name = constdecl.getName(); Identifier name = translateIdentifier( old_name ); constdecl.setName( name ); ParameterList old_params = constdecl.getParameters(); ParameterList params = translateParameterList( old_params ); constdecl.setParameters( params ); ClassTypeList old_throwlist = constdecl.getThrows(); ClassTypeList throwlist = translateClassTypeList( old_throwlist ); constdecl.setThrows( throwlist ); StatementList old_body = constdecl.getBody(); StatementList body = null; if(old_body != null){ body = translateStatementList( old_body ); } constdecl.setBody( body ); return constdecl; } /** * Translates static initializer node * * @return translated node * @param staticinit translating node * @exception ParseTreeException when exception occurs */ protected MemberDeclaration translateMemberInitializer( MemberInitializer minit ) throws ParseTreeException { StatementList old_body = minit.getBody(); StatementList body = translateStatementList( old_body ); minit.setBody( body ); return staticinit; } /** * Translates variable declarator node * * @return translated node * @param vardecl translating node * @exception ParseTreeException when exception occurs */ protected VariableDeclarator translateVariableDeclarator( VariableDeclarator vardecl ) throws ParseTreeException { Identifier old_declname = vardecl.getVariable(); Identifier declname = translateIdentifier( old_declname ); vardecl.setVariable( declname ); VariableInitializer old_varinit = vardecl.getInitializer(); VariableInitializer varinit = null; if(old_varinit != null){ varinit = translateVariableInitializer( old_varinit ); } vardecl.setInitializer( varinit ); return vardecl; } /** * Translates the variable initializer node * * @return translated node * @param varinit translating node * @exception ParseTreeException when exception occurs */ protected VariableInitializer translateVariableInitializer( VariableInitializer varinit ) throws ParseTreeException { if(varinit instanceof Expression){ return translateExpression( (Expression) varinit ); } if(varinit instanceof ArrayInitializer){ return translateArrayInitializer( (ArrayInitializer) varinit ); } throw new ParseTreeException( "translateVariableInitializer() : " + "illegal VariableInitializer object." ); } /** * Translates the array initializer node * * @return translated node * @param arrayinit translating node * @exception ParseTreeException when exception occurs */ protected ArrayInitializer translateArrayInitializer( ArrayInitializer arrayinit ) throws ParseTreeException { boolean same = true; ArrayInitializer ret = new ArrayInitializer(); for( int i = 0; i < arrayinit.size(); i++ ){ VariableInitializer old_vinit = arrayinit.get( i ); VariableInitializer vinit = null; if(old_vinit != null){ vinit = translateVariableInitializer( old_vinit ); } if(! eq( old_vinit, vinit )) same = false; ret.add( vinit ); } if(same){ return arrayinit; } return ret; } /** * Translates parameter list * * @return translated node * @param paramlist translating node * @exception ParseTreeException when exception occurs */ protected ParameterList translateParameterList( ParameterList paramlist ) throws ParseTreeException { boolean same = true; ParameterList ret = new ParameterList(); for( int i = 0; i < paramlist.size(); i++ ){ Parameter old_param = paramlist.get( i ); Parameter param = translateParameter( old_param ); if(! old_param.eq( param )) same = false; ret.add( param ); } if(same){ return paramlist; } return ret; } /** * Translates parameter * * @return translated node * @param param translating node * @exception ParseTreeException when exception occurs */ protected Parameter translateParameter( Parameter param ) throws ParseTreeException { ModifierList old_modiflist = param.getModifiers(); ModifierList modiflist = translateModifierList( old_modiflist ); param.setModifiers( modiflist ); TypeSpecifier old_typespec = param.getTypeSpecifier(); TypeSpecifier typespec = translateTypeSpecifier( old_typespec ); param.setTypeSpecifier( typespec ); Identifier old_name = param.getVariable(); Identifier name = translateIdentifier( old_name ); param.setVariable( name ); return param; } /** * Translates type specifier node * * @return translated node * @param typespec translating node * @exception ParseTreeException when exception occurs */ protected TypeSpecifier translateTypeSpecifier( TypeSpecifier typespec ) throws ParseTreeException { TypeName old_typename = typespec.getTypeName(); TypeName typename = translateTypeName( old_typename ); typespec.setTypeName( typename ); int dim = typespec.getDim(); typespec.setDim( dim ); return typespec; } /** * Translates class type list * * @return translated node * @param ctypelist translating node * @exception ParseTreeException when exception occurs */ protected ClassTypeList translateClassTypeList( ClassTypeList ctypelist ) throws ParseTreeException { boolean same = true; ClassTypeList ret = new ClassTypeList(); for( int i = 0; i < ctypelist.size(); i++ ){ ClassType old_ctype = ctypelist.get( i ); ClassType ctype = translateClassType( old_ctype ); if(! old_ctype.eq( ctype )) same = false; ret.add( ctype ); } if(same){ return ctypelist; } return ret; } /** * Translates type name node * * @return translated node * @param typename translating node * @exception ParseTreeException when exception occurs */ protected TypeName translateTypeName( TypeName typename ) throws ParseTreeException { if(typename instanceof PrimitiveType){ return translatePrimitiveType( (PrimitiveType) typename ); } if(typename instanceof ClassType){ return translateClassType( (ClassType) typename ); } throw new ParseTreeException( "translateTypeName() : " + "illegal TypeName object." ); } /** * Translates primitive type node * * @return translated node * @param typename translating node * @exception ParseTreeException when exception occurs */ protected PrimitiveType translatePrimitiveType( PrimitiveType ptype ) throws ParseTreeException { return ptype; } /** * Translates class type node * * @return translated node * @param typename translating node * @exception ParseTreeException when exception occurs */ protected ClassType translateClassType( ClassType ctypename ) throws ParseTreeException { QualifiedName old_pack = ctypename.getPackage(); QualifiedName pack = translateQualifiedName( old_pack ); ctypename.setPackage( pack ); Identifier old_name = ctypename.getName(); Identifier name = translateIdentifier( old_name ); ctypename.setName( name ); return ctypename; } /** * Translates qualified name * * @return translated list * @param qname translating list * @exception ParseTreeException when exception occurs */ protected QualifiedName translateQualifiedName( QualifiedName qname ) throws ParseTreeException { boolean same = true; QualifiedName ret = new QualifiedName(); for( int i = 0; i < qname.size(); i++ ){ Identifier old_ident = qname.get( i ); Identifier ident = translateIdentifier( old_ident ); if(! old_ident.eq( ident )) same = false; ret.add( ident ); } if(same){ return qname; } return ret; } /** * Translates modifier list * * @return translated list * @param modiflist translating list * @exception ParseTreeException when exception occurs */ protected ModifierList translateModifierList( ModifierList modiflist ) throws ParseTreeException { boolean same = true; ModifierList ret = new ModifierList(); for( int i = 0; i < modiflist.size(); i++ ){ Modifier old_modif = modiflist.get( i ); Modifier modif = translateModifier( old_modif ); if(! old_modif.eq( modif )) same = false; ret.add( modif ); } if(same){ return modiflist; } return ret; } /** * Translates modfier node * * @return translated node * @param modfier translating node * @exception ParseTreeException when exception occurs */ protected Modifier translateModifier( Modifier modifier ) throws ParseTreeException { return modifier; } /** * Translates block node * * @return translated node * @param block translating node * @exception ParseTreeException when exception occurs */ protected Statement translateBlock( Block block ) throws ParseTreeException { StatementList old_stmts = block.getStatements(); StatementList stmts = translateStatementList( old_stmts ); block.setStatements( stmts ); return block; } /** * Translates local variable declaration statement node. * * @return translated node * @param vardeclstate translating node * @exception ParseTreeException when exception occurs */ protected Statement translateVariableDeclaration( VariableDeclaration vardecl ) throws ParseTreeException { ModifierList old_modiflist = vardecl.getModifiers(); ModifierList modiflist = translateModifierList( old_modiflist ); vardecl.setModifiers( modiflist ); TypeSpecifier old_typespec = vardecl.getTypeSpecifier(); TypeSpecifier typespec = translateTypeSpecifier( old_typespec ); vardecl.setTypeSpecifier( typespec ); VariableDeclarator old_vd = vardecl.getVariableDeclarator(); VariableDeclarator vd = translateVariableDeclarator( old_vd ); vardecl.setVariableDeclarator( vd ); return vardecl; } /** * Translates statement list * * @return translated node * @param paramlist translating node * @exception ParseTreeException when exception occurs */ protected StatementList translateStatementList( StatementList stmtlist ) throws ParseTreeException { boolean same = true; StatementList ret = new StatementList(); for( int i = 0; i < stmtlist.size(); i++ ){ Statement old_stmt = stmtlist.get( i ); Statement stmt = translateStatement( old_stmt ); if(! old_stmt.eq( stmt )){ same = false; } ret.add( stmt ); } if(same){ return stmtlist; } return ret; } /** * Translates statement node * * @return translated node * @param statement translating node * @exception ParseTreeException when exception occurs */ protected Statement translateStatement( Statement stmt ) throws ParseTreeException { if(stmt instanceof VariableDeclaration){ return translateVariableDeclaration( (VariableDeclaration) stmt ); } if(stmt instanceof EmptyStatement){ return translateEmptyStatement( (EmptyStatement) stmt ); } if(stmt instanceof LabeledStatement){ return translateLabeldStatement( (LabeledStatement) stmt ); } if(stmt instanceof ExpressionStatement){ return translateExpressionStatement( (ExpressionStatement) stmt ); } if(isSelectionStatement( stmt )){ return translateSelectionStatement( stmt ); } if(isIterationStatement( stmt )){ return translateIterationStatement( stmt ); } if(isJumpStatement( stmt )){ return translateJumpStatement( stmt ); } if(isGuardingStatement( stmt )){ return translateGuardingStatement( stmt ); } throw new ParseTreeException( "translateStatement() : " + "illegal Statement object." ); } /** * Translates empty statement * * @return translated node * @param emptystate translating node * @exception ParseTreeException when exception occurs */ protected Statement translateEmptyStatement( EmptyStatement emptystate ) throws ParseTreeException { return emptystate; } /** * Translates labeled statement node * * @return translated node. * @param labelstate translating node. * @exception ParseTreeException when exception occurs */ protected Statement translateLabeldStatement( LabeledStatement labelstate ) throws ParseTreeException { Identifier old_label = labelstate.getLabel(); Identifier label = translateIdentifier( old_label ); labelstate.setLabel( label ); Statement old_stmt = labelstate.getStatement(); Statement stmt = translateStatement( old_stmt ); labelstate.setStatement( stmt ); return labelstate; } /** * Translates expression statement node. * * @return translated node. * @param expstate translating node. * @exception ParseTreeException when exception occurs */ protected Statement translateExpressionStatement( ExpressionStatement expstate ) throws ParseTreeException { Expression old_expr = expstate.getExpression(); Expression expr = translateExpression( old_expr ); expstate.setExpression( expr ); return expstate; } /** * Translates selection statement node * * @return translated node * @param selectstate translating ndoe * @exception ParseTreeException when exception occurs */ protected Statement translateSelectionStatement( Statement selectstate ) throws ParseTreeException { if(selectstate instanceof IfStatement){ return translateIfStatement( (IfStatement) selectstate); } if(selectstate instanceof SwitchStatement){ return translateSwitchStatement( (SwitchStatement) selectstate); } throw new ParseTreeException( "translateSelectionStatement() : " + "illegal selection statement object." ); } /** * Translates if statement node * * @return translated node * @param ifstate translating ndoe * @exception ParseTreeException when exception occurs */ protected Statement translateIfStatement( IfStatement ifstate ) throws ParseTreeException { Expression old_expr = ifstate.getExpression(); Expression expr = translateExpression( old_expr ); ifstate.setExpression( expr ); StatementList old_stmts = ifstate.getStatements(); StatementList stmts = translateStatementList( old_stmts ); ifstate.setStatements( stmts ); StatementList old_elsestmts = ifstate.getElseStatements(); StatementList elsestmts = translateStatementList( old_elsestmts ); ifstate.setElseStatements( elsestmts ); return ifstate; } /** * Translates switch statement node * * @return translated node * @param switchstate translating ndoe * @exception ParseTreeException when exception occurs */ protected Statement translateSwitchStatement( SwitchStatement switchstate ) throws ParseTreeException { Expression old_expr = switchstate.getExpression(); Expression expr = translateExpression( old_expr ); switchstate.setExpression( expr ); CaseGroupList old_cglist = switchstate.getCaseGroupList(); CaseGroupList cglist = translateCaseGroupList( old_cglist ); switchstate.setCaseGroupList( cglist ); return switchstate; } /** * Translates switch statement node * * @return translated node * @param switchstate translating ndoe * @exception ParseTreeException when exception occurs */ protected CaseGroupList translateCaseGroupList( CaseGroupList cglist ) throws ParseTreeException { boolean same = true; CaseGroupList ret = new CaseGroupList(); for( int i = 0; i < cglist.size(); i++ ){ CaseGroup old_cg = cglist.get( i ); CaseGroup cg = translateCaseGroup( old_cg ); if(! old_cg.eq( cg )) same = false; ret.add( cg ); } if(same){ return cglist; } return ret; } /** * Translates statements after case node * * @return translated node * @param switchstate translating ndoe * @exception ParseTreeException when exception occurs */ protected CaseGroup translateCaseGroup( CaseGroup cg ) throws ParseTreeException { return cg; } /** * Translates iteration node * * @return translated node * @param iterstate translating node * @exception ParseTreeException when exception occurs */ protected Statement translateIterationStatement( Statement iterstate ) throws ParseTreeException { if(iterstate instanceof WhileStatement){ return translateWhileStatement( (WhileStatement) iterstate ); } if(iterstate instanceof DoWhileStatement){ return translateDoWhileStatement( (DoWhileStatement) iterstate ); } if(iterstate instanceof ForStatement){ return translateForStatement( (ForStatement) iterstate ); } throw new ParseTreeException( "translateIterationStatement() : " + "illegal IterationStatement object." ); } /** * Translates while statement node * * @return translated node * @param whilestate translating node * @exception ParseTreeException when exception occurs */ protected Statement translateWhileStatement( WhileStatement whilestate ) throws ParseTreeException { Expression old_expr = whilestate.getExpression(); Expression expr = translateExpression( old_expr ); whilestate.setExpression( expr ); StatementList old_stmts = whilestate.getStatements(); StatementList stmts = translateStatementList( old_stmts ); whilestate.setStatements( stmts ); return whilestate; } /** * Translates the do-while statement node. * * @return translated node * @param dostate translating node * @exception ParseTreeException when exception occurs */ protected Statement translateDoWhileStatement( DoWhileStatement dostate ) throws ParseTreeException { StatementList old_stmts = dostate.getStatements(); StatementList stmts = translateStatementList( old_stmts ); dostate.setStatements( stmts ); Expression old_expr = dostate.getExpression(); Expression expr = translateExpression( old_expr ); dostate.setExpression( expr ); return dostate; } /** * Translates the for statement node. * * @return translated node * @param forstate translating node * @exception ParseTreeException when exception occurs */ protected Statement translateForStatement( ForStatement forstate ) throws ParseTreeException { List init = forstate.getInit(); Expression old_expr = forstate.getCondition(); Expression expr = null; if(old_expr != null){ expr = translateExpression( old_expr ); } forstate.setCondition( expr ); ExpressionList old_incr = forstate.getIncrement(); ExpressionList incr = translateExpressionList( old_incr ); forstate.setIncrement( incr ); StatementList old_stmts = forstate.getStatements(); StatementList stmts = translateStatementList( old_stmts ); forstate.setStatements( stmts ); return forstate; } /** * Translates jump statement node * * @return translated node * @param jumpstate translating node * @exception ParseTreeException when exception occurs */ protected Statement translateJumpStatement( Statement jumpstate ) throws ParseTreeException { if(jumpstate instanceof BreakStatement){ return translateBreakStatement( (BreakStatement) jumpstate ); } if(jumpstate instanceof ContinueStatement){ return translateContinueStatement( (ContinueStatement) jumpstate ); } if(jumpstate instanceof ReturnStatement){ return translateReturnStatement( (ReturnStatement) jumpstate ); } if(jumpstate instanceof ThrowStatement){ return translateThrowStatement( (ThrowStatement) jumpstate ); } throw new ParseTreeException( "translateJumpStatement : " + "illegal JumpStatement object." ); } /** * Translates break statement node. * * @return the translated node. * @param breakstate the translating node. * @exception ParseTreeException when exception occurs */ protected Statement translateBreakStatement( BreakStatement breakstate ) throws ParseTreeException { Identifier old_label = breakstate.getLabel(); Identifier label = null; if(old_label != null){ label = translateIdentifier( old_label ); } breakstate.setLabel( label ); return breakstate; } /** * Translates the continue statement node. * * @return the translated node. * @param continuestate the translating node. * @exception ParseTreeException when exception occurs */ protected Statement translateContinueStatement( ContinueStatement continuestate ) throws ParseTreeException { Identifier old_label = continuestate.getLabel(); Identifier label = null; if(old_label != null){ label = translateIdentifier( old_label ); } continuestate.setLabel( label ); return continuestate; } /** * Translates return statement node * * @return translated node * @param returnstate translating node * @exception ParseTreeException when exception occurs */ protected Statement translateReturnStatement( ReturnStatement returnstate ) throws ParseTreeException { Expression old_expr = returnstate.getExpression(); Expression expr = null; if(old_expr != null){ expr = translateExpression( old_expr ); } returnstate.setExpression( expr ); return returnstate; } /** * Translates throw statement node * * @return translated node * @param throwstate translating node * @exception ParseTreeException when exception occurs */ protected Statement translateThrowStatement( ThrowStatement throwstate ) throws ParseTreeException { Expression old_expr = throwstate.getExpression(); Expression expr = null; if(old_expr != null){ expr = translateExpression( old_expr ); } throwstate.setExpression( expr ); return throwstate; } /** * Translates the guarding statement node. * * @return the translated node. * @param guardstate the translating node. * @exception ParseTreeException when exception occurs */ protected Statement translateGuardingStatement( Statement guardstate ) throws ParseTreeException { if(guardstate instanceof SynchronizedStatement){ return translateSynchronizedStatement( (SynchronizedStatement) guardstate ); } if(guardstate instanceof TryStatement){ return translateTryStatement( (TryStatement) guardstate ); } if(guardstate instanceof Block){ return translateBlock( (Block) guardstate ); } throw new ParseTreeException( "translateGuardingStatement() : " + "illegal GuardingStatement object." ); } /** * Translates synchronized statement node. * * @return the translated node. * @param synctate the translating node. * @exception ParseTreeException when exception occurs */ protected Statement translateSynchronizedStatement( SynchronizedStatement syncstate ) throws ParseTreeException { Expression old_expr = syncstate.getExpression(); Expression expr = translateExpression( old_expr ); syncstate.setExpression( expr ); StatementList old_stmts = syncstate.getStatements(); StatementList stmts = translateStatementList( old_stmts ); syncstate.setStatements( stmts ); return syncstate; } /** * Translates the try statement node. * * @return the translated node. * @param trystate the translating node. * @exception ParseTreeException when exception occurs */ protected Statement translateTryStatement( TryStatement trystate ) throws ParseTreeException { StatementList old_stmts = trystate.getBody(); StatementList stmts = translateStatementList( old_stmts ); trystate.setBody( stmts ); CatchList old_catches = trystate.getCatchList(); CatchList catches = translateCatchList( old_catches ); trystate.setCatchList( catches ); StatementList old_finallie = trystate.getFinallyBody(); StatementList finallie = translateStatementList( old_finallie ); trystate.setFinallyBody( finallie ); return trystate; } /** * Translates catch list * * @return translated node * @param catchlist translating node * @exception ParseTreeException when exception occurs */ protected CatchList translateCatchList( CatchList catchlist ) throws ParseTreeException { boolean same = true; CatchList ret = new CatchList(); for( int i = 0; i < catchlist.size(); i++ ){ CatchBlock old_cb = catchlist.get( i ); CatchBlock cb = translateCatchBlock( old_cb ); if(! old_cb.eq( cb )) same = false; ret.add( cb ); } if(same){ return catchlist; } return ret; } /** * Translates the catch block node. * * @return the translated node. * @param katch the translating node. * @exception ParseTreeException when exception occurs */ protected CatchBlock translateCatchBlock( CatchBlock katch ) throws ParseTreeException { Parameter old_param = katch.getParameter(); Parameter param = translateParameter( old_param ); katch.setParameter( param ); StatementList old_stmts = katch.getBody(); StatementList stmts = translateStatementList( old_stmts ); katch.setBody( stmts ); return katch; } /** * Translates the expression list. * * @return the translated node. * @param exprlist the translating node. * @exception ParseTreeException when exception occurs */ protected ExpressionList translateExpressionList( ExpressionList exprlist ) throws ParseTreeException { boolean same = true; ExpressionList ret = new ExpressionList(); for( int i = 0; i < exprlist.size(); i++ ){ Expression old_expr = exprlist.get( i ); Expression expr = translateExpression( old_expr ); if(! old_expr.eq( expr )) same = false; ret.add( expr ); } if(same){ return exprlist; } return ret; } /** * Translates the expression node. * * @return the translated node. * @param expr the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateExpression( Expression expr ) throws ParseTreeException { if(expr instanceof UnaryExpression){ return translateUnaryExpression( (UnaryExpression) expr ); } if(expr instanceof BinaryExpression){ return translateBinaryExpression( (BinaryExpression) expr ); } if(expr instanceof InstanceofExpression){ return translateInstanceofExpression( (InstanceofExpression) expr ); } if(expr instanceof ConditionalExpression){ return translateConditionalExpression( (ConditionalExpression) expr ); } if(expr instanceof AssignmentExpression){ return translateAssignmentExpression( (AssignmentExpression) expr ); } if(expr instanceof CastExpression){ return translateCastExpression( (CastExpression) expr ); } if(isAllocationExpression( expr )){ return translateAllocationExpression( expr ); } if(expr instanceof QualifiedName){ return translateQualifiedName( (QualifiedName) expr ); } if(expr instanceof SpecialName){ return translateSpecialName( (SpecialName) expr ); } if(expr instanceof Literal){ return translateLiteral( (Literal) expr ); } if(expr instanceof ArrayAccess){ return translateArrayAccess( (ArrayAccess) expr ); } if(expr instanceof FieldAccess){ return translateFieldAccess( (FieldAccess) expr ); } if(expr instanceof MethodCall){ return translateMethodCall( (MethodCall) expr ); } throw new ParseTreeException( "translateExpression() : " + "illegal Expression object." ); } /** * Translates the literal. * * @return the translated node. * @param lit the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateLiteral( Literal lit ) throws ParseTreeException { return lit; } /** * Translates the unary expression. * * @return the translated node. * @param unary the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateUnaryExpression( UnaryExpression unary ) throws ParseTreeException { int opr = unary.getOperator(); Expression old_expr = unary.getExpression(); Expression expr = translateExpression( old_expr ); unary.setExpression( expr ); return unary; } /** * Translates the binary expression. * * @return the translated node. * @param binary the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateBinaryExpression( BinaryExpression binary ) throws ParseTreeException { Expression old_left = binary.getLeft(); Expression left = translateExpression( old_left ); binary.setLeft( left ); int opr = binary.getOperator(); Expression old_right = binary.getRight(); Expression right = translateExpression( old_right ); binary.setRight( right ); return binary; } /** * Translates the instanceof expression. * * @return the translated node. * @param insof the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateInstanceofExpression( InstanceofExpression insof ) throws ParseTreeException { Expression old_left = insof.getLeft(); Expression left = translateExpression( old_left ); insof.setLeft( left ); TypeSpecifier old_right = insof.getTypeSpecifier(); TypeSpecifier right = translateTypeSpecifier( old_right ); insof.setTypeSpecifier( right ); return insof; } /** * Translates the conditional expression. * * @return the translated node. * @param condexpr the translationg node. * @exception ParseTreeException when exception occurs */ protected Expression translateConditionalExpression( ConditionalExpression condexpr ) throws ParseTreeException { Expression old_condition = condexpr.getCondition(); Expression condition = translateExpression( old_condition ); condexpr.setCondition( condition ); Expression old_truecase = condexpr.getTrueCase(); Expression truecase = translateExpression( old_truecase ); condexpr.setTrueCase( truecase ); Expression old_falsecase = condexpr.getFalseCase(); Expression falsecase = translateExpression( old_falsecase ); condexpr.setTrueCase( falsecase ); return condexpr; } /** * Translates assignment expression node * * @return translated node * @param assignment translationg node * @exception ParseTreeException when exception occurs */ protected Expression translateAssignmentExpression( AssignmentExpression assignment ) throws ParseTreeException { Expression old_left = assignment.getLeft(); Expression left = translateExpression( old_left ); assignment.setLeft( left ); int opr = assignment.getOperator(); Expression old_right = assignment.getRight(); Expression right = translateExpression( old_right ); assignment.setRight( right ); return assignment; } /** * Translates the cast expression node. * * @return the translated node. * @param cast the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateCastExpression( CastExpression castexpr ) throws ParseTreeException { TypeSpecifier old_ts = castexpr.getTypeSpecifier(); TypeSpecifier ts = translateTypeSpecifier( old_ts ); castexpr.setTypeSpecifier( ts ); Expression old_expr = castexpr.getExpression(); Expression expr = translateExpression( old_expr ); castexpr.setExpression( expr ); return castexpr; } /** * Translates the allocation expression node. * * @return the translated node. * @param alloc the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateAllocationExpression( Expression alloc ) throws ParseTreeException { if(alloc instanceof AllocationExpression){ return translateAllocationExpression( (AllocationExpression) alloc ); } if(alloc instanceof ArrayAllocationExpression){ return translateArrayAllocationExpression( (ArrayAllocationExpression) alloc ); } throw new ParseTreeException( "translateAllocationExpression() : " + "illegal AllocationExpression object." ); } /** * Translates the construct expression node. * * @return the translated node. * @param construct the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateAllocationExpression( AllocationExpression construct ) throws ParseTreeException { ClassType old_typename = construct.getClassType(); ClassType typename = translateClassType( old_typename ); construct.setClassType( typename ); ExpressionList old_exprs = construct.getArguments(); ExpressionList exprs = translateExpressionList( old_exprs ); construct.setArguments( exprs ); MemberDeclarationList old_body = construct.getClassBody(); MemberDeclarationList body = null; if(old_body != null){ body = translateMemberDeclarationList( old_body ); } return construct; } /** * Translates the array allocation expression node. * * @return the translated node. * @param arrayalloc the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateArrayAllocationExpression( ArrayAllocationExpression arrayalloc ) throws ParseTreeException { TypeName old_typename = arrayalloc.getTypeName(); TypeName typename = translateTypeName( old_typename ); arrayalloc.setTypeName( typename ); DimExprList old_dims = arrayalloc.getDimExprList(); DimExprList dims = translateDimExprList( old_dims ); arrayalloc.setDimExprList( dims ); ArrayInitializer old_ainit = arrayalloc.getInitializer(); ArrayInitializer ainit = null; if(old_ainit != null){ ainit = translateArrayInitializer( old_ainit ); } arrayalloc.setInitializer( ainit ); return arrayalloc; } /** * Translates the array dimension expr list. * * @return translated node * @param construct translating node * @exception ParseTreeException when exception occurs */ protected DimExprList translateDimExprList( DimExprList delist ) throws ParseTreeException { boolean same = true; DimExprList ret = new DimExprList(); for( int i = 0; i < delist.size(); i++ ){ Expression old_expr = delist.get( i ); Expression expr = null; if(old_expr != null){ expr = translateExpression( old_expr ); } if(! eq( old_expr, expr )) same = false; ret.add( expr ); } if(same){ return delist; } return ret; } /** * Translates the array access node. * * @return the translated node. * @param arrayaccess the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateArrayAccess( ArrayAccess arrayaccess ) throws ParseTreeException { Expression old_expr = arrayaccess.getExpression(); Expression expr = translateExpression( old_expr ); arrayaccess.setExpression( expr ); DimExprList old_dims = arrayaccess.getDimExprList(); DimExprList dims = translateDimExprList( old_dims ); arrayaccess.setDimExprList( dims ); return arrayaccess; } /** * Translates the field access node. * * @return the translated node. * @param fieldaccess the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateFieldAccess( FieldAccess fieldaccess ) throws ParseTreeException { Expression old_expr = fieldaccess.getExpression(); Expression expr = null; if(old_expr != null){ expr = translateExpression( old_expr ); } fieldaccess.setExpression( expr ); Identifier old_name = fieldaccess.getName(); Identifier name = translateIdentifier( old_name ); fieldaccess.setName( name ); return fieldaccess; } /** * Translates the mehtod call node. * * @return the translated node. * @param methodcall the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateMethodCall( MethodCall methodcall ) throws ParseTreeException { Expression old_expr = methodcall.getExpression(); Expression expr = null; if(old_expr != null){ expr = translateExpression( old_expr ); } methodcall.setExpression( expr ); Identifier old_name = methodcall.getName(); Identifier name = translateIdentifier( old_name ); methodcall.setName( name ); ExpressionList old_args = methodcall.getArguments(); ExpressionList args = null; args = translateExpressionList( old_args ); methodcall.setArguments( args ); return methodcall; } /** * Translates the special name node. * * @return the translated node. * @param special the translating node. * @exception ParseTreeException when exception occurs */ protected Expression translateSpecialName( SpecialName special ) throws ParseTreeException { return special; } /** * Translates the identifier node. * * @return the translated node. * @param id the translating node. * @exception ParseTreeException when exception occurs */ protected Identifier translateIdentifier( Identifier id ) throws ParseTreeException { return id; } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 1467 | Julian Hyde |
saffron: First saffron check-in; incorporate my changes to openjava. |
||
#1 | 1461 | Julian Hyde |
saffron: First check in. Just documents, and the unmodified OpenJava 20001010 source files. |