001/*
002 * Copyright (C) 2012 Facebook, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may
005 * not use this file except in compliance with the License. You may obtain
006 * a copy of 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
014 * under the License.
015 */
016package com.facebook.swift.codec.metadata;
017
018import javax.annotation.concurrent.Immutable;
019
020import java.lang.reflect.Method;
021import java.lang.reflect.Type;
022
023import com.google.common.reflect.TypeToken;
024
025import static com.google.common.base.Preconditions.checkArgument;
026import static com.google.common.base.Preconditions.checkNotNull;
027
028@Immutable
029public class ThriftMethodExtractor implements ThriftExtraction
030{
031    private final short id;
032    private final String name;
033    private final Method method;
034    private final FieldKind fieldKind;
035    private final Class<?> type;
036
037    public ThriftMethodExtractor(
038            short fieldId, String fieldName, FieldKind fieldKind, Method method, Type fieldType)
039    {
040        this.name = checkNotNull(fieldName, "name is null");
041        this.method = checkNotNull(method, "method is null");
042        this.fieldKind = checkNotNull(fieldKind, "fieldKind is null");
043        this.type = TypeToken.of(checkNotNull(fieldType, "structType is null")).getRawType();
044
045        switch (fieldKind) {
046            case THRIFT_FIELD:
047                checkArgument(fieldId >= 0, "fieldId is negative");
048                break;
049            case THRIFT_UNION_ID:
050                checkArgument (fieldId == Short.MIN_VALUE, "fieldId must be Short.MIN_VALUE for thrift_union_id");
051                break;
052        }
053
054        this.id = fieldId;
055    }
056
057    @Override
058    public FieldKind getFieldKind()
059    {
060        return fieldKind;
061    }
062
063    @Override
064    public short getId()
065    {
066        return id;
067    }
068
069    @Override
070    public String getName()
071    {
072        return name;
073    }
074
075    public Method getMethod()
076    {
077        return method;
078    }
079
080    public Class<?> getType()
081    {
082        return type;
083    }
084
085    public boolean isGeneric()
086    {
087        return getMethod().getReturnType() != getMethod().getGenericReturnType();
088    }
089
090    @Override
091    public String toString()
092    {
093        final StringBuilder sb = new StringBuilder();
094        sb.append("ThriftMethodExtractor");
095        sb.append("{id=").append(id);
096        sb.append(", name='").append(name).append('\'');
097        sb.append(", fieldKind=").append(fieldKind);
098        sb.append(", method=").append(method);
099        sb.append(", type=").append(type);
100        sb.append('}');
101        return sb.toString();
102    }
103}