diff options
author | kmoore | 2010-10-14 15:34:01 +0000 |
---|---|---|
committer | kmoore | 2010-10-14 15:34:01 +0000 |
commit | 51d3bd11b31b795e6cddecdf3dbc1658cdfb59b8 (patch) | |
tree | 63b37f78d4a0f6cb0d33a653bbf758195737ff87 /jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal | |
parent | fdb5b12e97c55c612bb5181f3728287d1ab6443b (diff) | |
download | webtools.dali-51d3bd11b31b795e6cddecdf3dbc1658cdfb59b8.tar.gz webtools.dali-51d3bd11b31b795e6cddecdf3dbc1658cdfb59b8.tar.xz webtools.dali-51d3bd11b31b795e6cddecdf3dbc1658cdfb59b8.zip |
265087 - package-level annotation support
Diffstat (limited to 'jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal')
10 files changed, 645 insertions, 128 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/AbstractJpaAnnotationDefintionProvider.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/AbstractJpaAnnotationDefintionProvider.java index 90bc29d85a..40500ad355 100644 --- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/AbstractJpaAnnotationDefintionProvider.java +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/AbstractJpaAnnotationDefintionProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009 Oracle. All rights reserved. + * Copyright (c) 2009, 2010 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -12,6 +12,7 @@ package org.eclipse.jpt.core.internal; import java.util.ArrayList; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import org.eclipse.jpt.core.JpaAnnotationDefinitionProvider; import org.eclipse.jpt.core.resource.java.AnnotationDefinition; import org.eclipse.jpt.utility.internal.iterators.ArrayIterator; @@ -26,6 +27,8 @@ public abstract class AbstractJpaAnnotationDefintionProvider private AnnotationDefinition[] attributeAnnotationDefinitions; + private AnnotationDefinition[] packageAnnotationDefinitions; + protected AbstractJpaAnnotationDefintionProvider() { super(); @@ -98,4 +101,28 @@ public abstract class AbstractJpaAnnotationDefintionProvider protected void addAttributeAnnotationDefinitionsTo(@SuppressWarnings("unused") List<AnnotationDefinition> definitions) { // no op } + + + // ********** package annotation definitions ********** + + public synchronized ListIterator<AnnotationDefinition> packageAnnotationDefinitions() { + if (this.packageAnnotationDefinitions == null) { + this.packageAnnotationDefinitions = this.buildPackageAnnotationDefinitions(); + } + return new ArrayListIterator<AnnotationDefinition>(this.packageAnnotationDefinitions); + } + + protected AnnotationDefinition[] buildPackageAnnotationDefinitions() { + ArrayList<AnnotationDefinition> definitions = new ArrayList<AnnotationDefinition>(); + this.addPackageAnnotationDefinitionsTo(definitions); + return definitions.toArray(new AnnotationDefinition[definitions.size()]); + } + + /** + * Subclasses must override this to specify package annotation + * definitions. No package annotation definitions by default. + */ + protected void addPackageAnnotationDefinitionsTo(@SuppressWarnings("unused") List<AnnotationDefinition> definitions) { + // no op + } } diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/AbstractJpaProject.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/AbstractJpaProject.java index ba2d5831eb..fb62c6f411 100644 --- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/AbstractJpaProject.java +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/AbstractJpaProject.java @@ -48,7 +48,7 @@ import org.eclipse.jpt.core.JpaResourceModelListener; import org.eclipse.jpt.core.JptCorePlugin; import org.eclipse.jpt.core.context.JpaRootContextNode; import org.eclipse.jpt.core.internal.resource.java.binary.BinaryPersistentTypeCache; -import org.eclipse.jpt.core.internal.resource.java.source.SourceCompilationUnit; +import org.eclipse.jpt.core.internal.resource.java.source.SourceTypeCompilationUnit; import org.eclipse.jpt.core.internal.utility.PlatformTools; import org.eclipse.jpt.core.internal.validation.DefaultJpaValidationMessages; import org.eclipse.jpt.core.internal.validation.JpaValidationMessages; @@ -59,6 +59,8 @@ import org.eclipse.jpt.core.resource.ResourceLocator; import org.eclipse.jpt.core.resource.java.JavaResourceCompilationUnit; import org.eclipse.jpt.core.resource.java.JavaResourceNode; import org.eclipse.jpt.core.resource.java.JavaResourcePackageFragmentRoot; +import org.eclipse.jpt.core.resource.java.JavaResourcePackage; +import org.eclipse.jpt.core.resource.java.JavaResourcePackageInfoCompilationUnit; import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType; import org.eclipse.jpt.core.resource.java.JavaResourcePersistentTypeCache; import org.eclipse.jpt.core.resource.xml.JpaXmlResource; @@ -680,7 +682,7 @@ public abstract class AbstractJpaProject } protected JavaResourceCompilationUnit buildJavaResourceCompilationUnit(ICompilationUnit jdtCompilationUnit) { - return new SourceCompilationUnit( + return new SourceTypeCompilationUnit( jdtCompilationUnit, this.jpaPlatform.getAnnotationProvider(), this.jpaPlatform.getAnnotationEditFormatter(), @@ -919,6 +921,41 @@ public abstract class AbstractJpaProject } + // ********** Java resource persistent package look-up ********** + + public JavaResourcePackage getJavaResourcePackage(String packName) { + for (JavaResourcePackage jrpp : this.getJavaResourcePackages()) { + if (jrpp.getName().equals(packName)) { + return jrpp; + } + } + return null; + } + + public Iterable<JavaResourcePackage> getJavaResourcePackages(){ + return new FilteringIterable<JavaResourcePackage>( + new TransformationIterable<JpaFile, JavaResourcePackage>(this.getPackageInfoSourceJpaFiles()) { + @Override + protected JavaResourcePackage transform(JpaFile jpaFile) { + return ((JavaResourcePackageInfoCompilationUnit) jpaFile.getResourceModel()).getPackage(); + } + }) + { + @Override + protected boolean accept(JavaResourcePackage packageInfo) { + return packageInfo != null; + } + }; + } + + /** + * return JPA files with package-info source "content" + */ + protected Iterable<JpaFile> getPackageInfoSourceJpaFiles() { + return this.getJpaFiles(JptCorePlugin.JAVA_SOURCE_PACKAGE_INFO_CONTENT_TYPE); + } + + // ********** JARs ********** // TODO diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/GenericJpaAnnotationProvider.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/GenericJpaAnnotationProvider.java index 07794ca782..32b0e75cb3 100644 --- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/GenericJpaAnnotationProvider.java +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/GenericJpaAnnotationProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009 Oracle. All rights reserved. + * Copyright (c) 2009, 2010 Oracle. All rights reserved. * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -17,8 +17,10 @@ import org.eclipse.jpt.core.JpaAnnotationProvider; import org.eclipse.jpt.core.resource.java.Annotation; import org.eclipse.jpt.core.resource.java.AnnotationDefinition; import org.eclipse.jpt.core.resource.java.JavaResourcePersistentAttribute; +import org.eclipse.jpt.core.resource.java.JavaResourcePackage; import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType; import org.eclipse.jpt.core.utility.jdt.Attribute; +import org.eclipse.jpt.core.utility.jdt.Package; import org.eclipse.jpt.core.utility.jdt.Type; import org.eclipse.jpt.utility.internal.iterators.ArrayListIterator; import org.eclipse.jpt.utility.internal.iterators.CompositeIterator; @@ -160,4 +162,42 @@ public class GenericJpaAnnotationProvider return annotationDefinition; } + + // ********** package annotations ********** + + public Iterator<String> packageAnnotationNames() { + return annotationNames(packageAnnotationDefinitions()); + } + + protected Iterator<AnnotationDefinition> packageAnnotationDefinitions() { + return new CompositeIterator<AnnotationDefinition> ( + new TransformationIterator<JpaAnnotationDefinitionProvider, Iterator<AnnotationDefinition>>(this.annotationDefinitionProviders()) { + @Override + protected Iterator<AnnotationDefinition> transform(JpaAnnotationDefinitionProvider annotationDefinitionProvider) { + return annotationDefinitionProvider.packageAnnotationDefinitions(); + } + } + ); + } + + public Annotation buildPackageAnnotation(JavaResourcePackage parent, Package pack, String annotationName) { + return this.getPackageAnnotationDefinition(annotationName).buildAnnotation(parent, pack); + } + + public Annotation buildPackageAnnotation(JavaResourcePackage parent, IAnnotation jdtAnnotation) { + return this.getPackageAnnotationDefinition(jdtAnnotation.getElementName()).buildAnnotation(parent, jdtAnnotation); + } + + public Annotation buildNullPackageAnnotation(JavaResourcePackage parent, String annotationName) { + return this.getPackageAnnotationDefinition(annotationName).buildNullAnnotation(parent); + } + + protected AnnotationDefinition getPackageAnnotationDefinition(String annotationName) { + AnnotationDefinition annotationDefinition = this.selectAnnotationDefinition(this.packageAnnotationDefinitions(), annotationName); + if (annotationDefinition == null) { + throw new IllegalArgumentException("unsupported package mapping annotation: " + annotationName); //$NON-NLS-1$ + } + return annotationDefinition; + } + } diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/JavaPackageInfoResourceModelProvider.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/JavaPackageInfoResourceModelProvider.java new file mode 100644 index 0000000000..e51d6536a6 --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/JavaPackageInfoResourceModelProvider.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright (c) 2010 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.core.internal; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.runtime.content.IContentType; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jpt.core.JpaProject; +import org.eclipse.jpt.core.JpaResourceModelProvider; +import org.eclipse.jpt.core.JptCorePlugin; +import org.eclipse.jpt.core.internal.resource.java.source.SourcePackageInfoCompilationUnit; +import org.eclipse.jpt.core.resource.java.JavaResourceCompilationUnit; + +/** + * Java package-info.java source code + */ +public class JavaPackageInfoResourceModelProvider + implements JpaResourceModelProvider +{ + // singleton + private static final JpaResourceModelProvider INSTANCE = new JavaPackageInfoResourceModelProvider(); + + /** + * Return the singleton. + */ + public static JpaResourceModelProvider instance() { + return INSTANCE; + } + + /** + * Ensure single instance. + */ + private JavaPackageInfoResourceModelProvider() { + super(); + } + + public IContentType getContentType() { + return JptCorePlugin.JAVA_SOURCE_PACKAGE_INFO_CONTENT_TYPE; + } + + public JavaResourceCompilationUnit buildResourceModel(JpaProject jpaProject, IFile file) { + return new SourcePackageInfoCompilationUnit( + JavaCore.createCompilationUnitFrom(file), + jpaProject.getJpaPlatform().getAnnotationProvider(), + jpaProject.getJpaPlatform().getAnnotationEditFormatter(), + jpaProject.getModifySharedDocumentCommandExecutor() + ); + } + +} diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/JavaResourceModelProvider.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/JavaResourceModelProvider.java index 5d87c03086..e1b5e49a77 100644 --- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/JavaResourceModelProvider.java +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/JavaResourceModelProvider.java @@ -15,7 +15,7 @@ import org.eclipse.jdt.core.JavaCore; import org.eclipse.jpt.core.JpaProject; import org.eclipse.jpt.core.JpaResourceModelProvider; import org.eclipse.jpt.core.JptCorePlugin; -import org.eclipse.jpt.core.internal.resource.java.source.SourceCompilationUnit; +import org.eclipse.jpt.core.internal.resource.java.source.SourceTypeCompilationUnit; import org.eclipse.jpt.core.resource.java.JavaResourceCompilationUnit; /** @@ -46,7 +46,7 @@ public class JavaResourceModelProvider } public JavaResourceCompilationUnit buildResourceModel(JpaProject jpaProject, IFile file) { - return new SourceCompilationUnit( + return new SourceTypeCompilationUnit( JavaCore.createCompilationUnitFrom(file), jpaProject.getJpaPlatform().getAnnotationProvider(), jpaProject.getJpaPlatform().getAnnotationEditFormatter(), diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourceCompilationUnit.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourceCompilationUnit.java index d7264195d0..cfe7a4c1e7 100644 --- a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourceCompilationUnit.java +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourceCompilationUnit.java @@ -9,37 +9,28 @@ ******************************************************************************/ package org.eclipse.jpt.core.internal.resource.java.source; -import java.util.Iterator; -import java.util.List; import org.eclipse.core.resources.IFile; import org.eclipse.jdt.core.ICompilationUnit; import org.eclipse.jdt.core.JavaModelException; -import org.eclipse.jdt.core.dom.ASTNode; -import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; import org.eclipse.jdt.core.dom.CompilationUnit; -import org.eclipse.jdt.core.dom.TypeDeclaration; import org.eclipse.jpt.core.JpaAnnotationProvider; import org.eclipse.jpt.core.JpaResourceModelListener; -import org.eclipse.jpt.core.JpaResourceType; -import org.eclipse.jpt.core.JptCorePlugin; import org.eclipse.jpt.core.internal.utility.jdt.ASTTools; import org.eclipse.jpt.core.resource.java.JavaResourceCompilationUnit; -import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType; import org.eclipse.jpt.core.utility.TextRange; import org.eclipse.jpt.core.utility.jdt.AnnotationEditFormatter; import org.eclipse.jpt.utility.CommandExecutor; import org.eclipse.jpt.utility.internal.ListenerList; -import org.eclipse.jpt.utility.internal.iterators.EmptyIterator; /** * Java compilation unit (source file) */ -public final class SourceCompilationUnit +public abstract class SourceCompilationUnit extends SourceNode implements JavaResourceCompilationUnit { /** JDT compilation unit */ - private final ICompilationUnit compilationUnit; + final ICompilationUnit compilationUnit; /** pluggable annotation provider */ private final JpaAnnotationProvider annotationProvider; @@ -53,19 +44,10 @@ public final class SourceCompilationUnit /** listeners notified whenever the resource model changes */ private final ListenerList<JpaResourceModelListener> resourceModelListenerList; - /** - * The primary type of the AST compilation unit. We are not going to handle - * multiple types defined in a single compilation unit. Entities must have - * a public/protected no-arg constructor, and there is no way to access - * the constructor in a package class (which is what all top-level, - * non-primary classes must be). - */ - private JavaResourcePersistentType persistentType; - // ********** construction ********** - public SourceCompilationUnit( + protected SourceCompilationUnit( ICompilationUnit compilationUnit, JpaAnnotationProvider annotationProvider, AnnotationEditFormatter annotationEditFormatter, @@ -76,21 +58,13 @@ public final class SourceCompilationUnit this.annotationEditFormatter = annotationEditFormatter; this.modifySharedDocumentCommandExecutor = modifySharedDocumentCommandExecutor; this.resourceModelListenerList = new ListenerList<JpaResourceModelListener>(JpaResourceModelListener.class); - this.persistentType = this.buildPersistentType(); } public void initialize(CompilationUnit astRoot) { // never called? } - private JavaResourcePersistentType buildPersistentType() { - this.openCompilationUnit(); - CompilationUnit astRoot = this.buildASTRoot(); - this.closeCompilationUnit(); - return this.buildPersistentType(astRoot); - } - - private void openCompilationUnit() { + void openCompilationUnit() { try { this.compilationUnit.open(null); } catch (JavaModelException ex) { @@ -98,7 +72,7 @@ public final class SourceCompilationUnit } } - private void closeCompilationUnit() { + void closeCompilationUnit() { try { this.compilationUnit.close(); } catch (JavaModelException ex) { @@ -132,10 +106,6 @@ public final class SourceCompilationUnit // ********** JavaResourceNode implementation ********** - public void synchronizeWith(CompilationUnit astRoot) { - this.syncPersistentType(astRoot); - } - public TextRange getTextRange(CompilationUnit astRoot) { return null; } @@ -143,15 +113,6 @@ public final class SourceCompilationUnit // ********** JavaResourceNode.Root implementation ********** - /** - * NB: return *all* the persistent types since we build them all - */ - public Iterator<JavaResourcePersistentType> persistentTypes() { - return (this.persistentType == null) ? - EmptyIterator.<JavaResourcePersistentType>instance() : - this.persistentType.allTypes(); - } - public void resourceModelChanged() { for (JpaResourceModelListener listener : this.resourceModelListenerList.getListeners()) { listener.resourceModelChanged(this); @@ -165,12 +126,6 @@ public final class SourceCompilationUnit return this.compilationUnit; } - public void resolveTypes() { - if (this.persistentType != null) { - this.persistentType.resolveTypes(this.buildASTRoot()); - } - } - public CommandExecutor getModifySharedDocumentCommandExecutor() { return this.modifySharedDocumentCommandExecutor; } @@ -184,39 +139,7 @@ public final class SourceCompilationUnit } - // ********** persistent type ********** - - private JavaResourcePersistentType buildPersistentType(CompilationUnit astRoot) { - TypeDeclaration td = this.getPrimaryTypeDeclaration(astRoot); - return (td == null) ? null : this.buildPersistentType(astRoot, td); - } - - - private void syncPersistentType(CompilationUnit astRoot) { - TypeDeclaration td = this.getPrimaryTypeDeclaration(astRoot); - if (td == null) { - this.syncPersistentType_(null); - } else { - if (this.persistentType == null) { - this.syncPersistentType_(this.buildPersistentType(astRoot, td)); - } else { - this.persistentType.synchronizeWith(astRoot); - } - } - } - - private void syncPersistentType_(JavaResourcePersistentType astPersistentType) { - JavaResourcePersistentType old = this.persistentType; - this.persistentType = astPersistentType; - this.firePropertyChanged(PERSISTENT_TYPES_COLLECTION, old, astPersistentType); - } - - // ********** JpaResourceModel implementation ********** - - public JpaResourceType getResourceType() { - return JptCorePlugin.JAVA_SOURCE_RESOURCE_TYPE; - } public void addResourceModelListener(JpaResourceModelListener listener) { this.resourceModelListenerList.add(listener); @@ -236,45 +159,7 @@ public final class SourceCompilationUnit // ********** internal ********** - private JavaResourcePersistentType buildPersistentType(CompilationUnit astRoot, TypeDeclaration typeDeclaration) { - return SourcePersistentType.newInstance(this, typeDeclaration, astRoot); - } - - /** - * i.e. the type with the same name as the compilation unit; - * return the first class or interface (ignore annotations and enums) with - * the same name as the compilation unit (file); - * NB: this type could be in error if there is an annotation or enum - * with the same name preceding it in the compilation unit - * - * Return null if the parser did not resolve the type declaration's binding. - * This can occur if the project JRE is removed (bug 225332). - */ - private TypeDeclaration getPrimaryTypeDeclaration(CompilationUnit astRoot) { - String primaryTypeName = this.getPrimaryTypeName(); - for (AbstractTypeDeclaration atd : this.types(astRoot)) { - if (this.nodeIsPrimaryTypeDeclaration(atd, primaryTypeName)) { - return (atd.resolveBinding() == null) ? null : (TypeDeclaration) atd; - } - } - return null; - } - - private boolean nodeIsPrimaryTypeDeclaration(AbstractTypeDeclaration atd, String primaryTypeName) { - return (atd.getNodeType() == ASTNode.TYPE_DECLARATION) && - atd.getName().getFullyQualifiedName().equals(primaryTypeName); - } - - // minimize scope of suppressed warnings - @SuppressWarnings("unchecked") - private List<AbstractTypeDeclaration> types(CompilationUnit astRoot) { - return astRoot.types(); - } - - /** - * i.e. the name of the compilation unit - */ - private String getPrimaryTypeName() { + String getCompilationUnitName() { return this.removeJavaExtension(this.compilationUnit.getElementName()); } @@ -285,7 +170,7 @@ public final class SourceCompilationUnit @Override public void toString(StringBuilder sb) { - sb.append(this.getPrimaryTypeName()); + sb.append(this.getCompilationUnitName()); } } diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourcePackage.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourcePackage.java new file mode 100644 index 0000000000..6d0a1dd772 --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourcePackage.java @@ -0,0 +1,118 @@ +/******************************************************************************* + * Copyright (c) 2010 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributor: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.core.internal.resource.java.source; + +import java.util.Iterator; + +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.IPackageBinding; +import org.eclipse.jdt.core.dom.PackageDeclaration; +import org.eclipse.jpt.core.internal.utility.jdt.JDTPackage; +import org.eclipse.jpt.core.resource.java.Annotation; +import org.eclipse.jpt.core.resource.java.JavaResourceCompilationUnit; +import org.eclipse.jpt.core.resource.java.JavaResourcePackage; +import org.eclipse.jpt.core.utility.jdt.Package; + +/** + * @author Dmitry Geraskov + * Source package-info.java + * + */ +public final class SourcePackage + extends SourceAnnotatedElement<Package> + implements JavaResourcePackage { + + private String name; + + /** + * construct package info + */ + public static JavaResourcePackage newInstance( + JavaResourceCompilationUnit parent, + PackageDeclaration declaringPackage, + CompilationUnit astRoot) { + Package pack = new JDTPackage( + declaringPackage, + parent.getCompilationUnit(), + parent.getModifySharedDocumentCommandExecutor(), + parent.getAnnotationEditFormatter()); + JavaResourcePackage jrpp = new SourcePackage(parent, pack); + jrpp.initialize(astRoot); + return jrpp; + } + + private SourcePackage( + JavaResourceCompilationUnit parent, + Package pack){ + super(parent, pack); + } + + @Override + public void initialize(CompilationUnit astRoot) { + super.initialize(astRoot); + this.name = this.buildName(astRoot); + } + + + // ********** JavaResourcePackageInfo implementation ********** + + // ***** name + public String getName() { + return this.name; + } + + private void syncName(String astName) { + if (valuesAreDifferent(astName, this.name)){ + String old = this.name; + this.name = astName; + this.firePropertyChanged(NAME_PROPERTY, old, astName); + } + } + + private String buildName(CompilationUnit astRoot) { + IPackageBinding binding = this.annotatedElement.getBinding(astRoot); + return (binding == null) ? null : binding.getName(); + } + + + // ********** Java changes ********** + + @Override + public void synchronizeWith(CompilationUnit astRoot) { + super.synchronizeWith(astRoot); + this.syncName(this.buildName(astRoot)); + } + + // ********** SourceAnnotatedElement implementation ********** + + @Override + Iterator<String> validAnnotationNames() { + return this.getAnnotationProvider().packageAnnotationNames(); + } + + + @Override + Annotation buildAnnotation(String annotationName) { + return this.getAnnotationProvider().buildPackageAnnotation(this, this.annotatedElement, annotationName); + } + + @Override + Annotation buildNullAnnotation(String annotationName) { + return this.getAnnotationProvider().buildNullPackageAnnotation(this, annotationName); + } + + + @Override + public void toString(StringBuilder sb) { + sb.append(this.name); + } + +} diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourcePackageInfoCompilationUnit.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourcePackageInfoCompilationUnit.java new file mode 100644 index 0000000000..67b97ebcef --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourcePackageInfoCompilationUnit.java @@ -0,0 +1,121 @@ +/******************************************************************************* + * Copyright (c) 2010 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributor: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.core.internal.resource.java.source; + +import java.util.Iterator; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.PackageDeclaration; +import org.eclipse.jpt.core.JpaAnnotationProvider; +import org.eclipse.jpt.core.JpaResourceType; +import org.eclipse.jpt.core.JptCorePlugin; +import org.eclipse.jpt.core.resource.java.JavaResourcePackage; +import org.eclipse.jpt.core.resource.java.JavaResourcePackageInfoCompilationUnit; +import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType; +import org.eclipse.jpt.core.utility.jdt.AnnotationEditFormatter; +import org.eclipse.jpt.utility.CommandExecutor; +import org.eclipse.jpt.utility.internal.iterators.EmptyIterator; + +/** + * @author Dmitry Geraskov + * Source package-info.java + * + */ +public final class SourcePackageInfoCompilationUnit + extends SourceCompilationUnit + implements JavaResourcePackageInfoCompilationUnit { + + private JavaResourcePackage package_; + + public SourcePackageInfoCompilationUnit( + ICompilationUnit compilationUnit, + JpaAnnotationProvider annotationProvider, + AnnotationEditFormatter annotationEditFormatter, + CommandExecutor modifySharedDocumentCommandExecutor) { + super(compilationUnit, annotationProvider, annotationEditFormatter, modifySharedDocumentCommandExecutor); // the JPA compilation unit is the root of its sub-tree + this.package_ = this.buildPackage(); + } + + + private JavaResourcePackage buildPackage() { + this.openCompilationUnit(); + CompilationUnit astRoot = this.buildASTRoot(); + this.closeCompilationUnit(); + return this.buildPackage(astRoot); + } + + + // ********** JavaResourceNode.Root implementation ********** + + public Iterator<JavaResourcePersistentType> persistentTypes() { + return EmptyIterator.instance(); + } + + + // ********** JpaResourceModel implementation ********** + + public JpaResourceType getResourceType() { + return JptCorePlugin.JAVA_SOURCE_PACKAGE_INFO_RESOURCE_TYPE; + } + + + // ********** Java changes ********** + + public void synchronizeWith(CompilationUnit astRoot) { + this.syncPackage(astRoot); + } + + + // ********** JavaResourceCompilationUnit implementation ********** + + public void resolveTypes() { + //no-op + } + + // ********** package-info ********** + + public JavaResourcePackage getPackage() { + return this.package_; + } + + private JavaResourcePackage buildPackage(CompilationUnit astRoot) { + return this.buildPackage(astRoot, this.getPackageDeclaration(astRoot)); + } + + private void syncPackage(CompilationUnit astRoot) { + PackageDeclaration pd = this.getPackageDeclaration(astRoot); + if (pd == null) { + this.syncPackage_(null); + } else { + if (this.package_ == null) { + this.syncPackage_(this.buildPackage(astRoot, pd)); + } else { + this.package_.synchronizeWith(astRoot); + } + } + } + + private PackageDeclaration getPackageDeclaration(CompilationUnit astRoot) { + return astRoot.getPackage(); + } + + private void syncPackage_(JavaResourcePackage astPackage) { + JavaResourcePackage old = this.package_; + this.package_ = astPackage; + this.firePropertyChanged(PACKAGE, old, astPackage); + } + + private JavaResourcePackage buildPackage(CompilationUnit astRoot, PackageDeclaration packageDeclaration) { + return SourcePackage.newInstance(this, packageDeclaration, astRoot); + } + +} diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourceTypeCompilationUnit.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourceTypeCompilationUnit.java new file mode 100644 index 0000000000..d6dc5b48fe --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/resource/java/source/SourceTypeCompilationUnit.java @@ -0,0 +1,167 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.core.internal.resource.java.source; + +import java.util.Iterator; +import java.util.List; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.dom.ASTNode; +import org.eclipse.jdt.core.dom.AbstractTypeDeclaration; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.TypeDeclaration; +import org.eclipse.jpt.core.JpaAnnotationProvider; +import org.eclipse.jpt.core.JpaResourceType; +import org.eclipse.jpt.core.JptCorePlugin; +import org.eclipse.jpt.core.resource.java.JavaResourcePersistentType; +import org.eclipse.jpt.core.utility.jdt.AnnotationEditFormatter; +import org.eclipse.jpt.utility.CommandExecutor; +import org.eclipse.jpt.utility.internal.iterators.EmptyIterator; + +/** + * Java compilation unit (source file) + * non package-info.java file + */ +public final class SourceTypeCompilationUnit + extends SourceCompilationUnit +{ + + /** + * The primary type of the AST compilation unit. We are not going to handle + * multiple types defined in a single compilation unit. Entities must have + * a public/protected no-arg constructor, and there is no way to access + * the constructor in a package class (which is what all top-level, + * non-primary classes must be). + */ + private JavaResourcePersistentType persistentType; + + + // ********** construction ********** + + public SourceTypeCompilationUnit( + ICompilationUnit compilationUnit, + JpaAnnotationProvider annotationProvider, + AnnotationEditFormatter annotationEditFormatter, + CommandExecutor modifySharedDocumentCommandExecutor) { + super(compilationUnit, annotationProvider, annotationEditFormatter, modifySharedDocumentCommandExecutor); // the JPA compilation unit is the root of its sub-tree + this.persistentType = this.buildPersistentType(); + } + + private JavaResourcePersistentType buildPersistentType() { + this.openCompilationUnit(); + CompilationUnit astRoot = this.buildASTRoot(); + this.closeCompilationUnit(); + return this.buildPersistentType(astRoot); + } + + + // ********** JavaResourceNode implementation ********** + + public void synchronizeWith(CompilationUnit astRoot) { + this.syncPersistentType(astRoot); + } + + + // ********** JavaResourceNode.Root implementation ********** + + /** + * NB: return *all* the persistent types since we build them all + */ + public Iterator<JavaResourcePersistentType> persistentTypes() { + return (this.persistentType == null) ? + EmptyIterator.<JavaResourcePersistentType>instance() : + this.persistentType.allTypes(); + } + + + // ********** JpaResourceModel implementation ********** + + public JpaResourceType getResourceType() { + return JptCorePlugin.JAVA_SOURCE_RESOURCE_TYPE; + } + + + // ********** JavaResourceCompilationUnit implementation ********** + + public void resolveTypes() { + if (this.persistentType != null) { + this.persistentType.resolveTypes(this.buildASTRoot()); + } + } + + + // ********** persistent type ********** + + private JavaResourcePersistentType buildPersistentType(CompilationUnit astRoot) { + TypeDeclaration td = this.getPrimaryTypeDeclaration(astRoot); + return (td == null) ? null : this.buildPersistentType(astRoot, td); + } + + + private void syncPersistentType(CompilationUnit astRoot) { + TypeDeclaration td = this.getPrimaryTypeDeclaration(astRoot); + if (td == null) { + this.syncPersistentType_(null); + } else { + if (this.persistentType == null) { + this.syncPersistentType_(this.buildPersistentType(astRoot, td)); + } else { + this.persistentType.synchronizeWith(astRoot); + } + } + } + + private void syncPersistentType_(JavaResourcePersistentType astPersistentType) { + JavaResourcePersistentType old = this.persistentType; + this.persistentType = astPersistentType; + this.firePropertyChanged(PERSISTENT_TYPES_COLLECTION, old, astPersistentType); + } + + + // ********** internal ********** + + private JavaResourcePersistentType buildPersistentType(CompilationUnit astRoot, TypeDeclaration typeDeclaration) { + return SourcePersistentType.newInstance(this, typeDeclaration, astRoot); + } + + /** + * i.e. the type with the same name as the compilation unit; + * return the first class or interface (ignore annotations and enums) with + * the same name as the compilation unit (file); + * NB: this type could be in error if there is an annotation or enum + * with the same name preceding it in the compilation unit + * + * Return null if the parser did not resolve the type declaration's binding. + * This can occur if the project JRE is removed (bug 225332). + */ + private TypeDeclaration getPrimaryTypeDeclaration(CompilationUnit astRoot) { + String primaryTypeName = this.getPrimaryTypeName(); + for (AbstractTypeDeclaration atd : this.types(astRoot)) { + if (this.nodeIsPrimaryTypeDeclaration(atd, primaryTypeName)) { + return (atd.resolveBinding() == null) ? null : (TypeDeclaration) atd; + } + } + return null; + } + + private boolean nodeIsPrimaryTypeDeclaration(AbstractTypeDeclaration atd, String primaryTypeName) { + return (atd.getNodeType() == ASTNode.TYPE_DECLARATION) && + atd.getName().getFullyQualifiedName().equals(primaryTypeName); + } + + private String getPrimaryTypeName() { + return this.getCompilationUnitName(); + } + + // minimize scope of suppressed warnings + @SuppressWarnings("unchecked") + private List<AbstractTypeDeclaration> types(CompilationUnit astRoot) { + return astRoot.types(); + } +} diff --git a/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/JDTPackage.java b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/JDTPackage.java new file mode 100644 index 0000000000..8c30dc0b8f --- /dev/null +++ b/jpa/plugins/org.eclipse.jpt.core/src/org/eclipse/jpt/core/internal/utility/jdt/JDTPackage.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright (c) 2010 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + * + * Contributor: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.core.internal.utility.jdt; + +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jdt.core.dom.CompilationUnit; +import org.eclipse.jdt.core.dom.IPackageBinding; +import org.eclipse.jdt.core.dom.PackageDeclaration; +import org.eclipse.jpt.core.utility.TextRange; +import org.eclipse.jpt.core.utility.jdt.AnnotationEditFormatter; +import org.eclipse.jpt.core.utility.jdt.ModifiedDeclaration; +import org.eclipse.jpt.core.utility.jdt.Package; +import org.eclipse.jpt.utility.CommandExecutor; + +/** + * @author Dmitry Geraskov + * + */ + +public class JDTPackage extends JDTAnnotatedElement implements Package { + + + protected JDTPackage(PackageDeclaration declaringPackage, + ICompilationUnit compilationUnit, + CommandExecutor modifySharedDocumentCommandExecutor) { + super(declaringPackage.getName().getFullyQualifiedName(), + compilationUnit, + modifySharedDocumentCommandExecutor); + } + + public JDTPackage( + PackageDeclaration declaringPackage, + ICompilationUnit compilationUnit, + CommandExecutor modifySharedDocumentCommandExecutor, + AnnotationEditFormatter annotationEditFormatter) { + super(declaringPackage.getName().getFullyQualifiedName(), + compilationUnit, modifySharedDocumentCommandExecutor, annotationEditFormatter); + } + + @Override + public ModifiedDeclaration getModifiedDeclaration(CompilationUnit astRoot) { + return new JDTModifiedDeclaration(this.getBodyDeclaration(astRoot)); + } + + public IPackageBinding getBinding(CompilationUnit astRoot) { + PackageDeclaration pd = this.getBodyDeclaration(astRoot); + return (pd == null) ? null : pd.resolveBinding(); + } + + public PackageDeclaration getBodyDeclaration(CompilationUnit astRoot) { + return astRoot.getPackage(); + } + + public TextRange getNameTextRange(CompilationUnit astRoot) { + return new ASTNodeTextRange(this.getBodyDeclaration(astRoot).getName()); + } +} |