use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class JavaContext method addStaticImport.
/**
* Adds a static import for the member with name <code>qualifiedMemberName</code>. The member is
* either a static field or a static method or a '*' to import all static members of a type.
*
* @param qualifiedMemberName the fully qualified name of the member to import or a qualified type
* name plus a '.*' suffix.
* @return returns either the simple member name if the import was successful or else the qualified name.
* @since 3.4
*/
public String addStaticImport(String qualifiedMemberName) {
if (isReadOnly())
return qualifiedMemberName;
ICompilationUnit cu = getCompilationUnit();
if (cu == null)
return qualifiedMemberName;
int memberOffset = qualifiedMemberName.lastIndexOf('.');
if (memberOffset == -1)
return qualifiedMemberName;
String typeName = qualifiedMemberName.substring(0, memberOffset);
String memberName = qualifiedMemberName.substring(memberOffset + 1, qualifiedMemberName.length());
try {
boolean isField;
if ("*".equals(memberName)) {
//$NON-NLS-1$
isField = true;
} else {
IJavaProject javaProject = cu.getJavaProject();
IType type = javaProject.findType(typeName);
if (type == null)
return qualifiedMemberName;
IField field = type.getField(memberName);
if (field.exists()) {
isField = true;
} else if (hasMethod(type, memberName)) {
isField = false;
} else {
return qualifiedMemberName;
}
}
CompilationUnit root = getASTRoot(cu);
if (fImportRewrite == null) {
if (root == null) {
fImportRewrite = StubUtility.createImportRewrite(cu, true);
} else {
fImportRewrite = StubUtility.createImportRewrite(root, true);
}
}
ImportRewriteContext context;
if (root == null)
context = null;
else
context = new ContextSensitiveImportRewriteContext(root, getCompletionOffset(), fImportRewrite);
return fImportRewrite.addStaticImport(typeName, memberName, isField, context);
} catch (JavaModelException e) {
handleException(null, e);
return typeName;
}
}
use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class JavaTextSelection method resolveSelectedNodes.
public ASTNode[] resolveSelectedNodes() {
if (fNodesRequested)
return fSelectedNodes;
fNodesRequested = true;
CompilationUnit root = resolvePartialAstAtOffset();
if (root == null)
return null;
Selection ds = Selection.createFromStartLength(getOffset(), getLength());
SelectionAnalyzer analyzer = new SelectionAnalyzer(ds, false);
root.accept(analyzer);
fSelectedNodes = analyzer.getSelectedNodes();
fCoveringNode = analyzer.getLastCoveringNode();
return fSelectedNodes;
}
use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class ControlStatementsCleanUp method createFix.
/**
* {@inheritDoc}
*/
@Override
public ICleanUpFix createFix(CleanUpContext context) throws CoreException {
CompilationUnit compilationUnit = context.getAST();
if (compilationUnit == null)
return null;
boolean useBlocks = isEnabled(CleanUpConstants.CONTROL_STATEMENTS_USE_BLOCKS);
if (!useBlocks)
return null;
return ControlStatementsFix.createCleanUp(compilationUnit, isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_ALWAYS), isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NEVER), isEnabled(CleanUpConstants.CONTROL_STATMENTS_USE_BLOCKS_NO_FOR_RETURN_AND_THROW));
}
use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class ExpressionsCleanUp method createFix.
/**
* {@inheritDoc}
*/
@Override
public ICleanUpFix createFix(CleanUpContext context) throws CoreException {
CompilationUnit compilationUnit = context.getAST();
if (compilationUnit == null)
return null;
boolean usePrentheses = isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES);
if (!usePrentheses)
return null;
return ExpressionsFix.createCleanUp(compilationUnit, isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_ALWAYS), isEnabled(CleanUpConstants.EXPRESSIONS_USE_PARENTHESES_NEVER));
}
use of org.eclipse.jdt.core.dom.CompilationUnit in project che by eclipse.
the class ASTProvider method getAST.
/**
* Returns a shared compilation unit AST for the given Java element.
* <p>
* Clients are not allowed to modify the AST and must synchronize all access to its nodes.
* </p>
*
* @param input the Java element, must not be <code>null</code>
* @param waitFlag {@link SharedASTProvider#WAIT_YES}, {@link SharedASTProvider#WAIT_NO} or
* {@link SharedASTProvider#WAIT_ACTIVE_ONLY}
* @param progressMonitor the progress monitor or <code>null</code>
* @return the AST or <code>null</code> if the AST is not available
*/
public CompilationUnit getAST(final ITypeRoot input, SharedASTProvider.WAIT_FLAG waitFlag, IProgressMonitor progressMonitor) {
if (input == null || waitFlag == null)
//$NON-NLS-1$
throw new IllegalArgumentException("input or wait flag are null");
if (progressMonitor != null && progressMonitor.isCanceled())
return null;
boolean isActiveElement;
synchronized (this) {
//input.equals(fActiveJavaElement);
isActiveElement = false;
if (isActiveElement) {
if (fAST != null) {
if (DEBUG)
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "returning cached AST:" + toString(fAST) + " for: " + input.getElementName());
return fAST;
}
if (waitFlag == SharedASTProvider.WAIT_NO) {
if (DEBUG)
//$NON-NLS-1$ //$NON-NLS-2$
System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "returning null (WAIT_NO) for: " + input.getElementName());
return null;
}
}
}
final boolean canReturnNull = waitFlag == SharedASTProvider.WAIT_NO || (waitFlag == SharedASTProvider.WAIT_ACTIVE_ONLY && !(isActiveElement && fAST == null));
boolean isReconciling = false;
final ITypeRoot activeElement;
if (isActiveElement) {
// synchronized (fReconcileLock) {
// activeElement= fReconcilingJavaElement;
// isReconciling= isReconciling(input);
// if (!isReconciling && !canReturnNull)
// aboutToBeReconciled(input);
// }
} else
activeElement = null;
if (isReconciling) {
// }
return getAST(input, waitFlag, progressMonitor);
}
/*else if (canReturnNull)
return null;*/
CompilationUnit ast = null;
try {
ast = createAST(input, progressMonitor);
if (progressMonitor != null && progressMonitor.isCanceled()) {
ast = null;
if (DEBUG)
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "Ignore created AST for: " + input.getElementName() + " - operation has been cancelled");
}
} finally {
if (isActiveElement) {
if (fAST != null) {
// in the meantime, reconcile created a new AST. Return that one
if (DEBUG)
//$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
System.out.println(getThreadName() + " - " + DEBUG_PREFIX + "Ignore created AST for " + input.getElementName() + " - AST from reconciler is newer");
reconciled(fAST, input, null);
return fAST;
} else
reconciled(ast, input, null);
}
}
return ast;
}
Aggregations