Java tutorial
/*************************************************************************** * Copyright (C) 2013 by Eduardo Rodriguez Lorenzo * * edu.tabula@gmail.com * * http://www.cronopista.com * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ package com.cronopista.mapme.handler; import java.io.PrintWriter; import java.io.StringWriter; import java.util.ArrayList; import java.util.Iterator; import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.dom.AST; import org.eclipse.jdt.core.dom.ASTParser; import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jdt.core.dom.ITypeBinding; import org.eclipse.jdt.core.dom.IVariableBinding; import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.swt.widgets.Shell; import org.eclipse.ui.handlers.HandlerUtil; import com.cronopista.mapme.generator.junit.JUnitClass; import com.cronopista.mapme.generator.mainclass.ClassMapper; import com.cronopista.mapme.model.ClassToMap; import com.cronopista.mapme.model.Field; import com.cronopista.mapme.util.TypeUtil; /** * Entry class for the plugin. It creates the top level entities: package and * class declarations and two main methods. * * * @author edu * */ @SuppressWarnings({ "rawtypes", "unchecked" }) public class Map extends AbstractHandler { @Override public Object execute(ExecutionEvent event) throws ExecutionException { Shell shell = HandlerUtil.getActiveShell(event); boolean force = event.getCommand().getId().equals("com.cronopista.mapme.mapForce"); ISelection sel = HandlerUtil.getActiveMenuSelection(event); IStructuredSelection selection = (IStructuredSelection) sel; // sanity check if (selection.isEmpty()) { MessageDialog.openInformation(shell, "Info", "Select two Java files!"); } else { ArrayList sels = new ArrayList(); Iterator iterator = selection.iterator(); while (iterator.hasNext()) { sels.add(iterator.next()); } if (sels.size() != 2 || !(sels.get(0) instanceof ICompilationUnit) || !(sels.get(1) instanceof ICompilationUnit)) { MessageDialog.openInformation(shell, "Info", "Select two Java files!"); } else { // start actual mapping mapClasses((ICompilationUnit) sels.get(0), (ICompilationUnit) sels.get(1), force); } } return null; } private void mapClasses(ICompilationUnit from, ICompilationUnit To, boolean force) { try { IType tFrom = from.getTypes()[0]; IType tTo = To.getTypes()[0]; CompilationUnit comUnitFrom = parse(tFrom.getCompilationUnit()); CompilationUnit comUnitTo = parse(tTo.getCompilationUnit()); ITypeBinding bindingFrom = ((TypeDeclaration) comUnitFrom.types().get(0)).resolveBinding(); ITypeBinding bindingTo = ((TypeDeclaration) comUnitTo.types().get(0)).resolveBinding(); // save ClassToMap classFrom = prepareClass(bindingFrom); ClassToMap classTo = prepareClass(bindingTo); if (force) { classTo = classFrom; } // MAPEADOR ClassMapper classMapper = new ClassMapper(classFrom, classTo); tFrom.getPackageFragment().createCompilationUnit(classMapper.getClassName() + ".java", classMapper.getClassText(), true, null); // JUNIT // // JUnitClass jUnitClass = new JUnitClass(classFrom, classTo); // tFrom.getPackageFragment().createCompilationUnit( // classMapper.getClassName() + "Test.java", // jUnitClass.getClassText(), true, null); } catch (Exception e) { ConsoleWriter.write("An error ocurred: " + stackTraceToString(e)); } } private ClassToMap prepareClass(ITypeBinding a) { ClassToMap res = new ClassToMap(); res.setName(a.getName()); res.setPackageName(a.getPackage().getName()); res.setFields(new ArrayList<Field>()); for (IVariableBinding field : a.getDeclaredFields()) { if (!field.toString().contains("static")) { ITypeBinding inEclipseType = field.getType(); Field f = new Field(); f.setBasicType(TypeUtil.resolveType(inEclipseType.isPrimitive(), inEclipseType.getName(), inEclipseType.getQualifiedName(), inEclipseType.getTypeDeclaration().getQualifiedName())); if (f.getBasicType() != 0) { f.setSize(TypeUtil.getSize(inEclipseType.getName())); f.setName(field.getName()); f.setShortTypeName(inEclipseType.getName()); f.setFullTypeName(inEclipseType.getQualifiedName()); if (TypeUtil.isObject(f)) { f.setClassToMap(prepareClass(inEclipseType)); } if (TypeUtil.isCollection(f)) { ITypeBinding colType = inEclipseType.getTypeArguments()[0]; f.setClassToMap(prepareClass(colType)); f.setFullCollectionListType(inEclipseType.getTypeDeclaration().getQualifiedName()); f.setShortCollectionListType(inEclipseType.getTypeDeclaration().getName()); f.setCollectionType(new Field()); f.getCollectionType() .setBasicType(TypeUtil.resolveType(colType.isPrimitive(), colType.getName(), colType.getQualifiedName(), colType.getTypeDeclaration().getQualifiedName())); f.getCollectionType().setSize(TypeUtil.getSize(colType.getName())); f.getCollectionType().setShortTypeName(colType.getName()); f.getCollectionType().setFullTypeName(colType.getQualifiedName()); } res.getFields().add(f); } } } return res; } private static CompilationUnit parse(ICompilationUnit unit) { ASTParser parser = ASTParser.newParser(AST.JLS4); parser.setKind(ASTParser.K_COMPILATION_UNIT); parser.setSource(unit); parser.setResolveBindings(true); return (CompilationUnit) parser.createAST(null); // parse } private String stackTraceToString(Throwable t) { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); t.printStackTrace(pw); return sw.toString(); } }