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.Generator;
019: import com.google.gwt.core.ext.TreeLogger;
020: import com.google.gwt.core.ext.UnableToCompleteException;
021: import com.google.gwt.core.ext.typeinfo.JPrimitiveType;
022:
023: /**
024: * Super class for AbstractMethod and AbstractClass creators. The primary
025: * purpose is to pull up helper methods for logging and printing.
026: */
027: public class AbstractSourceCreator {
028:
029: /**
030: * Creates a branch for the treelogger.
031: * @param logger
032: * @param message
033: * @return treelogger
034: */
035: protected static TreeLogger branch(TreeLogger logger, String message) {
036: return logger.branch(TreeLogger.TRACE, message, null);
037: }
038:
039: /**
040: * Convenience method to use TreeLogger error pattern.
041: * @param logger logger to print to
042: * @param msg msg
043: * @return the exception to throw
044: */
045: protected static UnableToCompleteException error(TreeLogger logger,
046: String msg) {
047: logger.log(TreeLogger.ERROR, msg, null);
048: return new UnableToCompleteException();
049: }
050:
051: /**
052: * Convenience method to use TreeLogger error pattern.
053: * @param logger logger to print to
054: * @param msg msg
055: * @return the exception to throw
056: */
057: protected static UnableToCompleteException error(TreeLogger logger,
058: String msg, Throwable cause) {
059: logger.log(TreeLogger.ERROR, msg, cause);
060: return new UnableToCompleteException();
061: }
062:
063: /**
064: * Convenience method to use TreeLogger error pattern.
065: * @param logger logger to print to
066: * @param e throwable
067: * @return th exception to throw
068: */
069: protected static UnableToCompleteException error(TreeLogger logger,
070: Throwable e) {
071: logger.log(TreeLogger.ERROR, e.getMessage(), e);
072: return new UnableToCompleteException();
073: }
074:
075: /**
076: * Returns the String represention of the java type for a primitive for
077: * example int/Integer, float/Float.
078: *
079: * @param type
080: * @return the string representation
081: */
082: protected static String getJavaObjectTypeFor(JPrimitiveType type) {
083: if (type == JPrimitiveType.INT) {
084: return "Integer";
085: } else {
086: String s = type.getSimpleSourceName();
087: return s.substring(0, 1).toUpperCase() + s.substring(1);
088: }
089: }
090:
091: /**
092: * Helper method used to wrap a string constant with quotes. Must use to
093: * enable string escaping.
094: *
095: * @param wrapMe String to wrap
096: * @return wrapped String
097: */
098: protected static String wrap(String wrapMe) {
099: return "\"" + Generator.escape(wrapMe) + "\"";
100: }
101: }
|