Java tutorial
/* * Copyright 2011 MOPAS(Ministry of Public Administration and Security). * * 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 egovframework.mgt.fit.library.parser.visitor; import org.eclipse.jdt.core.dom.ASTNode; import org.eclipse.jdt.core.dom.ASTVisitor; import org.eclipse.jdt.core.dom.Block; import org.eclipse.jdt.core.dom.FieldDeclaration; import org.eclipse.jdt.core.dom.MethodDeclaration; import org.eclipse.jdt.core.dom.Statement; import org.eclipse.jdt.core.dom.VariableDeclaration; import egovframework.mgt.fit.library.unit.code.AbstractStatement; import egovframework.mgt.fit.library.unit.code.JavaClass; import egovframework.mgt.fit.library.unit.code.Method; import egovframework.mgt.fit.library.unit.code.Project; import egovframework.mgt.fit.library.unit.code.StatementLine; import egovframework.mgt.fit.library.unit.code.StatementList; import egovframework.mgt.fit.library.unit.code.Variable; import egovframework.mgt.fit.library.util.ParsingHelper; import egovframework.mgt.fit.library.util.StringHelper; /** * ? ?? ?? ? */ public class ClassParsingVisitor extends ASTVisitor { /** * ? ? */ private Project project; /** * ? ? ? */ private JavaClass javaClass; /** * ? ? ? ?. * @param project ? ? * @param jc ? ? ? */ public ClassParsingVisitor(Project project, JavaClass jc) { this.project = project; this.javaClass = jc; } /** * . * @return */ @Override public boolean visit(FieldDeclaration node) { if (node.getNodeType() == ASTNode.FIELD_DECLARATION) { for (Object obj : node.fragments()) { if (obj instanceof VariableDeclaration) { VariableDeclaration vd = (VariableDeclaration) obj; Variable field = new Variable(); field.setModifier(node.getModifiers()); field.setType(project.resolveClass(ParsingHelper.getFullNameWithSimpleName( node.getType().toString(), javaClass.getPackage(), javaClass.getImports()))); field.setName(vd.getName().getFullyQualifiedName()); field.setValue(StringHelper.safeToString(vd.getInitializer())); node.accept(new AnnotationParsingVisitor(field, ASTNode.FIELD_DECLARATION)); field.setNode(vd); javaClass.addField(field); } } } return super.visit(node); } /** * . * @return */ @Override public boolean visit(MethodDeclaration node) { Method m = javaClass.getMethod(node.getStartPosition()); try { if (node.getReturnType2() != null) { m.setReturnType(project.resolveClass(ParsingHelper.getFullNameWithSimpleName( node.getReturnType2().toString(), javaClass.getPackage(), javaClass.getImports()))); } } catch (NullPointerException e) { } return super.visit(node); } /** * ? . * @param node * @return ? */ private StatementList<AbstractStatement> getStatements(Block node) { StatementList<AbstractStatement> statements = new StatementList<AbstractStatement>(); for (Object obj : node.statements()) { if (obj instanceof Statement) { Statement s = (Statement) obj; if (obj instanceof Block) { statements.addStatement(getStatements((Block) obj)); } else { StatementLine sl = new StatementLine(); sl.setStatementType(s.getNodeType()); sl.setStatement(s.toString()); statements.addStatement(sl); } } } return statements; } }