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 org.fusesource.restygwt.client.JsonEncoderDecoder;
022
023 import com.google.gwt.core.ext.GeneratorContext;
024 import com.google.gwt.core.ext.TreeLogger;
025 import com.google.gwt.core.ext.UnableToCompleteException;
026 import com.google.gwt.core.ext.typeinfo.JClassType;
027 import com.google.gwt.core.ext.typeinfo.JParameterizedType;
028 import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
029
030 /**
031 *
032 * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
033 */
034 public class ExtendedJsonEncoderDecoderClassCreator extends BaseSourceCreator {
035
036 private static final String JSON_ENCODER_DECODER = JsonEncoderDecoder.class.getName();
037 private static final String JSON_ENCODER_SUFFIX = "_Generated_ExtendedJsonEncoderDecoder_";
038
039 public ExtendedJsonEncoderDecoderClassCreator(TreeLogger logger, GeneratorContext context, JClassType source) throws UnableToCompleteException {
040 super(logger, context, source, JSON_ENCODER_SUFFIX);
041 }
042
043 protected ClassSourceFileComposerFactory createComposerFactory() throws UnableToCompleteException {
044 ClassSourceFileComposerFactory composerFactory = new ClassSourceFileComposerFactory(packageName, shortName);
045 JClassType encodedType = getEncodedType(logger, context, source);
046 JsonEncoderDecoderClassCreator generator = new JsonEncoderDecoderClassCreator(logger, context, encodedType);
047 composerFactory.setSuperclass(generator.create());
048 composerFactory.addImplementedInterface(source.getQualifiedSourceName());
049 return composerFactory;
050 }
051
052 private JClassType getEncodedType(TreeLogger logger, GeneratorContext context, JClassType type) throws UnableToCompleteException {
053 JClassType intf = type.isInterface();
054 if (intf == null) {
055 error("Expected " + type + " to be an interface.");
056 throw new UnableToCompleteException();
057 }
058
059 JClassType[] intfs = intf.getImplementedInterfaces();
060 for (JClassType t : intfs) {
061 info("checking: " + t.getQualifiedSourceName() + ", type: " + t.getClass());
062 if (t.getQualifiedSourceName().equals(JSON_ENCODER_DECODER)) {
063
064 JParameterizedType genericType = t.isParameterized();
065 if (genericType == null) {
066 error("Expected the " + JSON_ENCODER_DECODER + " declaration to specify a parameterized type.");
067 throw new UnableToCompleteException();
068 }
069 JClassType[] typeParameters = genericType.getTypeArgs();
070 if (typeParameters == null || typeParameters.length != 1) {
071 error("Expected the " + JSON_ENCODER_DECODER + " declaration to specify 1 parameterized type.");
072 throw new UnableToCompleteException();
073 }
074 return typeParameters[0].isClass();
075 }
076 }
077 error("Expected " + type + " to extend the " + JSON_ENCODER_DECODER + " interface.");
078 throw new UnableToCompleteException();
079 }
080
081 @Override
082 protected void generate() throws UnableToCompleteException {
083 }
084
085 }