Example usage for org.eclipse.jdt.core IJavaElement COMPILATION_UNIT

List of usage examples for org.eclipse.jdt.core IJavaElement COMPILATION_UNIT

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement COMPILATION_UNIT.

Prototype

int COMPILATION_UNIT

To view the source code for org.eclipse.jdt.core IJavaElement COMPILATION_UNIT.

Click Source Link

Document

Constant representing a Java compilation unit.

Usage

From source file:jp.littleforest.pathtools.handlers.CopyQualifiedNameHandler.java

License:Open Source License

protected String getQualifiedClassName(IClassFile classFile) {
    String fqcn = StringUtil.trimSuffix(classFile.getElementName(), ".class");
    IJavaElement parent = classFile.getParent();
    if (parent == null) {
        return fqcn;
    }/* w ww  .ja va 2  s .  c  om*/
    int parentType = parent.getElementType();
    if (parentType == IJavaElement.TYPE) {
        // ??
        String enclosingQn = getQualifiedName(parent);
        fqcn = enclosingQn + "$" + fqcn;
    } else if (parentType == IJavaElement.COMPILATION_UNIT) {
        // ??
        fqcn = getQualifiedClassName((ICompilationUnit) parent);
    } else if (parentType == IJavaElement.PACKAGE_FRAGMENT) {
        fqcn = getQualifiedPackageName(parent) + "." + fqcn;
    }
    return fqcn;
}

From source file:mychangedetector.copyclasses.MyRenameLinkedMode.java

License:Open Source License

/**
 * Creates a rename descriptor./*from  w  w w .  j  a  v a2 s.  c o  m*/
 * 
 * @param javaElement element to rename 
 * @param newName new name
 * @return a rename descriptor with current settings as used in the refactoring dialogs
 * @throws JavaModelException if an error occurs while accessing the element
 */
private RenameJavaElementDescriptor createRenameDescriptor(IJavaElement javaElement, String newName)
        throws JavaModelException {
    String contributionId;
    // see RefactoringExecutionStarter#createRenameSupport(..):
    int elementType = javaElement.getElementType();
    switch (elementType) {
    case IJavaElement.JAVA_PROJECT:
        contributionId = IJavaRefactorings.RENAME_JAVA_PROJECT;
        break;
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
        contributionId = IJavaRefactorings.RENAME_SOURCE_FOLDER;
        break;
    case IJavaElement.PACKAGE_FRAGMENT:
        contributionId = IJavaRefactorings.RENAME_PACKAGE;
        break;
    case IJavaElement.COMPILATION_UNIT:
        contributionId = IJavaRefactorings.RENAME_COMPILATION_UNIT;
        break;
    case IJavaElement.TYPE:
        contributionId = IJavaRefactorings.RENAME_TYPE;
        break;
    case IJavaElement.METHOD:
        final IMethod method = (IMethod) javaElement;
        if (method.isConstructor())
            return createRenameDescriptor(method.getDeclaringType(), newName);
        else
            contributionId = IJavaRefactorings.RENAME_METHOD;
        break;
    case IJavaElement.FIELD:
        IField field = (IField) javaElement;
        if (field.isEnumConstant())
            contributionId = IJavaRefactorings.RENAME_ENUM_CONSTANT;
        else
            contributionId = IJavaRefactorings.RENAME_FIELD;
        break;
    case IJavaElement.TYPE_PARAMETER:
        contributionId = IJavaRefactorings.RENAME_TYPE_PARAMETER;
        break;
    case IJavaElement.LOCAL_VARIABLE:
        contributionId = IJavaRefactorings.RENAME_LOCAL_VARIABLE;
        break;
    default:
        return null;
    }

    RenameJavaElementDescriptor descriptor = (RenameJavaElementDescriptor) RefactoringCore
            .getRefactoringContribution(contributionId).createDescriptor();
    descriptor.setJavaElement(javaElement);
    descriptor.setNewName(newName);
    if (elementType != IJavaElement.PACKAGE_FRAGMENT_ROOT)
        descriptor.setUpdateReferences(true);

    IDialogSettings javaSettings = JavaPlugin.getDefault().getDialogSettings();
    IDialogSettings refactoringSettings = javaSettings.getSection(RefactoringWizardPage.REFACTORING_SETTINGS); //TODO: undocumented API
    if (refactoringSettings == null) {
        refactoringSettings = javaSettings.addNewSection(RefactoringWizardPage.REFACTORING_SETTINGS);
    }

    switch (elementType) {
    case IJavaElement.METHOD:
    case IJavaElement.FIELD:
        descriptor.setDeprecateDelegate(refactoringSettings.getBoolean(DelegateUIHelper.DELEGATE_DEPRECATION));
        descriptor.setKeepOriginal(refactoringSettings.getBoolean(DelegateUIHelper.DELEGATE_UPDATING));
    }
    switch (elementType) {
    case IJavaElement.TYPE:
        //         case IJavaElement.COMPILATION_UNIT: // TODO
        descriptor.setUpdateSimilarDeclarations(
                refactoringSettings.getBoolean(RenameRefactoringWizard.TYPE_UPDATE_SIMILAR_ELEMENTS));
        int strategy;
        try {
            strategy = refactoringSettings.getInt(RenameRefactoringWizard.TYPE_SIMILAR_MATCH_STRATEGY);
        } catch (NumberFormatException e) {
            strategy = RenamingNameSuggestor.STRATEGY_EXACT;
        }
        descriptor.setMatchStrategy(strategy);
    }
    switch (elementType) {
    case IJavaElement.PACKAGE_FRAGMENT:
        descriptor.setUpdateHierarchy(
                refactoringSettings.getBoolean(RenameRefactoringWizard.PACKAGE_RENAME_SUBPACKAGES));
    }
    switch (elementType) {
    case IJavaElement.PACKAGE_FRAGMENT:
    case IJavaElement.TYPE:
        String fileNamePatterns = refactoringSettings.get(RenameRefactoringWizard.QUALIFIED_NAMES_PATTERNS);
        if (fileNamePatterns != null && fileNamePatterns.length() != 0) {
            descriptor.setFileNamePatterns(fileNamePatterns);
            boolean updateQualifiedNames = refactoringSettings
                    .getBoolean(RenameRefactoringWizard.UPDATE_QUALIFIED_NAMES);
            descriptor.setUpdateQualifiedNames(updateQualifiedNames);
            fShowPreview |= updateQualifiedNames;
        }
    }
    switch (elementType) {
    case IJavaElement.PACKAGE_FRAGMENT:
    case IJavaElement.TYPE:
    case IJavaElement.FIELD:
        boolean updateTextualOccurrences = refactoringSettings
                .getBoolean(RenameRefactoringWizard.UPDATE_TEXTUAL_MATCHES);
        descriptor.setUpdateTextualOccurrences(updateTextualOccurrences);
        fShowPreview |= updateTextualOccurrences;
    }
    switch (elementType) {
    case IJavaElement.FIELD:
        descriptor
                .setRenameGetters(refactoringSettings.getBoolean(RenameRefactoringWizard.FIELD_RENAME_GETTER));
        descriptor
                .setRenameSetters(refactoringSettings.getBoolean(RenameRefactoringWizard.FIELD_RENAME_SETTER));
    }
    return descriptor;
}

From source file:navclus.userinterface.monitor.patterns.PatternPresenter.java

License:Open Source License

public void show(LinkedList<IJavaElement> selectedElements) throws JavaModelException {
    for (IJavaElement javaElement : selectedElements) {
        if (javaElement == null)
            continue;

        switch (javaElement.getElementType()) {
        case (IJavaElement.METHOD):
        case (IJavaElement.FIELD):
            // show the elements in a class figure
            rootmodel.addMember(javaElement);
            break;
        case (IJavaElement.TYPE):
            rootmodel.createType((IType) javaElement);
            break;
        case (IJavaElement.COMPILATION_UNIT):
            rootmodel.openCU((ICompilationUnit) javaElement);
            break;
        }/*w w w .  j av a2 s .  c  o m*/
    }
    rootmodel.drawNodes();
    (new RedrawAction()).run();
}

From source file:net.harawata.mybatipse.bean.BeanPropertyCache.java

License:Open Source License

protected static void parseSource(IJavaProject project, final IType type,
        final Map<String, String> readableFields, final Map<String, String> writableFields)
        throws JavaModelException {
    ICompilationUnit compilationUnit = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(compilationUnit);/*  w w  w .ja  v a  2s.  c  o  m*/
    parser.setResolveBindings(true);
    // parser.setIgnoreMethodBodies(true);
    CompilationUnit astUnit = (CompilationUnit) parser.createAST(null);
    astUnit.accept(new BeanPropertyVisitor(project, readableFields, writableFields));
}

From source file:net.harawata.mybatipse.mybatis.JavaMapperUtil.java

License:Open Source License

private static void findMapperMethodSource(MapperMethodStore methodStore, IJavaProject project,
        String mapperFqn, MethodMatcher annotationFilter, IType mapperType) {
    ICompilationUnit compilationUnit = (ICompilationUnit) mapperType.getAncestor(IJavaElement.COMPILATION_UNIT);
    ASTParser parser = ASTParser.newParser(AST.JLS4);
    parser.setKind(ASTParser.K_COMPILATION_UNIT);
    parser.setSource(compilationUnit);//www  .  j  a va2  s .c  om
    parser.setResolveBindings(true);
    // parser.setIgnoreMethodBodies(true);
    CompilationUnit astUnit = (CompilationUnit) parser.createAST(null);
    astUnit.accept(new JavaMapperVisitor(methodStore, project, mapperFqn, annotationFilter));
}

From source file:net.harawata.mybatipse.wizard.NewXmlMapperWizardPage.java

License:Open Source License

public NewXmlMapperWizardPage(IStructuredSelection selection) {
    super("NewXmlMapperWizardPage", selection);
    setTitle("New MyBatis XML Mapper");
    setDescription("Create a new MyBatis XML Mapper file.");
    setFileExtension(EXT);//  w ww. j  av a  2s .  co m
    // Propose file name
    Object elem = selection.getFirstElement();
    if (elem != null) {
        if (elem instanceof IJavaElement) {
            int javaElemType = ((IJavaElement) elem).getElementType();
            if (javaElemType == IJavaElement.COMPILATION_UNIT) {
                setFileNameWithExtension(((ICompilationUnit) elem).getElementName());
            } else if (javaElemType == IJavaElement.CLASS_FILE) {
                setFileNameWithExtension(((IClassFile) elem).getElementName());
            } else if (javaElemType == IJavaElement.TYPE) {
                setFileNameWithExtension(((IType) elem).getElementName());
            }
        } else if (elem instanceof IFile) {
            setFileNameWithExtension(((IFile) elem).getFullPath().removeFileExtension().lastSegment());
        } else {
            // Leave empty.
        }
    }
}

From source file:net.hillsdon.testlink.model.impl.JavaSelection.java

License:Open Source License

/**
 * Find the type best associated with the java element.
 *
 * @param javaElement A java element./*  ww  w. j a v  a2  s.c  o m*/
 * @return A type, or null if we can't figure one out.
 * @throws JavaModelException If we fail to read from the java model.
 */
public IType getSelectedType() throws JavaModelException {
    final IJavaElement javaElement = getSelectedJavaElement();
    IType type = null;
    if (javaElement != null) {
        type = getOutermostType(javaElement);
        if (type == null) {
            ICompilationUnit cu = (ICompilationUnit) javaElement.getAncestor(IJavaElement.COMPILATION_UNIT);
            if (cu != null) {
                IType[] types = cu.getTypes();
                if (types.length > 0) {
                    type = types[0];
                }
            }
        }
    }
    return type;
}

From source file:net.sf.commonclipse.Generator.java

License:Apache License

/**
 * Generates the appropriate method in <code>type</code>.
 * @param type IType/*from  w ww.  ja  v a2s.  c  om*/
 * @param shell Shell
 */
public void generate(IType type, Shell shell) {

    ICompilationUnit cu = (ICompilationUnit) type.getAncestor(IJavaElement.COMPILATION_UNIT);

    // first check if file is writable
    IResource resource;
    try {
        resource = cu.getCorrespondingResource();
    } catch (JavaModelException e) {
        MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), e.getMessage()); //$NON-NLS-1$
        return;
    }
    if (resource != null && resource.getResourceAttributes().isReadOnly()) {
        IStatus status = ResourcesPlugin.getWorkspace().validateEdit(new IFile[] { (IFile) resource }, shell);

        if (!status.isOK()) {
            MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), CCMessages //$NON-NLS-1$
                    .getString("Generator.readonly")); //$NON-NLS-1$
            return;
        }
    }

    resource = null;

    if (!validate(type, shell)) {
        return;
    }

    ProgressMonitorDialog progressDialog = new ProgressMonitorDialog(shell);
    progressDialog.open();
    try {

        IProgressMonitor monitor = progressDialog.getProgressMonitor();
        generateMethod(type, cu, shell, monitor);
    } catch (JavaModelException ex) {
        MessageDialog.openError(shell, CCMessages.getString("Generator.errortitle"), ex.getMessage()); //$NON-NLS-1$
    }

    progressDialog.close();
}

From source file:net.sourceforge.c4jplugin.internal.ui.contracthierarchy.ContractHierarchyLifeCycle.java

License:Open Source License

private void processDelta(IJavaElementDelta delta, ArrayList<IType> changedTypes) {
    IJavaElement element = delta.getElement();
    switch (element.getElementType()) {
    case IJavaElement.TYPE:
        processTypeDelta((IType) element, changedTypes);
        processChildrenDelta(delta, changedTypes); // (inner types)
        break;//from  w  ww  .  ja  va 2s. c  om
    case IJavaElement.JAVA_MODEL:
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT:
        processChildrenDelta(delta, changedTypes);
        break;
    case IJavaElement.COMPILATION_UNIT:
        ICompilationUnit cu = (ICompilationUnit) element;
        if (cu.getOwner() != null) {
            return;
        }

        if (delta.getKind() == IJavaElementDelta.CHANGED && isPossibleStructuralChange(delta.getFlags())) {
            try {
                if (cu.exists()) {
                    IType[] types = cu.getAllTypes();
                    for (int i = 0; i < types.length; i++) {
                        processTypeDelta(types[i], changedTypes);
                    }
                }
            } catch (JavaModelException e) {
                C4JActivator.log(e);
            }
        } else {
            processChildrenDelta(delta, changedTypes);
        }
        break;
    case IJavaElement.CLASS_FILE:
        if (delta.getKind() == IJavaElementDelta.CHANGED) {
            try {
                IType type = ((IClassFile) element).getType();
                processTypeDelta(type, changedTypes);
            } catch (JavaModelException e) {
                C4JActivator.log(e);
            }
        } else {
            processChildrenDelta(delta, changedTypes);
        }
        break;
    }
}

From source file:net.sourceforge.metrics.core.sources.AbstractMetricSource.java

License:Open Source License

/**
 * get the ICompilationUnit for the source
 * /*from   www  .  j  a v a2 s. c om*/
 * @return ICompilationUnit
 */
public ICompilationUnit getCompilationUnit() {
    IJavaElement input = getJavaElement();
    if (input.getElementType() == IJavaElement.COMPILATION_UNIT) {
        return (ICompilationUnit) input;
    } /* else { */
    return (ICompilationUnit) input.getAncestor(IJavaElement.COMPILATION_UNIT);
    /* } */
}