001    /**
002     * Copyright (C) 2009-2011 the original author or authors.
003     * See the notice.md file distributed with this work for additional
004     * information regarding copyright ownership.
005     *
006     * Licensed under the Apache License, Version 2.0 (the "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.fusesource.restygwt.rebind;
020    
021    import com.google.gwt.core.ext.Generator;
022    import com.google.gwt.core.ext.GeneratorContext;
023    import com.google.gwt.core.ext.TreeLogger;
024    import com.google.gwt.core.ext.UnableToCompleteException;
025    import com.google.gwt.core.ext.typeinfo.JClassType;
026    
027    /**
028     * 
029     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
030     */
031    public class JsonEncoderDecoderGenerator extends Generator {
032    
033        @Override
034        public String generate(TreeLogger logger, GeneratorContext context, String source) throws UnableToCompleteException {
035            try {
036                JClassType type = find(logger, context, source);
037                ExtendedJsonEncoderDecoderClassCreator generator = new ExtendedJsonEncoderDecoderClassCreator(logger, context, type);
038                return generator.create();
039            } finally {
040                BaseSourceCreator.clearGeneratedClasses();
041            }
042        }
043    
044        static JClassType find(TreeLogger logger, GeneratorContext context, String type) throws UnableToCompleteException {
045            JClassType rc = context.getTypeOracle().findType(type);
046            if (rc == null) {
047                logger.log(TreeLogger.ERROR, "TypeOracle could not find " + type);
048                throw new UnableToCompleteException();
049            }
050            return rc;
051        }
052    
053    }