Example usage for org.eclipse.jdt.core IType resolveType

List of usage examples for org.eclipse.jdt.core IType resolveType

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType resolveType.

Prototype

String[][] resolveType(String typeName) throws JavaModelException;

Source Link

Document

Resolves the given type name within the context of this type (depending on the type hierarchy and its imports).

Usage

From source file:at.bestsolution.efxclipse.tooling.fxgraph.ui.quickfix.FXGraphQuickfixProvider.java

License:Open Source License

@Fix(FXGraphJavaValidator.UNKNOWN_CONTROLLER_FIELD)
public void fixAddControllerField(final Issue issue, IssueResolutionAcceptor acceptor) {
    acceptor.accept(issue, "Add controller field",
            "Add a field of '" + issue.getData()[0] + "' to the controller '" + issue.getData()[1] + "'", null,
            new IModification() {

                @Override//from w  ww  . j  av a 2  s. c  om
                public void apply(IModificationContext context) throws Exception {
                    IJavaProject p = context.getXtextDocument()
                            .readOnly(new IUnitOfWork<IJavaProject, XtextResource>() {

                                @Override
                                public IJavaProject exec(XtextResource state) throws Exception {
                                    return provider.getJavaProject(state.getResourceSet());
                                }

                            });

                    IType type = p.findType(issue.getData()[1]);

                    String[][] resolvedType = type.resolveType("FXML");

                    if (resolvedType == null) {
                        type.getCompilationUnit().createImport("javafx.fxml.FXML", null,
                                new NullProgressMonitor());
                    }

                    resolvedType = type.resolveType(Signature.getSimpleName(issue.getData()[2]));

                    if (resolvedType == null) {
                        type.getCompilationUnit().createImport(issue.getData()[2], null,
                                new NullProgressMonitor());
                    }

                    type.createField("@FXML " + Signature.getSimpleName(issue.getData()[2]) + " "
                            + issue.getData()[0] + ";", null, true, new NullProgressMonitor());
                }
            });
    acceptor.accept(issue, "Remove id field", "Remove the id field from the element", null,
            new IModification() {

                @Override
                public void apply(IModificationContext context) throws Exception {
                    IXtextDocument xtextDocument = context.getXtextDocument();
                    StringBuilder b = new StringBuilder();
                    int start = issue.getOffset();
                    while (!b.toString().startsWith("id")) {
                        b.insert(0, xtextDocument.getChar(--start));
                    }

                    xtextDocument.replace(start, (b + issue.getData()[0]).length(), "");
                }
            });
}

From source file:at.bestsolution.efxclipse.tooling.fxgraph.ui.quickfix.FXGraphQuickfixProvider.java

License:Open Source License

@Fix(FXGraphJavaValidator.UNKNOWN_CONTROLLER_METHOD)
public void fixAddControllerMethod(final Issue issue, IssueResolutionAcceptor acceptor) {
    final String methodName = issue.getData()[0];
    final String controllerClass = issue.getData()[1];
    final String argType = issue.getData()[2];
    final String simpleArgType = Signature.getSimpleName(argType);
    acceptor.accept(issue, "Add controller method " + methodName + "(" + simpleArgType + ")",
            "Add a controller method '" + methodName + "(" + simpleArgType + ")' to controller '"
                    + controllerClass + "'",
            null, new IModification() {

                @Override// ww w .ja v  a2  s .c o m
                public void apply(IModificationContext context) throws Exception {
                    IJavaProject p = context.getXtextDocument()
                            .readOnly(new IUnitOfWork<IJavaProject, XtextResource>() {

                                @Override
                                public IJavaProject exec(XtextResource state) throws Exception {
                                    return provider.getJavaProject(state.getResourceSet());
                                }

                            });

                    IType type = p.findType(controllerClass);

                    String[][] resolvedType = type.resolveType("FXML");

                    if (resolvedType == null) {
                        type.getCompilationUnit().createImport("javafx.fxml.FXML", null,
                                new NullProgressMonitor());
                    }

                    resolvedType = type.resolveType(simpleArgType);

                    if (resolvedType == null) {
                        type.getCompilationUnit().createImport(argType, null, new NullProgressMonitor());
                    }

                    type.createMethod("@FXML public void " + methodName + "(" + simpleArgType + " event) {}",
                            null, true, new NullProgressMonitor());
                }
            });
    acceptor.accept(issue, "Add controller method " + methodName + "()",
            "Add a controller method '" + methodName + "()' to controller '" + controllerClass + "'", null,
            new IModification() {

                @Override
                public void apply(IModificationContext context) throws Exception {
                    IJavaProject p = context.getXtextDocument()
                            .readOnly(new IUnitOfWork<IJavaProject, XtextResource>() {

                                @Override
                                public IJavaProject exec(XtextResource state) throws Exception {
                                    return provider.getJavaProject(state.getResourceSet());
                                }

                            });

                    IType type = p.findType(controllerClass);

                    String[][] resolvedType = type.resolveType("FXML");

                    if (resolvedType == null) {
                        type.getCompilationUnit().createImport("javafx.fxml.FXML", null,
                                new NullProgressMonitor());
                    }

                    type.createMethod("@FXML public void " + methodName + "() {}", null, true,
                            new NullProgressMonitor());
                }
            });
}

From source file:at.bestsolution.efxclipse.tooling.fxgraph.ui.util.JDTHelper.java

License:Open Source License

public TypeData getTypeData(IJavaProject jproject, IType jdtType) {
    if (jdtType == null) {
        return null;
    }//from  ww  w  .  j a  v a 2 s .  c  o m

    TypeData data = typeCache.get(jdtType.getFullyQualifiedName());
    if (data == null) {
        try {
            List<IMethod> allMethods = new ArrayList<IMethod>();
            allMethods.addAll(Arrays.asList(jdtType.getMethods()));

            IType parentType = jdtType;

            while (parentType != null && parentType.getSuperclassName() != null) {
                if (parentType instanceof SourceType) {
                    String[][] typeDefs = parentType.resolveType(parentType.getSuperclassName());
                    if (typeDefs != null) {
                        for (String[] type : typeDefs) {
                            parentType = jproject.findType(type[0] + "." + type[1]);
                        }
                    }
                } else {
                    parentType = jproject.findType(parentType.getSuperclassName());
                }

                if (parentType != null) {
                    allMethods.addAll(Arrays.asList(parentType.getMethods()));
                }
            }
            data = createData(allMethods, jproject);
            if (!(jdtType instanceof SourceType)) {
                typeCache.put(jdtType.getFullyQualifiedName(), data);
            }
        } catch (JavaModelException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return data;
}

From source file:at.bestsolution.efxclipse.tooling.fxml.compile.FxmlAnnotationCompilationParticipant.java

License:Open Source License

/**
 * @param context/*from   w w  w . j av  a  2 s.  c o m*/
 */
private List<CategorizedProblem> checkCU(final ICompilationUnit unit,
        final Collection<CategorizedProblem> existingProblems) {
    List<CategorizedProblem> problems = new ArrayList<CategorizedProblem>();
    if (existingProblems != null) {
        problems.addAll(existingProblems);
    }
    List<String> fxmlMethods = new ArrayList<String>();
    try {
        IJavaProject project = unit.getJavaProject();
        for (IType type : unit.getTypes()) {
            for (IMethod method : type.getMethods()) {
                for (IAnnotation a : method.getAnnotations()) {
                    if ("FXML".equals(a.getElementName())) { ////$NON-NLS-1$
                        if (fxmlMethods.contains(method.getElementName())) {
                            DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(),
                                    "JavaFX controller method name is not unique: " //$NON-NLS-1$
                                            + method.getElementName(),
                                    IProblem.ExternalProblemNotFixable, new String[0],
                                    ProblemSeverities.Warning, method.getSourceRange().getOffset(),
                                    method.getSourceRange().getOffset() + method.getSourceRange().getLength(),
                                    getMethodLineNumber(type, method), 0);
                            problems.add(problem);
                        }
                        fxmlMethods.add(method.getElementName());

                        switch (method.getNumberOfParameters()) {
                        case 0:
                            break;
                        case 1: {
                            ILocalVariable pType = method.getParameters()[0];
                            String[][] resolvedType = type
                                    .resolveType(Signature.toString(pType.getTypeSignature()));
                            IType parameterType = null;
                            if (resolvedType != null) {
                                parameterType = project.findType(resolvedType[0][0] + "." + resolvedType[0][1]); //$NON-NLS-1$
                            }
                            if (resolvedType == null || !Util.assignable(parameterType,
                                    project.findType("javafx.event.Event"))) { ////$NON-NLS-1$
                                DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(),
                                        "Parameter '" //$NON-NLS-1$
                                                + pType.getElementName()
                                                + "' is not assignable to javafx.event.Event", //$NON-NLS-1$
                                        IProblem.ExternalProblemNotFixable, new String[0],
                                        ProblemSeverities.Warning, pType.getSourceRange().getOffset(),
                                        pType.getSourceRange().getOffset() + pType.getSourceRange().getLength(),
                                        getMethodLineNumber(type, method), 0);
                                problems.add(problem);
                            }

                        }
                            break;
                        default: {
                            DefaultProblem problem = new DefaultProblem(unit.getElementName().toCharArray(),
                                    "JavaFX controller method must have 0 or exactly 1 argument", //$NON-NLS-1$
                                    IProblem.ExternalProblemNotFixable, new String[0],
                                    ProblemSeverities.Warning, method.getSourceRange().getOffset(),
                                    method.getSourceRange().getOffset() + method.getSourceRange().getLength(),
                                    getMethodLineNumber(type, method), 0);
                            problems.add(problem);
                        }
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return problems;
}

From source file:at.bestsolution.efxclipse.tooling.jdt.ui.internal.FXBeanJavaCompletionProposalComputer.java

License:Open Source License

private IType toType(IType t, String typeSig) throws JavaModelException {
    String erasedType = Signature.getTypeErasure(Signature.toString(typeSig));
    String[][] types = t.resolveType(erasedType);
    if (types != null && types.length == 1) {
        StringBuilder b = new StringBuilder();
        for (String p : types[0]) {
            if (b.length() > 0) {
                b.append(".");
            }/*from w w w  .  ja  v  a 2s.c  o  m*/
            b.append(p);
        }
        return t.getJavaProject().findType(b.toString());
    }
    return null;
}

From source file:at.bestsolution.efxclipse.tooling.model.internal.utils.Util.java

License:Open Source License

public static String getFQNType(IType referenceType, String name) throws JavaModelException {
    // no need to resolve it is already an FQN
    if (name.contains(".")) {
        return name;
    }//from w  w w. j  a v  a  2s .  c  o m

    // This is a primitive type
    for (Type t : IFXPrimitiveProperty.Type.values()) {
        if (t.jvmType().equals(name)) {
            return null;
        }
    }

    String[][] parts = referenceType.resolveType(name);
    if (parts != null && parts.length > 0) {
        return toFQN(parts[0]);
    } else {
        //FIXME Log it
    }
    return null;
}

From source file:at.bestsolution.fxide.jdt.editor.internal.SignatureUtil.java

License:Open Source License

/**
 * Returns the qualified signature corresponding to
 * <code>signature</code>./* w  w  w  . j a  v  a2s. c  o m*/
 *
 * @param signature the signature to qualify
 * @param context the type inside which an unqualified type will be
 *        resolved to find the qualifier, or <code>null</code> if no
 *        context is available
 * @return the qualified signature
 */
public static String qualifySignature(final String signature, final IType context) {
    if (context == null)
        return signature;

    String qualifier = Signature.getSignatureQualifier(signature);
    if (qualifier.length() > 0)
        return signature;

    String elementType = Signature.getElementType(signature);
    String erasure = Signature.getTypeErasure(elementType);
    String simpleName = Signature.getSignatureSimpleName(erasure);
    String genericSimpleName = Signature.getSignatureSimpleName(elementType);

    int dim = Signature.getArrayCount(signature);

    try {
        String[][] strings = context.resolveType(simpleName);
        if (strings != null && strings.length > 0)
            qualifier = strings[0][0];
    } catch (JavaModelException e) {
        // ignore - not found
    }

    if (qualifier.length() == 0)
        return signature;

    String qualifiedType = Signature.toQualifiedName(new String[] { qualifier, genericSimpleName });
    String qualifiedSignature = Signature.createTypeSignature(qualifiedType, true);
    String newSignature = Signature.createArraySignature(qualifiedSignature, dim);

    return newSignature;
}

From source file:ca.ecliptical.pde.ds.search.DescriptorQueryParticipant.java

License:Open Source License

private IType findSuperclassType(IType type, IJavaProject project, IProgressMonitor monitor)
        throws JavaModelException, IllegalArgumentException {
    String superSig = type.getSuperclassTypeSignature();
    if (superSig == null)
        return null;

    String superName = Signature.toString(Signature.getTypeErasure(superSig));
    if (type.isResolved())
        return project.findType(superName, monitor);

    String[][] resolvedNames = type.resolveType(superName);
    if (resolvedNames == null || resolvedNames.length == 0)
        return null;

    return project.findType(resolvedNames[0][0], resolvedNames[0][1], monitor);
}

From source file:ca.mcgill.cs.swevo.jayfx.FastConverter.java

License:Open Source License

public String resolveType(String pType, final IType pEnclosingType) throws ConversionException {
    String lReturn = "";
    int lDepth = 0;
    int lIndex = 0;
    while (pType.charAt(lIndex) == Signature.C_ARRAY) {
        lDepth++;//w  ww  . j av  a  2  s  .  co m
        lIndex++;
    }

    if (pType.charAt(lIndex) == Signature.C_BYTE || pType.charAt(lIndex) == Signature.C_CHAR
            || pType.charAt(lIndex) == Signature.C_DOUBLE || pType.charAt(lIndex) == Signature.C_FLOAT
            || pType.charAt(lIndex) == Signature.C_INT || pType.charAt(lIndex) == Signature.C_LONG
            || pType.charAt(lIndex) == Signature.C_SHORT || pType.charAt(lIndex) == Signature.C_VOID
            || pType.charAt(lIndex) == Signature.C_BOOLEAN || pType.charAt(lIndex) == Signature.C_RESOLVED)
        lReturn = pType;
    else
        try {
            pType = Signature.getTypeErasure(pType);
            final int lIndex2 = pType.indexOf(Signature.C_NAME_END);
            final String lType = pType.substring(lIndex + 1, lIndex2);
            final String[][] lTypes = pEnclosingType.resolveType(lType);
            if (lTypes == null)
                throw new ConversionException("Cannot convert type " + lType + " in " + pEnclosingType);
            if (lTypes.length != 1)
                throw new ConversionException("Cannot convert type " + lType + " in " + pEnclosingType);
            for (int i = 0; i < lDepth; i++)
                lReturn += "[";
            lReturn += "L" + lTypes[0][0] + "." + lTypes[0][1].replace('.', '$') + ";";
        } catch (final JavaModelException pException) {
            throw new ConversionException(pException);
        }
    return lReturn;
}

From source file:com.aqua.wikiwizard.JavaModelUtils.java

License:Apache License

/**
 * Returns the name of the class/*from  ww  w . j  a  va  2s. co m*/
 * @param target
 *          The given type
 * @return
 *          String
 * @throws Exception
 */
public static String getClassName(IType target) throws Exception {
    String signature = target.getElementName();
    String[][] rt = target.resolveType(signature);
    if (rt != null && rt.length > 0 && rt[0] != null && rt[0].length == 2) {
        return rt[0][0] + "." + rt[0][1];
    }
    return null;
}