KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jjs > impl > ReplaceRebinds


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.jjs.impl;
17
18 import com.google.gwt.dev.jjs.ast.Context;
19 import com.google.gwt.dev.jjs.ast.JClassLiteral;
20 import com.google.gwt.dev.jjs.ast.JClassType;
21 import com.google.gwt.dev.jjs.ast.JExpression;
22 import com.google.gwt.dev.jjs.ast.JMethod;
23 import com.google.gwt.dev.jjs.ast.JMethodCall;
24 import com.google.gwt.dev.jjs.ast.JModVisitor;
25 import com.google.gwt.dev.jjs.ast.JNewInstance;
26 import com.google.gwt.dev.jjs.ast.JProgram;
27
28 /**
29  * Replaces any "GWT.create()" calls with a new expression for the actual result
30  * of the deferred binding decision.
31  */

32 public class ReplaceRebinds {
33
34   private class RebindVisitor extends JModVisitor {
35
36     // @Override
37
public void endVisit(JMethodCall x, Context ctx) {
38       JMethod method = x.getTarget();
39       if (method == program.getRebindCreateMethod()) {
40         assert (x.getArgs().size() == 1);
41         JExpression arg = (JExpression) x.getArgs().get(0);
42         assert (arg instanceof JClassLiteral);
43         JClassLiteral classLiteral = (JClassLiteral) arg;
44         JClassType classType = program.rebind(classLiteral.getRefType());
45
46         /*
47          * Find the appropriate (noArg) constructor. In our AST, constructors
48          * are instance methods that should be qualified with a new expression.
49          */

50         JMethod noArgCtor = null;
51         for (int i = 0; i < classType.methods.size(); ++i) {
52           JMethod ctor = (JMethod) classType.methods.get(i);
53           if (ctor.getName().equals(classType.getShortName())) {
54             if (ctor.params.size() == 0) {
55               noArgCtor = ctor;
56             }
57           }
58         }
59         assert (noArgCtor != null);
60         // Call it, using a new expression as a qualifier
61
JNewInstance newInstance = new JNewInstance(program, x.getSourceInfo(),
62             classType);
63         JMethodCall call = new JMethodCall(program, x.getSourceInfo(),
64             newInstance, noArgCtor);
65         ctx.replaceMe(call);
66       }
67     }
68   }
69
70   public static boolean exec(JProgram program) {
71     return new ReplaceRebinds(program).execImpl();
72   }
73
74   private final JProgram program;
75
76   private ReplaceRebinds(JProgram program) {
77     this.program = program;
78   }
79
80   private boolean execImpl() {
81     RebindVisitor rebinder = new RebindVisitor();
82     rebinder.accept(program);
83     return rebinder.didChange();
84   }
85
86 }
87
Popular Tags