List of usage examples for org.eclipse.jdt.core.dom AST newQualifiedName
public QualifiedName newQualifiedName(Name qualifier, SimpleName name)
From source file:com.android.ide.eclipse.adt.internal.lint.AddSuppressAnnotation.java
License:Open Source License
private Expression createLiteral(AST ast) { Expression value;// ww w .j a va2 s . c o m if (!isCodeName()) { value = ast.newQualifiedName(ast.newQualifiedName(ast.newSimpleName("Build"), //$NON-NLS-1$ ast.newSimpleName("VERSION_CODES")), //$NON-NLS-1$ ast.newSimpleName(mTargetApi)); } else { value = ast.newNumberLiteral(mTargetApi); } return value; }
From source file:com.google.devtools.j2objc.translate.ASTFactory.java
License:Apache License
public static QualifiedName newQualifiedName(AST ast, Name qualifier, SimpleName name) { QualifiedName qName = ast.newQualifiedName(qualifier, name); Types.addBinding(qName, Types.getBinding(name)); return qName; }
From source file:com.halware.nakedide.eclipse.ext.annot.utils.AstUtils.java
License:Open Source License
/** * Creates an (AST) {@link Literal} expression. * /*ww w . j av a2s. c om*/ * @param ast * @param value * @return */ public static Expression createExpression(AST ast, Object value) { if (value instanceof String) { String stringValue = (String) value; StringLiteral newStringLiteral = ast.newStringLiteral(); newStringLiteral.setLiteralValue(stringValue); return newStringLiteral; } if (value instanceof Boolean) { Boolean booleanValue = (Boolean) value; return ast.newBooleanLiteral(booleanValue); } if (value instanceof Number) { Number numberValue = (Number) value; return ast.newNumberLiteral(numberValue.toString()); } if (value instanceof AstUtils.TypeLiteralValue) { AstUtils.TypeLiteralValue classHandle = (AstUtils.TypeLiteralValue) value; TypeLiteral newTypeLiteral = ast.newTypeLiteral(); Name typeName = ast.newName(classHandle.getClassName()); Type type = ast.newSimpleType(typeName); newTypeLiteral.setType(type); return newTypeLiteral; } if (value instanceof AstUtils.QualifiedNameValue) { AstUtils.QualifiedNameValue enumMember = (AstUtils.QualifiedNameValue) value; Name enumTypeName = ast.newName(enumMember.getName()); SimpleName enumMemberName = ast.newSimpleName(enumMember.getIdentifier()); return ast.newQualifiedName(enumTypeName, enumMemberName); } return ast.newNullLiteral(); }
From source file:de.ovgu.cide.export.physical.ahead.ExportJavaFileJob.java
License:Open Source License
private void addCideJakUtilImport(CompilationUnit cunit) { AST ast = cunit.getAST(); ImportDeclaration imp = ast.newImportDeclaration(); SimpleName de = ast.newSimpleName("de"); QualifiedName deOvgu = ast.newQualifiedName(de, ast.newSimpleName("ovgu")); QualifiedName deOvguCide = ast.newQualifiedName(deOvgu, ast.newSimpleName("cide")); QualifiedName deOvguCideJakutil = ast.newQualifiedName(deOvguCide, ast.newSimpleName("jakutil")); imp.setName(deOvguCideJakutil);/*from w w w.j av a 2s .c o m*/ imp.setOnDemand(true); cunit.imports().add(imp); }
From source file:edu.buffalo.cse.green.relationships.RelationshipGenerator.java
License:Open Source License
/** * @param fullyQualifiedString - The String. * @return A qualified name representing the given fully qualified String. *//*from www .j a v a 2 s . c om*/ protected QualifiedName createQualifiedName(String fullyQualifiedString) { AST ast = getAST(); if (fullyQualifiedString.indexOf(".") == -1) { GreenException.illegalOperation("String must be fully qualified"); } int prevIndex = fullyQualifiedString.indexOf("."); String qual = fullyQualifiedString.substring(0, prevIndex); Name qualifier = ast.newSimpleName(qual); while (true) { qual = fullyQualifiedString.substring(prevIndex + 1); int index = qual.indexOf("."); if (index == -1) { break; } index += prevIndex; qual = fullyQualifiedString.substring(prevIndex + 1, index + 1); qualifier = ast.newQualifiedName(qualifier, ast.newSimpleName(qual)); prevIndex = index + 1; } return ast.newQualifiedName(qualifier, ast.newSimpleName(qual)); }
From source file:es.bsc.servicess.ide.editors.ImplementationFormPage.java
License:Apache License
/** * Modify parameter element//ww w. j a v a 2s . c om * @param serviceClass Orchestration class name * @param methodName Element method name * @param p Parameter * @throws PartInitException * @throws JavaModelException * @throws MalformedTreeException * @throws BadLocationException * @throws SAXException * @throws IOException * @throws ParserConfigurationException */ private void modifyDirection(String serviceClass, String methodName, CoreElementParameter p) throws PartInitException, JavaModelException, MalformedTreeException, BadLocationException, SAXException, IOException, ParserConfigurationException { log.debug("Modifying direction for core element" + serviceClass + "." + methodName + " parameter " + p.getName() + "(" + p.getDirection() + ")"); ICompilationUnit cu = getCEInterface(serviceClass, ((ServiceFormEditor) getEditor()).getProject(), ((ServiceFormEditor) getEditor()).getProjectMetadata()); if (cu != null) { Document document = new Document(cu.getSource()); log.debug(document.get()); String localtypeName = serviceClass.subSequence(serviceClass.lastIndexOf(".") + 1, serviceClass.length()) + "Itf"; ASTParser parser = ASTParser.newParser(AST.JLS3); // handles JDK // 1.0, 1.1, // 1.2, 1.3, // 1.4, 1.5, 1.6 parser.setSource(cu); CompilationUnit result = (CompilationUnit) parser.createAST(null); if (result != null) { result.recordModifications(); log.debug(result.toString()); AST ast = result.getAST(); java.util.List<AbstractTypeDeclaration> types = result.types(); log.debug("pack: " + result.getPackage().toString() + " types: " + result.types().size()); if (result.types().size() > 0) { boolean find = false; for (AbstractTypeDeclaration type : types) { log.debug("Type: " + type.getName().getIdentifier() + "(" + type.getName().getFullyQualifiedName() + ")"); if (type.getName().getIdentifier().equals(localtypeName)) { MethodDeclaration[] methods = ((TypeDeclaration) type).getMethods(); for (MethodDeclaration m : methods) { log.debug("method FQDN: " + m.getName().getFullyQualifiedName() + " identifier: " + m.getName().getIdentifier()); if (m.getName().getIdentifier().equals(methodName)) { java.util.List<SingleVariableDeclaration> pars = m.parameters(); for (SingleVariableDeclaration var : pars) { log.debug("var FQDN: " + var.getName().getFullyQualifiedName() + " identifier: " + var.getName().getIdentifier()); if (var.getName().getIdentifier().equals(p.getName())) { java.util.List<IExtendedModifier> mods = var.modifiers(); for (IExtendedModifier mod : mods) { log.debug("modifier: " + mod.getClass().toString()); if (mod.isAnnotation()) { if (((Annotation) mod).isNormalAnnotation()) { log.debug("annotation: " + ((NormalAnnotation) mod) .getTypeName().toString()); if (((NormalAnnotation) mod).getTypeName().toString() .equals("Parameter")) { java.util.List<MemberValuePair> vals = ((NormalAnnotation) mod) .values(); MemberValuePair dir_value = null; for (MemberValuePair v : vals) { log.debug("member: " + v.getName().getIdentifier()); if (v.getName().getIdentifier() .equals("direction")) { dir_value = v; break; } } if (dir_value == null) { dir_value = ast.newMemberValuePair(); dir_value.setName(ast.newSimpleName("direction")); QualifiedName qn = ast.newQualifiedName( ast.newSimpleName("Direction"), ast.newSimpleName(p.getDirection())); dir_value.setValue(qn); log.debug("Adding property to annotation: " + dir_value.toString()); vals.add(dir_value); } else { QualifiedName ex = (QualifiedName) dir_value .getValue(); log.debug("ValueClass: " + ex.getClass()); ex.setName(ast.newSimpleName(p.getDirection())); log.debug("Changing direction: " + dir_value.toString()); } find = true; break; } } } } if (find) break; } } if (find) break; } } if (find) break; } } if (find) { TextEdit edits = result.rewrite(document, cu.getJavaProject().getOptions(true)); edits.apply(document); String newSource = document.get(); cu.getBuffer().setContents(newSource); cu.save(null, true); log.debug("writting modifications " + newSource); } else { log.warn("Varaible and annotation not found"); } } else { log.warn("No types found in the Compilation unit from AST"); } } else { log.error("Error parsing Compilation unit with AST"); } } else { log.error("Error getting Interface Compilation Unit"); } }
From source file:lombok.eclipse.agent.PatchValEclipse.java
License:Open Source License
public static MarkerAnnotation createValAnnotation(AST ast, Annotation original, int start, int end) { MarkerAnnotation out = null;/*from w w w .ja va 2s .co m*/ try { out = Reflection.markerAnnotationConstructor.newInstance(ast); } catch (InstantiationException e) { throw Lombok.sneakyThrow(e); } catch (IllegalAccessException e) { throw Lombok.sneakyThrow(e); } catch (InvocationTargetException e) { throw Lombok.sneakyThrow(e); } if (out != null) { SimpleName valName = ast.newSimpleName("val"); valName.setSourceRange(start, end - start + 1); if (original.type instanceof SingleTypeReference) { out.setTypeName(valName); setIndex(valName, 1); } else { SimpleName lombokName = ast.newSimpleName("lombok"); lombokName.setSourceRange(start, end - start + 1); setIndex(lombokName, 1); setIndex(valName, 2); QualifiedName fullName = ast.newQualifiedName(lombokName, valName); setIndex(fullName, 1); fullName.setSourceRange(start, end - start + 1); out.setTypeName(fullName); } out.setSourceRange(start, end - start + 1); } return out; }
From source file:org.decojer.cavaj.utils.Annotations.java
License:Open Source License
/** * Decompile annotation default value (value or default value literal). * * @param defaultValue//from ww w . ja va 2 s . co m * default value * @param context * context * @return expression AST Node */ @Nullable public static Expression decompileAnnotationDefaultValue(@Nullable final Object defaultValue, @Nonnull final Element context) { final AST ast = context.getCu().getAst(); if (defaultValue == null) { return null; } if (defaultValue instanceof A) { return decompileAnnotation((A) defaultValue, context); } // could be primitive array - use slow reflection if (defaultValue.getClass().isArray()) { final int size = Array.getLength(defaultValue); if (size == 1) { // single entry autoboxing return decompileAnnotationDefaultValue(Array.get(defaultValue, 0), context); } final ArrayInitializer arrayInitializer = ast.newArrayInitializer(); for (int i = 0; i < size; ++i) { final Expression expression = decompileAnnotationDefaultValue(Array.get(defaultValue, i), context); if (expression != null) { arrayInitializer.expressions().add(expression); } } return arrayInitializer; } if (defaultValue instanceof Boolean) { return ast.newBooleanLiteral((Boolean) defaultValue); } if (defaultValue instanceof Byte) { return ast.newNumberLiteral(defaultValue.toString()); } if (defaultValue instanceof Character) { final CharacterLiteral characterLiteral = ast.newCharacterLiteral(); characterLiteral.setCharValue((Character) defaultValue); return characterLiteral; } if (defaultValue instanceof T) { final TypeLiteral typeLiteral = ast.newTypeLiteral(); typeLiteral.setType(newType((T) defaultValue, context)); return typeLiteral; } if (defaultValue instanceof Double) { return ast.newNumberLiteral(defaultValue.toString() + 'D'); } if (defaultValue instanceof F) { final F f = (F) defaultValue; if (!f.isEnum()) { log.warn("Default value field must be enum!"); } return ast.newQualifiedName(newTypeName(f.getT(), context), newSimpleName(f.getName(), ast)); } if (defaultValue instanceof Float) { return ast.newNumberLiteral(defaultValue.toString() + 'F'); } if (defaultValue instanceof Integer) { return ast.newNumberLiteral(defaultValue.toString()); } if (defaultValue instanceof Long) { return ast.newNumberLiteral(defaultValue.toString() + 'L'); } if (defaultValue instanceof Short) { return ast.newNumberLiteral(defaultValue.toString()); } if (defaultValue instanceof String) { final StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue((String) defaultValue); return stringLiteral; } log.warn("Unknown member value type '" + defaultValue.getClass().getName() + "'!"); final StringLiteral stringLiteral = ast.newStringLiteral(); stringLiteral.setLiteralValue(defaultValue.toString()); return stringLiteral; }
From source file:org.decojer.cavaj.utils.Expressions.java
License:Open Source License
@Nonnull @SuppressWarnings("null") private static Expression newLiteral2(@Nonnull final T t, @Nullable final Object value, @Nonnull final Element context) { final AST ast = context.getCu().getAst(); if (t.isRef() /* incl. T.AREF */) { if (value == null) { return ast.newNullLiteral(); }/*from ww w . j ava 2s.c o m*/ if (value instanceof T && t.isAssignableFrom(Class.class)) { final TypeLiteral typeLiteral = ast.newTypeLiteral(); typeLiteral.setType(newType((T) value, context)); return typeLiteral; } if (value instanceof String && t.isAssignableFrom(String.class)) { final StringLiteral stringLiteral = ast.newStringLiteral(); try { stringLiteral.setLiteralValue((String) value); } catch (final IllegalArgumentException e) { // TODO hmm, escaping doesn't always work? stringLiteral.setLiteralValue("<Invalid string literal>"); } return stringLiteral; } log.warn(context + ": Unknown reference type '" + t + "'!"); return ast.newNullLiteral(); } if (t.is(T.BOOLEAN)) { // we prefer boolean, even if this is a multi-type if (value instanceof Boolean) { return ast.newBooleanLiteral(((Boolean) value).booleanValue()); } if (value instanceof Number) { return ast.newBooleanLiteral(((Number) value).intValue() != 0); } if (value == null) { return ast.newBooleanLiteral(false); } log.warn("Boolean type with value '" + value + "' has type '" + value.getClass().getName() + "'!"); return ast.newBooleanLiteral( value instanceof String ? Boolean.valueOf((String) value) : true /* value is not null here */); } if (t.is(T.BYTE)) { if (value instanceof Number) { final byte b = ((Number) value).byteValue(); if (b == Byte.MAX_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Byte"), ast.newSimpleName("MAX_VALUE")); } if (b == Byte.MIN_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Byte"), ast.newSimpleName("MIN_VALUE")); } return ast.newNumberLiteral(Byte.toString(b)); } if (value == null) { return ast.newNumberLiteral(Byte.toString((byte) 0)); } log.warn("Byte type with value '" + value + "' has type '" + value.getClass().getName() + "'!"); return ast.newNumberLiteral(value.toString()); } if (t.is(T.SHORT)) { if (value instanceof Number) { final short s = ((Number) value).shortValue(); if (s == Short.MAX_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Short"), ast.newSimpleName("MAX_VALUE")); } if (s == Short.MIN_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Short"), ast.newSimpleName("MIN_VALUE")); } return ast.newNumberLiteral(Short.toString(s)); } if (value == null) { return ast.newNumberLiteral(Short.toString((short) 0)); } log.warn("Short type with value '" + value + "' has type '" + value.getClass().getName() + "'!"); return ast.newNumberLiteral(value.toString()); } if (t.is(T.INT)) { if (value instanceof Number) { final int i = ((Number) value).intValue(); if (i == Integer.MAX_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Integer"), ast.newSimpleName("MAX_VALUE")); } if (i == Integer.MIN_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Integer"), ast.newSimpleName("MIN_VALUE")); } return ast.newNumberLiteral(Integer.toString(i)); } if (value == null) { return ast.newNumberLiteral(Integer.toString(0)); } log.warn("Integer type with value '" + value + "' has type '" + value.getClass().getName() + "'!"); return ast.newNumberLiteral(value.toString()); } if (t.is(T.CHAR)) { // if this is a multi-type, we only use char if this is not already consumed; // we don't want to output strange characters if we are not very shure about this if (value instanceof Character || value instanceof Number || value instanceof String && ((String) value).length() == 1) { final char c = value instanceof Character ? (Character) value : value instanceof Number ? (char) ((Number) value).intValue() : ((String) value).charAt(0); switch (c) { case Character.MAX_VALUE: return ast.newQualifiedName(ast.newSimpleName("Character"), ast.newSimpleName("MAX_VALUE")); case Character.MIN_VALUE: return ast.newQualifiedName(ast.newSimpleName("Character"), ast.newSimpleName("MIN_VALUE")); case Character.MAX_HIGH_SURROGATE: if (context.getT().isAtLeast(Version.JVM_5)) { return ast.newQualifiedName(ast.newSimpleName("Character"), ast.newSimpleName("MAX_HIGH_SURROGATE")); } break; case Character.MAX_LOW_SURROGATE: if (context.getT().isAtLeast(Version.JVM_5)) { return ast.newQualifiedName(ast.newSimpleName("Character"), ast.newSimpleName("MAX_LOW_SURROGATE")); } break; case Character.MIN_HIGH_SURROGATE: if (context.getT().isAtLeast(Version.JVM_5)) { return ast.newQualifiedName(ast.newSimpleName("Character"), ast.newSimpleName("MIN_HIGH_SURROGATE")); } break; case Character.MIN_LOW_SURROGATE: if (context.getT().isAtLeast(Version.JVM_5)) { return ast.newQualifiedName(ast.newSimpleName("Character"), ast.newSimpleName("MIN_LOW_SURROGATE")); } break; } final CharacterLiteral characterLiteral = ast.newCharacterLiteral(); characterLiteral.setCharValue(c); return characterLiteral; } if (value == null) { final CharacterLiteral characterLiteral = ast.newCharacterLiteral(); characterLiteral.setCharValue((char) 0); return characterLiteral; } log.warn("Character type with value '" + value + "' has type '" + value.getClass().getName() + "'!"); // char is per default 'X' return ast.newCharacterLiteral(); } if (t.is(T.FLOAT)) { if (value instanceof Float || value instanceof Integer) { final float f = value instanceof Float ? (Float) value : Float.intBitsToFloat((Integer) value); if (Float.isNaN(f)) { return ast.newQualifiedName(ast.newSimpleName("Float"), ast.newSimpleName("NaN")); } if (f == Float.POSITIVE_INFINITY) { return ast.newQualifiedName(ast.newSimpleName("Float"), ast.newSimpleName("POSITIVE_INFINITY")); } if (f == Float.NEGATIVE_INFINITY) { return ast.newQualifiedName(ast.newSimpleName("Float"), ast.newSimpleName("NEGATIVE_INFINITY")); } if (f == Float.MAX_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Float"), ast.newSimpleName("MAX_VALUE")); } if (f == Float.MIN_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Float"), ast.newSimpleName("MIN_VALUE")); } if (f == Float.MIN_NORMAL) { if (context.getT().isAtLeast(Version.JVM_6)) { return ast.newQualifiedName(ast.newSimpleName("Float"), ast.newSimpleName("MIN_NORMAL")); } } return ast.newNumberLiteral(Float.toString(f) + 'F'); } if (value == null) { return ast.newNumberLiteral(Float.toString(0F) + 'F'); } log.warn("Float type with value '" + value + "' has type '" + value.getClass().getName() + "'!"); return ast.newNumberLiteral(value.toString() + 'F'); } if (t.is(T.LONG)) { if (value instanceof Long) { final long l = (Long) value; if (l == Long.MAX_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Long"), ast.newSimpleName("MAX_VALUE")); } if (l == Long.MIN_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Long"), ast.newSimpleName("MIN_VALUE")); } return ast.newNumberLiteral(Long.toString(l) + 'L'); } if (value == null) { return ast.newNumberLiteral(Long.toString(0L) + 'L'); } log.warn("Long type with value '" + value + "' has type '" + value.getClass().getName() + "'!"); return ast.newNumberLiteral(value.toString() + 'L'); } if (t.is(T.DOUBLE)) { if (value instanceof Double || value instanceof Long) { final double d = value instanceof Double ? (Double) value : Double.longBitsToDouble((Long) value); if (Double.isNaN(d)) { return ast.newQualifiedName(ast.newSimpleName("Double"), ast.newSimpleName("NaN")); } if (d == Double.POSITIVE_INFINITY) { return ast.newQualifiedName(ast.newSimpleName("Double"), ast.newSimpleName("POSITIVE_INFINITY")); } if (d == Double.NEGATIVE_INFINITY) { return ast.newQualifiedName(ast.newSimpleName("Double"), ast.newSimpleName("NEGATIVE_INFINITY")); } if (d == Double.MAX_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Double"), ast.newSimpleName("MAX_VALUE")); } if (d == Double.MIN_VALUE) { return ast.newQualifiedName(ast.newSimpleName("Double"), ast.newSimpleName("MIN_VALUE")); } if (d == Double.MIN_NORMAL) { if (context.getT().isAtLeast(Version.JVM_6)) { return ast.newQualifiedName(ast.newSimpleName("Double"), ast.newSimpleName("MIN_NORMAL")); } } return ast.newNumberLiteral(Double.toString(d) + 'D'); } if (value == null) { return ast.newNumberLiteral(Double.toString(0D) + 'D'); } log.warn("Double type with value '" + value + "' has type '" + value.getClass().getName() + "'!"); return ast.newNumberLiteral(value.toString() + 'D'); } log.warn("Unknown data type '" + t + "'!"); final StringLiteral stringLiteral = ast.newStringLiteral(); if (value != null) { stringLiteral.setLiteralValue(value.toString()); } return stringLiteral; }
From source file:org.decojer.cavaj.utils.Expressions.java
License:Open Source License
/** * New name. Handles illegal Java names. * * @param identifiers// ww w.j a va2s .c o m * identifiers * @param ast * AST * @return AST name */ @Nonnull public static Name newName(@Nonnull final String[] identifiers, @Nonnull final AST ast) { // update internalSetName(String[] if changed final int count = identifiers.length; if (count == 0) { throw new IllegalArgumentException(); } Name result = newSimpleName(identifiers[0], ast); for (int i = 1; i < count; i++) { final SimpleName name = newSimpleName(identifiers[i], ast); result = ast.newQualifiedName(result, name); assert result != null; } return result; }