Search in sources :

Example 1 with SourceField

use of org.eclipse.jdt.internal.core.SourceField in project che by eclipse.

the class SourceTypeConverter method convert.

/*
	 * Convert a source element type into a parsed type declaration
	 */
private TypeDeclaration convert(SourceType typeHandle, CompilationResult compilationResult) throws JavaModelException {
    SourceTypeElementInfo typeInfo = (SourceTypeElementInfo) typeHandle.getElementInfo();
    if (typeInfo.isAnonymousMember())
        throw new AnonymousMemberFound();
    /* create type declaration - can be member type */
    TypeDeclaration type = new TypeDeclaration(compilationResult);
    if (typeInfo.getEnclosingType() == null) {
        if (typeHandle.isAnonymous()) {
            type.name = CharOperation.NO_CHAR;
            type.bits |= (ASTNode.IsAnonymousType | ASTNode.IsLocalType);
        } else {
            if (typeHandle.isLocal()) {
                type.bits |= ASTNode.IsLocalType;
            }
        }
    } else {
        type.bits |= ASTNode.IsMemberType;
    }
    if ((type.bits & ASTNode.IsAnonymousType) == 0) {
        type.name = typeInfo.getName();
    }
    type.name = typeInfo.getName();
    // only positions available
    int start, end;
    type.sourceStart = start = typeInfo.getNameSourceStart();
    type.sourceEnd = end = typeInfo.getNameSourceEnd();
    type.modifiers = typeInfo.getModifiers();
    type.declarationSourceStart = typeInfo.getDeclarationSourceStart();
    type.declarationSourceEnd = typeInfo.getDeclarationSourceEnd();
    type.bodyEnd = type.declarationSourceEnd;
    // convert 1.5 specific constructs only if compliance is 1.5 or above
    if (this.has1_5Compliance) {
        /* convert annotations */
        type.annotations = convertAnnotations(typeHandle);
    }
    /* https://bugs.eclipse.org/bugs/show_bug.cgi?id=324850, even in a 1.4 project, we
		   must internalize type variables and observe any parameterization of super class
		   and/or super interfaces in order to be able to detect overriding in the presence
		   of generics.
		 */
    char[][] typeParameterNames = typeInfo.getTypeParameterNames();
    if (typeParameterNames.length > 0) {
        int parameterCount = typeParameterNames.length;
        char[][][] typeParameterBounds = typeInfo.getTypeParameterBounds();
        type.typeParameters = new TypeParameter[parameterCount];
        for (int i = 0; i < parameterCount; i++) {
            type.typeParameters[i] = createTypeParameter(typeParameterNames[i], typeParameterBounds[i], start, end);
        }
    }
    /* set superclass and superinterfaces */
    if (typeInfo.getSuperclassName() != null) {
        type.superclass = createTypeReference(typeInfo.getSuperclassName(), start, end, true);
        type.superclass.bits |= ASTNode.IsSuperType;
    }
    char[][] interfaceNames = typeInfo.getInterfaceNames();
    int interfaceCount = interfaceNames == null ? 0 : interfaceNames.length;
    if (interfaceCount > 0) {
        type.superInterfaces = new TypeReference[interfaceCount];
        for (int i = 0; i < interfaceCount; i++) {
            type.superInterfaces[i] = createTypeReference(interfaceNames[i], start, end, true);
            type.superInterfaces[i].bits |= ASTNode.IsSuperType;
        }
    }
    /* convert member types */
    if ((this.flags & MEMBER_TYPE) != 0) {
        SourceType[] sourceMemberTypes = typeInfo.getMemberTypeHandles();
        int sourceMemberTypeCount = sourceMemberTypes.length;
        type.memberTypes = new TypeDeclaration[sourceMemberTypeCount];
        for (int i = 0; i < sourceMemberTypeCount; i++) {
            type.memberTypes[i] = convert(sourceMemberTypes[i], compilationResult);
            type.memberTypes[i].enclosingType = type;
        }
    }
    /* convert intializers and fields*/
    InitializerElementInfo[] initializers = null;
    int initializerCount = 0;
    if ((this.flags & LOCAL_TYPE) != 0) {
        initializers = typeInfo.getInitializers();
        initializerCount = initializers.length;
    }
    SourceField[] sourceFields = null;
    int sourceFieldCount = 0;
    if ((this.flags & FIELD) != 0) {
        sourceFields = typeInfo.getFieldHandles();
        sourceFieldCount = sourceFields.length;
    }
    int length = initializerCount + sourceFieldCount;
    if (length > 0) {
        type.fields = new FieldDeclaration[length];
        for (int i = 0; i < initializerCount; i++) {
            type.fields[i] = convert(initializers[i], compilationResult);
        }
        int index = 0;
        for (int i = initializerCount; i < length; i++) {
            type.fields[i] = convert(sourceFields[index++], type, compilationResult);
        }
    }
    /* convert methods - need to add default constructor if necessary */
    boolean needConstructor = (this.flags & CONSTRUCTOR) != 0;
    boolean needMethod = (this.flags & METHOD) != 0;
    if (needConstructor || needMethod) {
        SourceMethod[] sourceMethods = typeInfo.getMethodHandles();
        int sourceMethodCount = sourceMethods.length;
        /* source type has a constructor ?           */
        /* by default, we assume that one is needed. */
        int extraConstructor = 0;
        int methodCount = 0;
        int kind = TypeDeclaration.kind(type.modifiers);
        boolean isAbstract = kind == TypeDeclaration.INTERFACE_DECL || kind == TypeDeclaration.ANNOTATION_TYPE_DECL;
        if (!isAbstract) {
            extraConstructor = needConstructor ? 1 : 0;
            for (int i = 0; i < sourceMethodCount; i++) {
                if (sourceMethods[i].isConstructor()) {
                    if (needConstructor) {
                        // Does not need the extra constructor since one constructor already exists.
                        extraConstructor = 0;
                        methodCount++;
                    }
                } else if (needMethod) {
                    methodCount++;
                }
            }
        } else {
            methodCount = needMethod ? sourceMethodCount : 0;
        }
        type.methods = new AbstractMethodDeclaration[methodCount + extraConstructor];
        if (extraConstructor != 0) {
            // add default constructor in first position
            type.methods[0] = type.createDefaultConstructor(false, false);
        }
        int index = 0;
        boolean hasAbstractMethods = false;
        for (int i = 0; i < sourceMethodCount; i++) {
            SourceMethod sourceMethod = sourceMethods[i];
            SourceMethodElementInfo methodInfo = (SourceMethodElementInfo) sourceMethod.getElementInfo();
            boolean isConstructor = methodInfo.isConstructor();
            if ((methodInfo.getModifiers() & ClassFileConstants.AccAbstract) != 0) {
                hasAbstractMethods = true;
            }
            if ((isConstructor && needConstructor) || (!isConstructor && needMethod)) {
                AbstractMethodDeclaration method = convert(sourceMethod, methodInfo, compilationResult);
                if (isAbstract || method.isAbstract()) {
                    // fix-up flag
                    method.modifiers |= ExtraCompilerModifiers.AccSemicolonBody;
                }
                type.methods[extraConstructor + index++] = method;
            }
        }
        if (hasAbstractMethods)
            type.bits |= ASTNode.HasAbstractMethods;
    }
    return type;
}
Also used : SourceMethodElementInfo(org.eclipse.jdt.internal.core.SourceMethodElementInfo) ISourceType(org.eclipse.jdt.internal.compiler.env.ISourceType) SourceType(org.eclipse.jdt.internal.core.SourceType) SourceField(org.eclipse.jdt.internal.core.SourceField) InitializerElementInfo(org.eclipse.jdt.internal.core.InitializerElementInfo) SourceTypeElementInfo(org.eclipse.jdt.internal.core.SourceTypeElementInfo) SourceMethod(org.eclipse.jdt.internal.core.SourceMethod) TypeDeclaration(org.eclipse.jdt.internal.compiler.ast.TypeDeclaration) AbstractMethodDeclaration(org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)

Example 2 with SourceField

use of org.eclipse.jdt.internal.core.SourceField in project tdi-studio-se by Talend.

the class TalendJavaCompletionProcessor method sortProposals.

/*
     * (non-Javadoc)
     * 
     * @see org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor#filterAndSortProposals(java.util.List,
     * org.eclipse.core.runtime.IProgressMonitor, org.eclipse.jdt.ui.text.java.ContentAssistInvocationContext)
     */
@Override
protected List<ICompletionProposal> sortProposals(List<ICompletionProposal> proposals, IProgressMonitor monitor, ContentAssistInvocationContext context) {
    proposals = super.sortProposals(proposals, monitor, context);
    Map<String, ICompletionProposal> completionProposalMap = new HashMap<String, ICompletionProposal>();
    boolean globalFieldsDone = false;
    for (Object o : proposals) {
        ICompletionProposal proposal = (ICompletionProposal) o;
        String longna = proposal.getDisplayString();
        //$NON-NLS-1$
        int indexna = longna.indexOf("-");
        boolean shouldRemove = false;
        if (indexna > 0) {
            if (longna.substring(indexna + 2, longna.length()).equals(TalendJavaSourceViewer.getClassName())) {
                shouldRemove = true;
            }
        }
        if (proposal instanceof JavaCompletionProposal) {
            JavaCompletionProposal javaProposal = ((JavaCompletionProposal) proposal);
            if (javaProposal.getJavaElement() instanceof SourceField) {
                globalFieldsDone = true;
            }
            if (javaProposal.getJavaElement() == null && globalFieldsDone) {
                shouldRemove = true;
            }
        }
        if (proposal.getDisplayString().startsWith(TalendJavaSourceViewer.VIEWER_CLASS_NAME)) {
            shouldRemove = true;
        }
        if (!shouldRemove) {
            completionProposalMap.put(longna, proposal);
        }
    }
    List<ICompletionProposal> newProposals = new ArrayList<ICompletionProposal>(completionProposalMap.values());
    Collections.sort(newProposals, new Comparator<ICompletionProposal>() {

        @Override
        public int compare(ICompletionProposal arg0, ICompletionProposal arg1) {
            if (arg1 instanceof TalendCompletionProposal && (!(arg0 instanceof TalendCompletionProposal))) {
                return 1;
            } else if (arg0 instanceof TalendCompletionProposal && (!(arg1 instanceof TalendCompletionProposal))) {
                return -1;
            } else if (arg1 instanceof TalendCompletionProposal && (arg0 instanceof TalendCompletionProposal)) {
                TalendCompletionProposal a = (TalendCompletionProposal) arg0;
                TalendCompletionProposal b = (TalendCompletionProposal) arg1;
                return a.getType().compareTo(b.getType());
            }
            return 0;
        }
    });
    return newProposals;
}
Also used : HashMap(java.util.HashMap) SourceField(org.eclipse.jdt.internal.core.SourceField) ArrayList(java.util.ArrayList) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) TalendCompletionProposal(org.talend.designer.core.ui.viewer.proposal.TalendCompletionProposal) JavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal)

Example 3 with SourceField

use of org.eclipse.jdt.internal.core.SourceField in project tdi-studio-se by Talend.

the class TalendJavaSourceViewer method createViewerForComponent.

public static ISourceViewer createViewerForComponent(Composite composite, int styles, CompilationUnitEditor editor, List<Variable> variableList, String uniqueName, String codePart) {
    //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    String selectedPart = "[" + uniqueName + " " + codePart + " ] start";
    IDocument originalDocument = editor.getDocumentProvider().getDocument(editor.getEditorInput());
    int documentOffset = -1;
    //$NON-NLS-1$
    String globalFields = "";
    //$NON-NLS-1$
    String localFields = "";
    //$NON-NLS-1$
    globalFields = "\tprivate static java.util.Properties context = new java.util.Properties();\n";
    //$NON-NLS-1$
    globalFields += "\tprivate static final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();\n";
    IDocument newDoc = new Document();
    boolean checkCode = false;
    FindReplaceDocumentAdapter frda = null;
    // hywang modified for bug 9180
    String documentText = originalDocument.get();
    if (documentText != null && !documentText.equals("") && documentText.length() > 0) {
        //$NON-NLS-1$
        frda = new FindReplaceDocumentAdapter(originalDocument);
    }
    try {
        Region region = null;
        if (frda != null) {
            region = (Region) frda.find(0, selectedPart, true, false, false, false);
        }
        if (region != null) {
            checkCode = true;
            documentOffset = region.getOffset();
            documentOffset = originalDocument.getLineOffset(originalDocument.getLineOfOffset(documentOffset) + 2);
            JavaCompletionProcessor processor = new JavaCompletionProcessor(editor, new ContentAssistant(), IDocument.DEFAULT_CONTENT_TYPE);
            boolean globalFieldsDone = false;
            //$NON-NLS-1$
            globalFields = "";
            String className = editor.getPartName().substring(0, editor.getPartName().indexOf('.') + 1);
            ICompletionProposal[] proposals = processor.computeCompletionProposals(editor.getViewer(), documentOffset);
            for (ICompletionProposal curProposal : proposals) {
                if (curProposal instanceof JavaCompletionProposal) {
                    JavaCompletionProposal javaProposal = ((JavaCompletionProposal) curProposal);
                    if (javaProposal.getJavaElement() instanceof SourceField) {
                        globalFieldsDone = true;
                        SourceField sourceField = (SourceField) javaProposal.getJavaElement();
                        //$NON-NLS-1$ //$NON-NLS-2$
                        globalFields += "\t" + sourceField.getSource() + "\n";
                    // System.out.println();
                    }
                    if (javaProposal.getJavaElement() == null && !globalFieldsDone) {
                        //$NON-NLS-1$
                        String[] str = javaProposal.getSortString().split(" ");
                        //$NON-NLS-1$
                        String variable = "";
                        for (int i = str.length - 1; i >= 0; i--) {
                            //$NON-NLS-1$
                            variable += str[i].length() == 0 ? " " : str[i];
                        }
                        if (variable.contains(className)) {
                            continue;
                        }
                        //$NON-NLS-1$
                        variable += ";";
                        //$NON-NLS-1$ //$NON-NLS-2$
                        localFields += "\t\t" + variable + "\n";
                    // System.out.println(variable);
                    }
                }
            }
        }
    } catch (BadLocationException e) {
        ExceptionHandler.process(e);
    } catch (JavaModelException e) {
        // e.printStackTrace();
        ExceptionHandler.process(e);
    }
    StringBuffer buff = new StringBuffer();
    //$NON-NLS-1$
    buff.append("package internal;\n\n");
    buff.append(getImports());
    //$NON-NLS-1$ //$NON-NLS-2$
    buff.append("public class " + VIEWER_CLASS_NAME + currentId + " {\n");
    buff.append(globalFields);
    //$NON-NLS-1$
    buff.append("\tpublic void myFunction(){\n");
    buff.append(localFields);
    documentOffset = buff.toString().length();
    //$NON-NLS-1$
    buff.append("\n\t\n}\n}");
    newDoc.set(buff.toString());
    return initializeViewer(composite, styles, checkCode, newDoc, documentOffset);
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) SourceField(org.eclipse.jdt.internal.core.SourceField) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) ContentAssistant(org.eclipse.jface.text.contentassist.ContentAssistant) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Region(org.eclipse.jface.text.Region) JavaCompletionProcessor(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor) IDocument(org.eclipse.jface.text.IDocument) FindReplaceDocumentAdapter(org.eclipse.jface.text.FindReplaceDocumentAdapter) BadLocationException(org.eclipse.jface.text.BadLocationException) JavaCompletionProposal(org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal)

Aggregations

SourceField (org.eclipse.jdt.internal.core.SourceField)3 JavaCompletionProposal (org.eclipse.jdt.internal.ui.text.java.JavaCompletionProposal)2 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 JavaModelException (org.eclipse.jdt.core.JavaModelException)1 AbstractMethodDeclaration (org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration)1 TypeDeclaration (org.eclipse.jdt.internal.compiler.ast.TypeDeclaration)1 ISourceType (org.eclipse.jdt.internal.compiler.env.ISourceType)1 InitializerElementInfo (org.eclipse.jdt.internal.core.InitializerElementInfo)1 SourceMethod (org.eclipse.jdt.internal.core.SourceMethod)1 SourceMethodElementInfo (org.eclipse.jdt.internal.core.SourceMethodElementInfo)1 SourceType (org.eclipse.jdt.internal.core.SourceType)1 SourceTypeElementInfo (org.eclipse.jdt.internal.core.SourceTypeElementInfo)1 JavaCompletionProcessor (org.eclipse.jdt.internal.ui.text.java.JavaCompletionProcessor)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Document (org.eclipse.jface.text.Document)1 FindReplaceDocumentAdapter (org.eclipse.jface.text.FindReplaceDocumentAdapter)1 IDocument (org.eclipse.jface.text.IDocument)1 Region (org.eclipse.jface.text.Region)1