001 /*
002 * Copyright 2006 Google Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005 * use this file except in compliance with the License. You may obtain a copy of
006 * the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013 * License for the specific language governing permissions and limitations under
014 * the License.
015 */
016 package com.google.gwt.user.rebind;
017
018 import com.google.gwt.core.ext.GeneratorContext;
019
020 import java.io.PrintWriter;
021 import java.util.HashSet;
022 import java.util.Set;
023
024 public class ClassSourceFileComposerFactory {
025
026 private String fCreatedPackage;
027 private String fCreatedClassShortName;
028
029 private String fSuperclassName;
030 private Set fInterfaceNames = new HashSet();
031 private Set fImports = new HashSet();
032
033 public ClassSourceFileComposerFactory(String createdPackage,
034 String createdClassShortName) {
035 fCreatedPackage = createdPackage;
036 fCreatedClassShortName = createdClassShortName;
037 }
038
039 /**
040 * Creates an implementation of {@link SourceWriter} that can be used to write
041 * the innards of a class. Note that the subsequent changes to this factory do
042 * not affect the returned instance.
043 *
044 * @throws RuntimeException If the settings on this factory are inconsistent
045 * or invalid
046 */
047 public SourceWriter createSourceWriter(GeneratorContext ctx, PrintWriter printWriter) {
048 return new ClassSourceFileComposer(ctx, printWriter, getCreatedPackage(),
049 getCreatedClassShortName(), getSuperclassName(), getInterfaceNames(),
050 getImports());
051 }
052
053 public String[] getInterfaceNames() {
054 return (String[]) fInterfaceNames
055 .toArray(new String[fInterfaceNames.size()]);
056 }
057
058 private String[] getImports() {
059 return (String[]) fImports
060 .toArray(new String[fImports.size()]);
061 }
062
063 public String getSuperclassName() {
064 return fSuperclassName;
065 }
066
067 public String getCreatedClassShortName() {
068 return fCreatedClassShortName;
069 }
070
071 public String getCreatedPackage() {
072 return fCreatedPackage;
073 }
074
075 public String getCreatedClassName() {
076 return getCreatedPackage() + "." + getCreatedClassShortName();
077 }
078
079 public void addImplementedInterface(String intfName) {
080 fInterfaceNames.add(intfName);
081 }
082
083 public void addImplementedInterface(Class intf) {
084 addImplementedInterface(makeSourceCompatibleName(intf));
085 }
086
087 public void addImport(String typeName) {
088 fImports.add(typeName);
089 }
090
091 public void addImport(Class type) {
092 addImport(makeSourceCompatibleName(type));
093 }
094
095 public void setSuperclass(Class superclass) {
096 setSuperclass(makeSourceCompatibleName(superclass));
097 }
098
099 public void setSuperclass(String superclassName) {
100 fSuperclassName = superclassName;
101 }
102
103 private String makeSourceCompatibleName(Class c) {
104 if (c.isArray())
105 return makeSourceCompatibleName(c.getComponentType()) + "[]";
106 else if (c.isPrimitive())
107 return c.getName();
108 else
109 return c.getName().replace('$', '.');
110 }
111 }