Example usage for org.eclipse.jdt.internal.core CompilationUnit CompilationUnit

List of usage examples for org.eclipse.jdt.internal.core CompilationUnit CompilationUnit

Introduction

In this page you can find the example usage for org.eclipse.jdt.internal.core CompilationUnit CompilationUnit.

Prototype

public CompilationUnit(PackageFragment parent, String name, WorkingCopyOwner owner) 

Source Link

Document

Constructs a handle to a compilation unit with the given name in the specified package for the specified owner

Usage

From source file:io.sarl.eclipse.wizards.elements.AbstractNewSarlElementWizardPage.java

License:Apache License

private ICompilationUnit getCompilationUnitStub() {
    final String compilationUnitName = getCompilationUnitName(getTypeName());
    return new CompilationUnit((PackageFragment) getPackageFragment(), compilationUnitName,
            DefaultWorkingCopyOwner.PRIMARY);
}

From source file:org.codehaus.jdt.groovy.integration.DefaultLanguageSupport.java

License:Open Source License

public CompilationUnit newCompilationUnit(PackageFragment parent, String name, WorkingCopyOwner owner) {
    return new CompilationUnit(parent, name, owner);
}

From source file:org.codehaus.jdt.groovy.integration.internal.GroovyLanguageSupport.java

License:Open Source License

public CompilationUnit newCompilationUnit(PackageFragment parent, String name, WorkingCopyOwner owner) {

    // should use a content type here
    if (ContentTypeUtils.isGroovyLikeFileName(name)) {
        return new GroovyCompilationUnit(parent, name, owner);
    } else {//from   w  w w  .  ja v  a2  s  .  c  o m
        return new CompilationUnit(parent, name, owner);
    }
}

From source file:org.eclipse.ajdt.core.text.ITDCodeSelection.java

License:Open Source License

public IJavaElement[] findJavaElement(IRegion wordRegion) throws JavaModelException {
    JavaProject javaProject = (JavaProject) unit.getJavaProject();
    SearchableEnvironment environment = new ITDAwareNameEnvironment(javaProject, unit.getOwner(), null);

    ITDAwareSelectionRequestor requestor = new ITDAwareSelectionRequestor(
            AJProjectModelFactory.getInstance().getModelForJavaElement(javaProject), unit);
    /* AJDT 1.7 */
    SelectionEngine engine = new SelectionEngine(environment, requestor, javaProject.getOptions(true),
            unit.getOwner());// w  w  w  . j av  a2 s.c  o m

    final AspectsConvertingParser converter = new AspectsConvertingParser(
            ((CompilationUnit) unit).getContents());
    converter.setUnit(unit);
    ArrayList<Replacement> replacements = converter.convert(ConversionOptions.CODE_COMPLETION);

    org.eclipse.jdt.internal.compiler.env.ICompilationUnit wrappedUnit = new CompilationUnit(
            (PackageFragment) unit.getParent(), unit.getElementName(), unit.getOwner()) {
        public char[] getContents() {
            return converter.content;
        }
    };
    int transformedStart = AspectsConvertingParser.translatePositionToAfterChanges(wordRegion.getOffset(),
            replacements);
    int transformedEnd = AspectsConvertingParser
            .translatePositionToAfterChanges(wordRegion.getOffset() + wordRegion.getLength(), replacements) - 1;
    requestor.setReplacements(replacements);
    engine.select(wrappedUnit, transformedStart, transformedEnd);

    // maybe perform code select again.  If we are inside of an ITD method
    // must check for ITD references to the target type
    IntertypeElement itd = itdOrNull(unit, wordRegion.getOffset());
    if (itd != null && (itd.getAJKind() == IProgramElement.Kind.INTER_TYPE_METHOD
            || itd.getAJKind() == IProgramElement.Kind.INTER_TYPE_CONSTRUCTOR)) {
        char[] targetType = itd.getTargetType();

        final AspectsConvertingParser converter2 = new AspectsConvertingParser(
                ((CompilationUnit) unit).getContents());
        converter2.setUnit(unit);
        ArrayList<Replacement> replacements2 = converter2.convert(
                ConversionOptions.getCodeCompletionOptionWithContextSwitch(wordRegion.getOffset(), targetType));
        wrappedUnit = new CompilationUnit((PackageFragment) unit.getParent(), unit.getElementName(),
                unit.getOwner()) {
            public char[] getContents() {
                return converter2.content;
            }
        };
        transformedStart = AspectsConvertingParser.translatePositionToAfterChanges(wordRegion.getOffset(),
                replacements2);
        transformedEnd = AspectsConvertingParser.translatePositionToAfterChanges(
                wordRegion.getOffset() + wordRegion.getLength(), replacements2) - 1;
        requestor.setReplacements(replacements2);

        SelectionEngine engine2 = new SelectionEngine(environment, requestor, javaProject.getOptions(true),
                unit.getOwner());
        engine2.select(wrappedUnit, transformedStart, transformedEnd);
    }

    IJavaElement[] elements = requestor.getElements();
    if (itd != null && elements.length == 0) {
        // maybe we are selecting on the name of the itd itself
        ISourceRange nameRange = itd.getNameRange();
        if (nameRange.getOffset() <= wordRegion.getOffset() && (nameRange.getOffset()
                + nameRange.getLength()) >= (wordRegion.getOffset() + wordRegion.getLength())) {
            elements = new IJavaElement[] { itd };
        }

        // maybe we are selecting the target type of the itd
        ISourceRange targetNameRange = itd.getTargetTypeSourceRange();
        if (targetNameRange.getOffset() <= wordRegion.getOffset() && (targetNameRange.getOffset()
                + targetNameRange.getLength()) >= (wordRegion.getOffset() + wordRegion.getLength())) {
            IType targetType = itd.findTargetType();
            if (targetType != null) { // will be null if model not initialized
                elements = new IJavaElement[] { targetType };
            }
        }
    }
    return elements;
}

From source file:org.eclipse.jdt.internal.core.CompilationUnit.java

License:Open Source License

public CompilationUnit cloneCachingContents() {
    return new CompilationUnit((PackageFragment) this.parent, this.name, this.owner) {
        private char[] cachedContents;

        public char[] getContents() {
            if (this.cachedContents == null)
                this.cachedContents = CompilationUnit.this.getContents();
            return this.cachedContents;
        }/*w w  w.ja  v  a  2s  .  c  o m*/

        public CompilationUnit originalFromClone() {
            return CompilationUnit.this;
        }
    };
}

From source file:org.eclipse.xtend.ide.wizards.AbstractNewXtendElementWizardPage.java

License:Open Source License

private ICompilationUnit getCompilationUnitStub() {
    String compilationUnitName = getCompilationUnitName(getTypeName());
    return new CompilationUnit((PackageFragment) getPackageFragment(), compilationUnitName,
            DefaultWorkingCopyOwner.PRIMARY);
}

From source file:org.jboss.tools.common.java.test.FakeAnnotationImpl.java

License:Open Source License

@Override
public IJavaElement getAncestor(int ancestorType) {
    if (ancestorType == IJavaElement.COMPILATION_UNIT) {
        return new CompilationUnit(null, null, null) {
            public IImportDeclaration[] getImports() throws JavaModelException {
                return new IImportDeclaration[0];
            }// w w w  . jav a  2s  .  c  om
        };
    }
    if (ancestorType == IJavaElement.TYPE) {
        class FakeSourceType extends SourceType {
            FakeSourceType() {
                super(null, "FakeType");
            }
        }
        return new FakeSourceType();
    }
    return null;
}