|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.javacore.jmiimpl.javamodel; import org.netbeans.lib.java.parser.ASTree; import org.netbeans.mdr.storagemodel.StorableObject; import org.netbeans.modules.javacore.parser.ElementInfo; import org.openide.util.Utilities; import java.util.*; import org.netbeans.jmi.javamodel.*; import org.netbeans.modules.javacore.parser.ASTProvider; /** * Implementation of Import object instance interface. * * @author Martin Matula * @author Pavel Flaska */ public abstract class ImportImpl extends SemiPersistentElement implements Import { private static final ElementInfo DEFAULT_INFO = new ElementInfo(null, ElementInfo.SINGLE_IMPORT_TYPE, null); String name = null; boolean isStatic = false; boolean isOnDemand = false; private MultipartId identifier = null; /** Creates a new instance of ImportImpl */ public ImportImpl(StorableObject s) { super(s); } /** Should be overriden by elements that have persistent attributes. * They should implement this method to compare values in newly passed AST info with * values of the persistent attributes and if the values do not match, they should be * updated and proper events should be fired. */ protected void matchPersistent(ElementInfo info) { // override to not match name (which is matched by default) } protected ElementInfo getDefaultInfo() { return DEFAULT_INFO; } /** The method has to make sure that the AST infos of children are also updated. */ protected void matchElementInfo(ElementInfo newInfo) { super.matchElementInfo(newInfo); ElementInfo refInfo=getElementInfo(); if (!Utilities.compareObjects(refInfo.name, newInfo.name)) setName(newInfo.name); } protected void resetChildren() { if (identifier != null) { MultipartId temp = identifier; identifier = null; temp.refDelete(); } } public String getName() { if (isChanged(CHANGED_NAME)) { return name; } else { return getElementInfo().name; } } public void setName(String name) { objectChanged(CHANGED_NAME); this.name = name; MultipartIdClass proxy = ((JavaModelPackage) refImmediatePackage()).getMultipartId(); MultipartId newValue = proxy.createMultipartId(name, null, null); changeChild(getIdentifier(), newValue); this.identifier = newValue; } public boolean isStatic() { if (isChanged(CHANGED_IS_STATIC)) { return isStatic; } else { ASTree tree=getASTree(); if (tree==null) return false; return tree.getSubTrees()[0]!=null; } } public void setStatic(boolean s) { objectChanged(CHANGED_IS_STATIC); isStatic = s; } public boolean isOnDemand() { if (isChanged(CHANGED_IS_ON_DEMAND)) { return isOnDemand; } else { return getElementInfo().infoType == ElementInfo.IMPORT_ON_DEMAND_TYPE; } } public void setOnDemand(boolean s) { objectChanged(CHANGED_IS_ON_DEMAND); isOnDemand = s; } protected ASTree getPartTree(ElementPartKind part) { if (ElementPartKindEnum.NAME.equals(part)) { return getASTree().getSubTrees()[1]; } throw new IllegalArgumentException("Invalid part for this element: " + part); // NOI18N } public String getSourceText() { String origElem; if ((origElem = checkChange()) != null) return origElem; StringBuffer buf = new StringBuffer(); buf.append("import "); // NOI18N if (isStatic()) buf.append("static "); // NOI18N MetadataElement id = (MetadataElement)getIdentifier(); buf.append(id == null ? getName() : id.getSourceText()); if (isOnDemand()) { buf.append(".*"); // NOI18N } buf.append(';'); return buf.toString(); } public void getDiff(List diff) { ASTProvider parser = getParser(); ASTree tree = getASTree(); ASTree[] children = tree.getSubTrees(); if (isChanged(CHANGED_IS_STATIC)) { int startOffset, endOffset = parser.getToken(children[1].getFirstToken()).getStartOffset(); if (isStatic) { if (children[0] == null) { startOffset = endOffset; diff.add(new DiffElement(startOffset, endOffset, "static ")); // NOI18N } } else { if (children[0] != null) { startOffset = parser.getToken(children[0].getFirstToken()).getStartOffset(); diff.add(new DiffElement(startOffset, endOffset, "")); } } } getChildDiff(diff, parser, children[1], (MetadataElement) getIdentifier(), CHANGED_NAME); if (isChanged(CHANGED_IS_ON_DEMAND)) { int startOffset = parser.getToken(children[1].getLastToken()).getEndOffset(); int endOffset = parser.getToken(tree.getLastToken()).getStartOffset(); diff.add(new DiffElement(startOffset, endOffset, isOnDemand() ? ".*" : "")); // NOI18N } } public void replaceChild(Element oldElement, Element newElement) { if (childrenInited && oldElement.equals(identifier)) { setIdentifier((MultipartId) newElement); return; } } public MultipartId getIdentifier() { if (!childrenInited) { initChildren(); } return identifier; } public void setIdentifier(MultipartId newValue) { objectChanged(CHANGED_NAME); changeChild(getIdentifier(), newValue); this.identifier = newValue; this.name = newValue != null ? ((MultipartIdImpl)newValue).getSourceText() : ""; // NOI18N } protected void initChildren() { childrenInited = false; ElementInfo info = getElementInfo(); ASTree tree = info.getTypeAST(this); if (identifier != null) { ((TransientElement) identifier).init(tree); } else { identifier = (MultipartId) createElement(tree, ((JavaModelPackage) refImmediatePackage()).getMultipartId()); } childrenInited = true; } public List getChildren() { List l = new ArrayList(1); addIfNotNull(l, getIdentifier()); return l; } public NamedElement getImportedElement() { JavaModelPackage model = (JavaModelPackage) refImmediatePackage(); MultipartId id = getIdentifier(); if (id == null) return null; return id.getElement(); } public void setImportedElement(NamedElement element) { setName(element.getName()); } public Collection getReferences() { return Collections.EMPTY_LIST; } protected void hardRefParent(boolean enabled) { // do not hardref parent - this object is transient } protected void parentChanged() { // do nothing on parentChanged - this object is transient } } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.