Permalink
Browse files

Code select support for binary groovy files.

no tests.
  • Loading branch information...
aeisenberg committed Apr 5, 2013
1 parent 04a49fd commit 1d101e7f36e88db56d8fab9169027d18636950b1
@@ -27,6 +27,7 @@
import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyErrorCollectorForJDT;
import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyTypeDeclaration;
import org.codehaus.jdt.groovy.internal.compiler.ast.JDTResolver;
+import org.codehaus.jdt.groovy.model.GroovyClassFileWorkingCopy;
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
import org.codehaus.jdt.groovy.model.GroovyNature;
import org.eclipse.core.resources.IProject;
@@ -51,6 +52,7 @@
import org.eclipse.jdt.internal.compiler.SourceElementParser;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
import org.eclipse.jdt.internal.compiler.ast.TypeDeclaration;
+import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.parser.Parser;
@@ -256,4 +258,15 @@ public IJavaSearchScope expandSearchScope(IJavaSearchScope scope, SearchPattern
}
return scope;
}
+
+ public boolean isInterestingBinary(BinaryType type, IBinaryType typeInfo) {
+ return isInterestingProject(type.getJavaProject().getProject())
+ && ContentTypeUtils.isGroovyLikeFileName(type.sourceFileName(typeInfo));
+ }
+
+ public IJavaElement[] binaryCodeSelect(ClassFile classFile, int offset, int length, WorkingCopyOwner owner)
+ throws JavaModelException {
+ GroovyCompilationUnit binaryUnit = new GroovyClassFileWorkingCopy(classFile, owner);
+ return binaryUnit.codeSelect(offset, length, owner);
+ }
}
@@ -1,40 +0,0 @@
-/*******************************************************************************
- * Copyright (c) 2010 Codehaus.org, SpringSource, and others.
- * 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:
- * Andrew Eisenberg - Additional work
- *******************************************************************************/
-package org.codehaus.jdt.groovy.model;
-
-import org.eclipse.jdt.core.IJavaElement;
-import org.eclipse.jdt.core.JavaModelException;
-import org.eclipse.jdt.core.WorkingCopyOwner;
-import org.eclipse.jdt.internal.core.ClassFile;
-import org.eclipse.jdt.internal.core.PackageFragment;
-
-/**
- * Don't think I need thisce
- *
- * @author Andrew Eisenberg
- * @created Oct 22, 2010
- */
-public class GroovyClassFile extends ClassFile {
-
- protected GroovyClassFile(PackageFragment parent, String nameWithoutExtension) {
- super(parent, nameWithoutExtension);
- }
-
- @Override
- protected IJavaElement[] codeSelect(org.eclipse.jdt.internal.compiler.env.ICompilationUnit cu, int offset, int length,
- WorkingCopyOwner o) throws JavaModelException {
-
- if (CodeSelectHelperFactory.selectHelper != null) {
- // return CodeSelectHelperFactory.selectHelper.select(this, offset, length);
- }
- return new IJavaElement[0];
- }
-}
@@ -0,0 +1,246 @@
+/*******************************************************************************
+ * Copyright (c) 2012 VMWare, Inc.
+ * 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:
+ * VMWare, Inc. - initial API and implementation
+ *******************************************************************************/
+package org.codehaus.jdt.groovy.model;
+
+import org.codehaus.groovy.ast.ModuleNode;
+import org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration;
+import org.codehaus.jdt.groovy.internal.compiler.ast.JDTResolver;
+import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
+import org.codehaus.jdt.groovy.model.ModuleNodeMapper.ModuleNodeInfo;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.jdt.core.IBuffer;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.IJavaModelStatusConstants;
+import org.eclipse.jdt.core.JavaModelException;
+import org.eclipse.jdt.core.ToolFactory;
+import org.eclipse.jdt.core.WorkingCopyOwner;
+import org.eclipse.jdt.core.compiler.CharOperation;
+import org.eclipse.jdt.core.util.ClassFileBytesDisassembler;
+import org.eclipse.jdt.core.util.IClassFileReader;
+import org.eclipse.jdt.groovy.core.Activator;
+import org.eclipse.jdt.groovy.core.util.ReflectionUtils;
+import org.eclipse.jdt.internal.core.BinaryType;
+import org.eclipse.jdt.internal.core.BufferManager;
+import org.eclipse.jdt.internal.core.ClassFile;
+import org.eclipse.jdt.internal.core.ClassFileWorkingCopy;
+import org.eclipse.jdt.internal.core.CompilationUnitElementInfo;
+import org.eclipse.jdt.internal.core.DefaultWorkingCopyOwner;
+import org.eclipse.jdt.internal.core.JavaModelManager.PerWorkingCopyInfo;
+import org.eclipse.jdt.internal.core.JavaModelStatus;
+import org.eclipse.jdt.internal.core.Openable;
+import org.eclipse.jdt.internal.core.PackageFragment;
+import org.eclipse.jdt.internal.core.PackageFragmentRoot;
+import org.eclipse.jdt.internal.core.util.Disassembler;
+import org.eclipse.jdt.internal.core.util.Util;
+
+/**
+ * Working copy for groovy class files. Allows access to the ModuleNode for class files if the source is available. Copied from
+ * {@link ClassFileWorkingCopy} Groovy changes marked
+ *
+ * @author Andrew Eisenberg
+ * @author Christian Dupuis
+ * @created Dec 11, 2009
+ */
+public class GroovyClassFileWorkingCopy extends GroovyCompilationUnit {
+
+ public ClassFile classFile;
+
+ // GROOVY Change
+ private final PerWorkingCopyInfo info;
+ private CompilationUnitElementInfo elementInfo;
+ private ModuleNode moduleNode;
+ private ModuleNodeInfo moduleNodeInfo;
+
+ // GROOVY End
+
+ public GroovyClassFileWorkingCopy(ClassFile classFile, WorkingCopyOwner owner) {
+ super((PackageFragment) classFile.getParent(), ((BinaryType) classFile.getType())
+ .getSourceFileName(null/* no info available */), owner);
+ this.classFile = classFile;
+ // GROOVY Change
+ info = new PerWorkingCopyInfo(this, null);
+ // GROOVY End
+ }
+
+ public void commitWorkingCopy(boolean force, IProgressMonitor monitor) throws JavaModelException {
+ throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.INVALID_ELEMENT_TYPES, this));
+ }
+
+ public IBuffer getBuffer() throws JavaModelException {
+ // GROOVY Always use the classFile's buffer
+ // old
+ // if (isWorkingCopy())
+ // return super.getBuffer();
+ // else
+ // GROOVY end
+ return this.classFile.getBuffer();
+ }
+
+ public char[] getContents() {
+ try {
+ IBuffer buffer = getBuffer();
+ if (buffer == null)
+ return CharOperation.NO_CHAR;
+ char[] characters = buffer.getCharacters();
+ if (characters == null)
+ return CharOperation.NO_CHAR;
+ return characters;
+ } catch (JavaModelException e) {
+ return CharOperation.NO_CHAR;
+ }
+ }
+
+ public IPath getPath() {
+ return this.classFile.getPath();
+ }
+
+ public IJavaElement getPrimaryElement(boolean checkOwner) {
+ if (checkOwner && isPrimary())
+ return this;
+ return new ClassFileWorkingCopy(this.classFile, DefaultWorkingCopyOwner.PRIMARY);
+ }
+
+ public IResource resource(PackageFragmentRoot root) {
+ if (root.isArchive())
+ return root.resource(root);
+ return this.classFile.resource(root);
+ }
+
+ /**
+ * @see Openable#openBuffer(IProgressMonitor, Object)
+ */
+ protected IBuffer openBuffer(IProgressMonitor pm, Object info) throws JavaModelException {
+
+ // create buffer
+ IBuffer buffer = this.owner.createBuffer(this);
+ if (buffer == null)
+ return null;
+
+ // set the buffer source
+ if (buffer.getCharacters() == null) {
+ IBuffer classFileBuffer = this.classFile.getBuffer();
+ if (classFileBuffer != null) {
+ buffer.setContents(classFileBuffer.getCharacters());
+ } else {
+ // Disassemble
+ IClassFileReader reader = ToolFactory.createDefaultClassFileReader(this.classFile, IClassFileReader.ALL);
+ Disassembler disassembler = new Disassembler();
+ String contents = disassembler.disassemble(reader,
+ Util.getLineSeparator("", getJavaProject()), ClassFileBytesDisassembler.WORKING_COPY); //$NON-NLS-1$
+ buffer.setContents(contents);
+ }
+ }
+
+ // add buffer to buffer cache
+ BufferManager bufManager = getBufferManager();
+
+ // GROOVY Change access to private member
+ // old
+ // bufManager.addBuffer(buffer);
+ // new
+ if (buffer.getContents() != null) {
+ ReflectionUtils.executePrivateMethod(BufferManager.class,
+ "addBuffer", new Class<?>[] { IBuffer.class }, bufManager, new Object[] { buffer }); //$NON-NLS-1$
+ }
+ // GROOVY End
+
+ // listen to buffer changes
+ buffer.addBufferChangedListener(this);
+
+ return buffer;
+ }
+
+ protected void toStringName(StringBuffer buffer) {
+ buffer.append(this.classFile.getElementName());
+ }
+
+ // GROOVY Change
+ // all be a working copy
+ // build structure only needs to happen once.
+ @Override
+ public PerWorkingCopyInfo getPerWorkingCopyInfo() {
+ if (elementInfo == null) {
+ try {
+ elementInfo = (CompilationUnitElementInfo) createElementInfo();
+ // FIXADE in E4.2, this method takes 3 args
+ // when no longer supporting E3.7, then remove this
+ // and uncomment the line below
+ openWhenClosed(elementInfo, new NullProgressMonitor());
+ // openWhenClosed(elementInfo, true, new NullProgressMonitor());
+ } catch (JavaModelException e) {
+ elementInfo = null;
+ Activator.getDefault().getLog().log(e.getJavaModelStatus());
+ }
+ }
+ return info;
+ }
+
+ /**
+ * Cache module node locally and not in the mapper
+ */
+ @Override
+ protected void maybeCacheModuleNode(PerWorkingCopyInfo perWorkingCopyInfo,
+ GroovyCompilationUnitDeclaration compilationUnitDeclaration) {
+ if (compilationUnitDeclaration != null) {
+ moduleNode = compilationUnitDeclaration.getModuleNode();
+ moduleNode.setDescription(this.name);
+ JDTResolver resolver;
+ if (ModuleNodeMapper.shouldStoreResovler()) {
+ resolver = (JDTResolver) compilationUnitDeclaration.getCompilationUnit().getResolveVisitor();
+ } else {
+ resolver = null;
+ }
+
+ moduleNodeInfo = new ModuleNodeInfo(moduleNode, resolver);
+ }
+ }
+
+ @Override
+ public ModuleNodeInfo getModuleInfo(boolean force) {
+ return moduleNodeInfo;
+ }
+
+ @Override
+ public ModuleNodeInfo getNewModuleInfo() {
+ return moduleNodeInfo;
+ }
+
+ /**
+ * ModuleNode is not cached in the Mapper, but rather cached locally
+ */
+ @Override
+ public ModuleNode getModuleNode() {
+ // ensure moduleNode is initialized
+ getPerWorkingCopyInfo();
+ return moduleNode;
+ }
+
+ @Override
+ public IResource resource() {
+ return getJavaProject().getResource();
+ }
+
+ @Override
+ public char[] getFileName() {
+ return name.toCharArray();
+ }
+
+ @Override
+ public boolean isOnBuildPath() {
+ // a call to super.isOnBuildPath() will always return false,
+ // but it should be true
+ return true;
+ }
+ // GROOVY End
+}
@@ -40,7 +40,7 @@
*/
public class ModuleNodeMapper {
- public class ModuleNodeInfo {
+ public static class ModuleNodeInfo {
public ModuleNodeInfo(ModuleNode module, JDTResolver resolver) {
this.module = module;
this.resolver = resolver;
@@ -81,7 +81,7 @@ void store(PerWorkingCopyInfo info, ModuleNode module, JDTResolver resolver) {
DSL_BUNDLE_INSTALLED = result;
}
- private boolean shouldStoreResovler() {
+ public static boolean shouldStoreResovler() {
return DSL_BUNDLE_INSTALLED;
}
@@ -12,6 +12,8 @@
package org.codehaus.jdt.groovy.integration;
import org.eclipse.core.resources.IProject;
+import org.eclipse.jdt.core.IJavaElement;
+import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.WorkingCopyOwner;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchPattern;
@@ -21,11 +23,13 @@
import org.eclipse.jdt.internal.compiler.ISourceElementRequestor;
import org.eclipse.jdt.internal.compiler.SourceElementParser;
import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
+import org.eclipse.jdt.internal.compiler.env.IBinaryType;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.parser.Parser;
import org.eclipse.jdt.internal.compiler.problem.ProblemReporter;
import org.eclipse.jdt.internal.core.BinaryType;
+import org.eclipse.jdt.internal.core.ClassFile;
import org.eclipse.jdt.internal.core.CompilationUnit;
import org.eclipse.jdt.internal.core.JavaProject;
import org.eclipse.jdt.internal.core.PackageFragment;
@@ -123,4 +127,12 @@ public IJavaSearchScope expandSearchScope(IJavaSearchScope scope, SearchPattern
// never expand
return scope;
}
+
+ public boolean isInterestingBinary(BinaryType type, IBinaryType typeInfo) {
+ return false;
+ }
+
+ public IJavaElement[] binaryCodeSelect(ClassFile classFile, int offset, int length, WorkingCopyOwner owner) throws JavaModelException {
+ return new IJavaElement[0];
+ }
}
Oops, something went wrong.

0 comments on commit 1d101e7

Please sign in to comment.