1 /* 2 * Copyright the original author or authors. 3 * 4 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.mozilla.org/MPL/MPL-1.1.html 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import org.as2lib.env.reflect.ClassInfo; 18 import org.as2lib.env.reflect.MethodInfo; 19 20 /** 21 * {@code ConstructorInfo} represents the constrcutor of a class. 22 * 23 * <p>The name of a constructor is always {@code "new"}. This name can be obtained 24 * through the constant {@link #NAME}. 25 * 26 * <p>Constructors are also not static. 27 * 28 * @author Simon Wacker 29 */ 30 class org.as2lib.env.reflect.ConstructorInfo extends MethodInfo { 31 32 /** The name of constructors. */ 33 public static var NAME:String = "new"; 34 35 /** 36 * Constructs a new {@code ConstructorInfo} instance. 37 * 38 * <p>If {@code constructor} is not specified, what means {@code undefined}, it 39 * will be resolved at run-time everytime requested. 40 * 41 * @param declaringClass the class that declares the {@code constructor} 42 * @param constructor (optional) the concrete constructor 43 */ 44 public function ConstructorInfo(declaringClass:ClassInfo, constructor:Function) { 45 //super (NAME, constructor, declaringClass, false); 46 // there is a cyclic import: ClassInfo imports ConstructorInfo and ConstructorInfo ClassInfo 47 this.__proto__.__proto__ = MethodInfo.prototype; 48 this.name = NAME; 49 this.method = constructor; 50 this.declaringType = declaringClass; 51 this.staticFlag = false; 52 } 53 54 /** 55 * Returns the concrete constructor this instance represents. 56 * 57 * <p>If the concrete constructor was not specified on construction it will be 58 * resolved at run-time by this method everytime asked for. The returned 59 * constructor is thus always the current constructor of the declaring type. 60 * Resolving the class's constructor at run-time does only work if the declaring 61 * type returns a not-{@code null} package and a not-{@code null} name. If these 62 * two are {@code null} or {@code undefined} the function returned by the 63 * {@code getType} method of the declaring type is returned. 64 * 65 * @return the concrete constructor 66 */ 67 public function getMethod(Void):Function { 68 if (method !== undefined) { 69 return method; 70 } 71 if (declaringType.getPackage().getPackage() == null 72 || declaringType.getName() == null) { 73 return declaringType.getType(); 74 } 75 return declaringType.getPackage().getPackage()[declaringType.getName()]; 76 } 77 78 /** 79 * Returns a {@link ConstructorInfo} instance that reflects the current state of 80 * this constructor info. 81 * 82 * @return a snapshot of this constructor info 83 */ 84 public function snapshot(Void):MethodInfo { 85 return new ConstructorInfo(ClassInfo(declaringType), getMethod()); 86 } 87 88 }