Package br.com.objectos.way.code

Source Code of br.com.objectos.way.code.TypeDeclarationWriter

/*
* Copyright 2014 Objectos, Fábrica de Software LTDA.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package br.com.objectos.way.code;

import static com.google.common.collect.Sets.newLinkedHashSet;

import java.util.Set;

import org.eclipse.jdt.core.dom.AST;
import org.eclipse.jdt.core.dom.FieldDeclaration;
import org.eclipse.jdt.core.dom.MethodDeclaration;
import org.eclipse.jdt.core.dom.SimpleName;
import org.eclipse.jdt.core.dom.Type;
import org.eclipse.jdt.core.dom.TypeDeclaration;

import com.google.common.base.Optional;

/**
* @author marcio.endo@objectos.com.br (Marcio Endo)
*/
@SuppressWarnings("unchecked")
public class TypeDeclarationWriter {

  private final AST ast;
  private final TypeDeclaration type;

  private final Set<ImportInfo> importInfoSet = newLinkedHashSet();

  TypeDeclarationWriter(TypeDeclaration type) {
    ast = type.getAST();
    this.type = type;
  }

  public MethodDeclarationWriter addConstructor() {
    MethodDeclaration method = ast.newMethodDeclaration();
    method.setConstructor(true);
    type.bodyDeclarations().add(method);

    SimpleName name = type.getName();
    method.setName(ast.newSimpleName(name.getIdentifier()));

    return new MethodDeclarationWriter(method);
  }

  public FieldDeclarationWriter addFieldInfo(FieldInfo fieldInfo) {
    Optional<ImportInfo> maybeImport = fieldInfo.toImportInfo();
    if (maybeImport.isPresent()) {
      importInfoSet.add(maybeImport.get());
    }

    FieldDeclaration field = fieldInfo.toFieldDeclaration(ast);
    type.bodyDeclarations().add(field);

    return new FieldDeclarationWriter(field);
  }

  public MethodDeclarationWriter addMethodInfo(MethodInfo methodInfo) {
    Set<ImportInfo> thisSet = methodInfo.toImportInfoSet();
    importInfoSet.addAll(thisSet);

    MethodDeclaration method = methodInfo.toMethodDeclaration(ast);
    type.bodyDeclarations().add(method);

    return new MethodDeclarationWriter(method);
  }

  public TypeDeclarationWriter implement(SimpleTypeInfo entity) {
    Type ifaceType = entity.toType(ast);
    type.superInterfaceTypes().add(ifaceType);
    return this;
  }

  public TypeDeclarationWriter implement(InterfaceInfo iface) {
    Type ifaceType = iface.toType(ast);
    type.superInterfaceTypes().add(ifaceType);
    return this;
  }

  public TypeDeclarationWriter namedAfter(SimpleTypeInfo typeInfo, String suffix) {
    SimpleName name = typeInfo.newNameSuffix(ast, suffix);
    type.setName(name);
    return this;
  }

  public TypeDeclarationWriter namedAfter(TypeInfo typeInfo, String suffix) {
    SimpleName name = typeInfo.toSimpleNameSuffix(ast, suffix);
    type.setName(name);
    return this;
  }

  public TypeDeclarationWriter publicAccess() {
    AccessInfo.PUBLIC.writeTo(type);
    return this;
  }

  public TypeDeclarationWriter writeClass() {
    return this;
  }

  String toCompilationUnitName() {
    return String.format("%s.java", type.getName());
  }

  Set<ImportInfo> toImportInfoSet() {
    return importInfoSet;
  }

}
TOP

Related Classes of br.com.objectos.way.code.TypeDeclarationWriter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.