Java tutorial
/* * Copyright (C) 2012 NS Solutions Corporation * * 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 com.htmlhifive.tools.codeassist.core.proposal.checker; import java.util.regex.Pattern; import org.apache.commons.lang.StringUtils; import org.eclipse.wst.jsdt.core.IJavaScriptProject; import org.eclipse.wst.jsdt.core.IJavaScriptUnit; import org.eclipse.wst.jsdt.core.JavaScriptModelException; import org.eclipse.wst.jsdt.core.ast.IASTNode; import org.eclipse.wst.jsdt.internal.codeassist.complete.CompletionOnMemberAccess; import org.eclipse.wst.jsdt.internal.compiler.ast.CompilationUnitDeclaration; import com.htmlhifive.tools.codeassist.core.config.bean.ObjectLiteralBean; import com.htmlhifive.tools.codeassist.core.logger.H5CodeAssistPluginLogger; import com.htmlhifive.tools.codeassist.core.logger.H5CodeAssistPluginLoggerFactory; import com.htmlhifive.tools.codeassist.core.messages.Messages; import com.htmlhifive.tools.codeassist.core.proposal.build.CodeBuilderType; import com.htmlhifive.tools.codeassist.core.proposal.collector.NodeCollector; import com.htmlhifive.tools.codeassist.core.proposal.collector.NodeCollectorFactory; /** * ???.<br> * (??) * * @author NS Solutions Corporation * */ @SuppressWarnings("restriction") abstract class AbstractSuffixProposalChecker extends AbstractObjectProposalChecker { /** * . */ private static H5CodeAssistPluginLogger logger = H5CodeAssistPluginLoggerFactory .getLogger(AbstractSuffixProposalChecker.class); /** * ?. */ private String codeAssistStr; /** * . */ private ObjectLiteralBean bean; /** * . */ private CompletionOnMemberAccess memberAccess; /** * . * * @param unit * @param project . * @param bean . * @throws JavaScriptModelException ?. */ public AbstractSuffixProposalChecker(IJavaScriptUnit unit, IJavaScriptProject project, ObjectLiteralBean bean) throws JavaScriptModelException { super(unit, project); this.bean = bean; } @Override protected boolean doCheckCodeAssist(CompilationUnitDeclaration unitDeclaration) { return suffixCheckCodeAssist(unitDeclaration, bean, bean.getRegExPattern()); } /** * ??(??). * * @param unitDeclaration * @param suffixBean . * @param suffixPattern . * @return ?????????. */ boolean suffixCheckCodeAssist(CompilationUnitDeclaration unitDeclaration, ObjectLiteralBean suffixBean, Pattern suffixPattern) { // ?? NodeCollector collector = NodeCollectorFactory.createNodeCollector(suffixBean); // unitDeclaration????. collector.collect(unitDeclaration); // ?? SuffixAssistNodeInfo info = CheckerUtils.getAssistNodeInfo(collector, unitDeclaration); // ????null???false memberAccess = info.getMemberAccess(); if (memberAccess == null) { return false; } // codeAssist??. boolean checkCodeAssistNodeFlg = CheckerUtils.checkCodeAssistNode(suffixBean, memberAccess); logger.log(Messages.DB0004, checkCodeAssistNodeFlg); if (!checkCodeAssistNodeFlg) { return false; } // ? this.codeAssistStr = memberAccess.getReceiver().toString(); logger.log(Messages.DB0005, this.codeAssistStr); String firstSegMemberAccess = CheckerUtils.getRootObject(codeAssistStr); // ??(xxxController)????? if (suffixPattern.matcher(firstSegMemberAccess).matches()) { logger.log(Messages.DB0006); return checkParentObjController() && checkCodeAssistNodeFlg; } // ??this???? if (StringUtils.equals(firstSegMemberAccess, "this")) { logger.log(Messages.DB0007); return checkParentObjThis(info) && checkCodeAssistNodeFlg; } // ??????. logger.log(Messages.DB0008); return checkOtherCase(info); } /** * ??this????????.<br> * ????????<br> * ???. * * @param info ????. * @return ??????. */ private boolean checkOtherCase(SuffixAssistNodeInfo info) { if (info.getTargetNodes() == null || info.getTargetNodes().length == 0) { return false; } return checkParentObj(info.getTargetNodes()); } /** * ????this??????.<br> * ??????<br> * ??. * * @param info ????. * @return ??????. */ private boolean checkParentObjThis(SuffixAssistNodeInfo info) { return checkParentObj(info.getTargetNodes()); } /** * ???????????true?<br> * ???????<br> * ??.<br> * ????????????false?. * * @param targetNodes . * @return ?????????. */ private boolean checkParentObj(IASTNode[] targetNodes) { // ??????. boolean check = false; Pattern suffixPattern = getBean().getRegExPattern(); for (IASTNode astNode : targetNodes) { int insertPosition = CheckerUtils.getInitializerSourceEnd(suffixPattern, astNode); if (insertPosition > 0) { this.addDummyCodeInfoList(createDummyCodeInfo(insertPosition, CodeBuilderType.OBJ_LITERAL)); check = true; } } return check; } /** * ??. * * @param insertPosition ?. * @param objLiteral . * @return . */ private DummyCodeInfo createDummyCodeInfo(int insertPosition, CodeBuilderType objLiteral) { return new DummyCodeInfo(insertPosition, objLiteral); } /** * ??????????.<br> * ?????????true?.<br> * ???false. * * @return ?????????. */ private boolean checkParentObjController() { String addedObjectName = null; Pattern suffixPattern = getBean().getRegExPattern(); if (suffixPattern.matcher(this.getCodeAssistStr()).matches()) { addedObjectName = this.getCodeAssistStr(); } else { addedObjectName = StringUtils.substringBeforeLast(this.getCodeAssistStr().toString(), "."); } this.addDummyCodeInfoList(new DelegateDummyCodeInfo(memberAccess.sourceEnd() + 1, addedObjectName, CodeBuilderType.REFERENCE_OBJ)); // } return true; } @Override public String getCodeAssistStr() { return codeAssistStr; } @Override protected ObjectLiteralBean getBean() { return bean; } @Override CompletionOnMemberAccess getMemberAccess() { return memberAccess; } }